BookmarkSubscribeRSS Feed
Melk
Lapis Lazuli | Level 10

I have a dataset with patients with at least 2 observations - observed longitudinally. I would like to turn this into an independent dataset as below. What is the most efficient way to do this?

 

 

OLD:

 

ID    YEAR

1        1980

1        1990

2        1975

2        1980

2        1985

 

 

NEW:

 

ID    YEAR     YEARS_UNTIL_2ND  YEARS_UNTIL_LAST      NUM_OBS

1     1980                10                                  10                             2                   

2     1975                 5                                   10                             3

 

5 REPLIES 5
novinosrin
Tourmaline | Level 20
data have;
input ID    YEAR;
datalines;
1        1980
1        1990
2        1975
2        1980
2        1985
;

data want;
do _n_=1 by 1 until(last.id);
set have;
by id;
if _n_=1 then _t=year;
 else if _n_=2 then YEARS_UNTIL_2ND=year-_t;
if last.id then do;YEARS_UNTIL_LAST =year-_t;NUM_OBS=_n_;year=_t;end;
end;
drop _:;
run;
Melk
Lapis Lazuli | Level 10

I actually have more variables in my dataset so this is keeping data for my last event, but replacing year with _t.  I think it was meant to work only if we have these specific variables  in the dataset, but how to I maintain the first?

novinosrin
Tourmaline | Level 20

Can you please modify your sample

 

Melk
Lapis Lazuli | Level 10

OLD:

 

ID    YEAR         SYS     DIAS ......................(just few of hundreds of vars)

1        1980        120        90

1        1990        130        85

2        1975        160       100

2        1980        146        98

2        1985        140        88

 

 

NEW:

 

ID    YEAR     YEARS_UNTIL_2ND  YEARS_UNTIL_LAST      NUM_OBS          SYS     DIAS.................(just few of hundreds of vars)

1     1980                10                                  10                             2                 120        90                 

2     1975                 5                                   10                             3                 160       100

novinosrin
Tourmaline | Level 20

see if this works, I am off to class now at my college. If any issues, let me know. I will look into it in 3-4 hours

 


data have;
input ID    YEAR SYS     DIAS;
datalines;
1        1980        120        90
1        1990        130        85
2        1975        160       100
2        1980        146        98
2        1985        140        88
;

data want;
if _n_=1 then do;
  dcl hash H (dataset:'have(obs=0)',ordered: "A") ;
   h.definekey  ("id") ;
   h.definedata (all:'y') ;
   h.definedone () ;
   end;
do _n_=1 by 1 until(last.id);
set have;
by id;
if _n_=1 then do; _t=year;h.add();end;
 else if _n_=2 then YEARS_UNTIL_2ND=year-_t;
if last.id then do;YEARS_UNTIL_LAST =year-_t;NUM_OBS=_n_;year=_t;h.find();end;
end;
drop _:;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1676 views
  • 0 likes
  • 2 in conversation