Hi there,
MY data looks like this. It's ordered by ID and date
BAN | col_date |
100023 | 05-May-20 |
100023 | 04-Jun-20 |
123455 | 05-May-20 |
I need an output like below where the date from the second row becomes a variable in the first row and so on. Grouping has to be by ID. if there is only one record for the ID then the next date would be blank. ID could have single or multiple rows
BAN | col_date | next_date |
100023 | 05-May-20 | 04-Jun-20 |
100023 | 04-Jun-20 | . |
123455 | 05-May-20 | . |
I tried sorting the data by ID descending date and the using the dif function to get the difference between dates but it's not working. What am i doing wrong? direction is appreciated.
Data test;
set Non;
by ID ;
Difference = Dif(col_date);
run;
Use a "look-ahead":
data want;
merge
have
have (firstobs=2 rename=(ban=_ban col_date=next_date))
;
if ban ne _ban then next_date = .;
drop _ban;
run;
Untested, posted from my tablet.
Hi there,
MY data looks like this. It's ordered by ID and date
BAN | col_date |
100023 | 05-May-20 |
100023 | 04-Jun-20 |
123455 | 05-May-20 |
I need an output like below where the date from the second row becomes a variable in the first row and so on. Grouping has to be by ID. if there is only one record for the ID then the next date would be blank. ID could have single or multiple rows
BAN | col_date | next_date |
100023 | 05-May-20 | 04-Jun-20 |
100023 | 04-Jun-20 | . |
123455 | 05-May-20 | . |
I tried sorting the data by ID descending date and the using the dif function to get the difference between dates but it's not working. What am i doing wrong? direction is appreciated.
Data test;
set Non;
by ID ;
Difference = Dif(col_date);
run;
Please post the same question only once.
I suggest that "we" answer only here.
Something like below should do.
proc sort data=have out=want;
by id descending col_date;
run;
data want;
set want;
by id;
format next_date date9.;
next_date=lag(col_date);
if first.id then call missing(next_date);
run;
If you have a license to SAS/ETS, use proc expand:
proc expand data=have out=want;
by BAN;
convert col_date=next_date / transformout=(lead);
run;
Note that, as a bonus, the format associated with col_date will also be given to next_date by the procedure.
Use a "look-ahead":
data want;
merge
have
have (firstobs=2 rename=(ban=_ban col_date=next_date))
;
if ban ne _ban then next_date = .;
drop _ban;
run;
Untested, posted from my tablet.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.