Following table - i would like to subtract randdate and start date and convert into "Day" but if randdate = startdate then i want that time to be Day 1, rather than day 0
subjectid | startdate | randdate |
1 | 1/1/2019 | 1/1/2019 |
1 | 1/2/2019 | 1/1/2019 |
1 | 1/3/2019 | 1/1/2019 |
1 | 1/4/2019 | 1/1/2019 |
Something like this
subjectid | startdate | randdate | group |
1 | 1/1/2019 | 1/1/2019 | Day 1 |
1 | 1/2/2019 | 1/1/2019 | Day 2 |
1 | 1/2/2019 | 1/1/2019 | Day 2 |
1 | 1/3/2019 | 1/1/2019 | Day 3 |
Right now i can only do randdate - startdate and recode if subtract=0 then group = "day 1" but this is tedious
The "tedious" way is fine, but you can see if you find this more appealing:
days = startdate - randdate + (startdate >= randdate);
Is startdate a numeric variable or a character variable?
Is randdate a numeric variable or a character variable?
Hi @radhikaa4
The solution you described is the easiest.
You can also define your own function using proc fmcp. It is easier to maintain if the rule is going to change in the future and if you use it in multiple datasets.
/* Function definition -> Fcalc_Day() with 2 arguments as defined below */
libname lib_fcmp "&path."; /* to be specified */
proc fcmp outlib=lib_fcmp.functions.dev;
function Fcalc_Day(randdate, startdate);
if randdate > startdate then return (startdate - randdate);
if randdate <= startdate then return (startdate - randdate + 1);
endsub;
quit;
options cmplib=lib_fcmp.functions;
/* Use case */
data have;
input subjectid startdate : mmddyy10. randdate : mmddyy10.;
format startdate randdate mmddyy10.;
cards;
1 12/31/2018 1/1/2019
1 1/1/2019 1/1/2019
1 1/2/2019 1/1/2019
1 1/3/2019 1/1/2019
1 1/4/2019 1/1/2019
;
run;
data want;
set have;
day = Fcalc_Day(randdate,startdate);
run;
The "tedious" way is fine, but you can see if you find this more appealing:
days = startdate - randdate + (startdate >= randdate);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.