Hi guys,
suppose to have the following dataset:
data have;
input ID :$20. Admission :date09. Discharge :date09.;
format Admission date9. Discharge date9.;
cards;
0001 13JAN2015 20JAN2015
0001 21FEB2015 31DEC2015
0001 01MAR2018 30SEP2018
0001 01JAN2019 31DEC2019
0002 01JAN2015 31DEC2015
0002 01JAN2019 31OCT2019
0002 01NOV2019 31DEC2020
;
I need to add a variable indicating the year of admission from start of February of one year to the end of January of the next year starting from 2015 and ending to 2021. All happening before will be set to 0. The desired output should be:
data db;
input ID :$20. Admission :date09. Discharge :date09. year;
format Admission date9. Discharge date9.;
cards;
0001 13JAN2015 20JAN2015 0
0001 21FEB2015 31DEC2015 2015
0001 01MAR2018 30SEP2018 2018
0001 01JAN2019 31DEC2019 2018
0002 01JAN2015 31DEC2015 0
0002 01JAN2019 31OCT2019 2018
0002 01NOV2019 31DEC2020 2019
;
In other words since the admission of 0001 on 13JAN2015 happens before February 2015 (start), the variable takes value 0. The same for ID 0002 on 01JAN2015.
Note that only admission date will be taken into consideration for the evaluation.
Thank you in advance
data want;
set have;
if admission<'01FEB2015'd then year=0;
else do;
year=year(admission);
if month(admission)=1 then year=year-1;
end;
run;
P.S.: your data have code has an error, it doesn't run. It's easy to fix, but you should fix it, rather than having us fix your errors.
data want;
set have;
if admission<'01FEB2015'd then year=0;
else do;
year=year(admission);
if month(admission)=1 then year=year-1;
end;
run;
P.S.: your data have code has an error, it doesn't run. It's easy to fix, but you should fix it, rather than having us fix your errors.
Or
data want; set have; if admission<'01FEB2015'd then year=0; else year=year(admission) - (month(admission)=1); run;
Using the bit that SAS returns 1 or 0 for comparisons.
This sort of offset "year" is common when needing alternate "year" start/end such as different fiscal years or in one of my projects the start/end of a "water year".
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.