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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.