Search code examples
arrayssasrowssubtraction

How to subtract rows from first row, per subject?


I have subject ids with measurements at different ages. It looks like this: enter image description here

Subject Gender Datasource MHGCAT_COD_4 MHGCAT_COD_6 MHGCAT_COD_11 age_ga
124       1       1         21            122          1212         38
124       1       1         21            122          1212         39
124       1       1         21            122          1212         41
124       1       1         21            122          1212         43
125       1       1         21            122          1212         33
125       1       1         21            122          1212         38

I want to create an extra column that computes like this:

Subject Gender Datasource MHGCAT_COD_4 MHGCAT_COD_6 MHGCAT_COD_11 age_ga  window
124       1       1         21            122          1212         38      0
124       1       1         21            122          1212         39      1
124       1       1         21            122          1212         41      3
124       1       1         21            122          1212         43      5
125       1       1         21            122          1212         33      0 
125       1       1         21            122          1212         38      5

I am using SAS to do this. THanks!

Image attached in case the format goes haywire.


Solution

  • Use BY variable processing.

    data want ;
      set have ;
      by subject age_ga ;
      if first.subject then index_age = age_ga ;
      retain index_age ;
      window = age_ga - index_age;
    run;