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);
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.