student seq date1 date2
'1' 1 2017-11-11 2017-11-18
'1' 2 2017-11-18T14:57 2018-02-14
'1' 3 2018-02-14 2018-02-28
'1' 4 2018-02-28 2018-03-14
'2' 1 2018-02-14 2018-02-28
'2' 2 2018-02-28T09:18 2018-03-14
all the variables are categorical. how do we subtract 1 from the date2 only if the next record of date1 doesn't have time components attached to it? Resulting data should be:
subj seq date1 date2
'1' 1 2017-11-11 2017-11-18
'1' 2 2017-11-18T14:57 2018-02-13
'1' 3 2018-02-14 2018-02-27
'1' 4 2018-02-28 2018-03-14
'2' 1 2018-02-14 2018-02-28
'2' 2 2018-02-28T09:18 2018-03-14
I tried to create a sas dataset but that gave invalid sas variable error.. So the below code doesn't work, but if this can help:
data test ;
input student $ seq date1 $ date2 $ ;
datalines;
1 1 2017-11-11 2017-11-18
1 2 2017-11-18T14:57 2018-02-14
1 3 2018-02-14 2018-02-28
1 4 2018-02-28 2018-03-14
2 1 2018-02-14 2018-02-28
2 2 2018-02-28T09:18 2018-03-14
;
What I tried so far: first, sort
proc sort data=test out=test1;
by subjid seq;
run;
data test2;
set test1;
by subjid seq;
if length(date2) >= 10 and not last.subjid then do; *<-by the way, both dates can contain time components;
date2_n= input(date2, yymmdd10.)-1;
date2_c= strip(put(date2_n, yymmdd10.)); *<- I need a character version of the new date later;
end;
run;
But this subtracts all not last dates even though the next date1 has time component
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.