Hello everyone,
I have the below dataset in SAS, and I want to produce the "flag" column:
What I want to do is to check each date for each ID if it's between any date_start and date_end or not, if yes then flag will take 1, else 0
Thank you in advance!
Why is flag = 1 in obs 2 and 0 in obs 3?
@PeterClemmensen for obs=2 for ID=1, date_start=07/12/1998<=date=01/10/2001<=date_end=01/09/2002 ==> flag=1
and in obs=3 for ID=1, date=01/10/2002 > date_end=01/09/2002 ==> date is not included in any ranges of dates of date_start-date_end that's why flag=0
If both start and end dates are null then you want 1.
What if only one of them is null?
B.
Something like this should do the job:
data have;
call streaminit(123);
do ID = 1 to 3;
do date = today()-3 to today()+3;
mi = rand("integer",-10,10);
mx = rand("integer",-10,10);
start_date = date + min(mi,mx);
end_date = date + max(mi,mx);
if date = today() then end_date = .;
output;
end;
end;
format date start_date end_date yymmdd10.;
drop mi mx;
run;
data want;
set have;
if . < start_date <= end_date then
flag = start_date <= date <= end_date;
run;
Bart
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!
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.