I have a data containing names, SSN, date of service, and oranges as column names. Each person is to receive one orange within a week, i.e. any day from sunday to saturday.
I need help with a sas code to flag all SSN that received more than one orange within the specified one week period. The code below is flagging those who received oranges on sunday as well, however, any sunday is the first day of the week. Thanks!
data mydata;
by SSN date;
days_passed=dif(date);
if SSN=0 and days_passed in (1,2,3,4,5,6) then event=1;
drop days_passed;
run;
If the goal is to check by calendar week then try using INTNX() function with week interval.
data want;
set have ;
by SSN date;
week=intnx('week',date,0,'b');
if first.ssn then n_orange=0;
if week ne lag(week) then n_orange=0;
n_orange+1;
event=(n_orange>1) ;
run;
You want to do something different for the first value of each SSN, assuming your data is sorted by SSN and date.
Perhaps:
data mydata; by SSN date; days_passed=dif(date); if first.ssn then event=0; else if days_passed in (1,2,3,4,5,6) then event=1; drop days_passed; run;
But without seeing some actual values it might be hard to guess what exact code you need.
@Lordy wrote:
Did not work - code still flagging sunday
Provide actual example starting data and the desired output for a few records.
We're kind of flying blind as to your actual values and what the actual result should look like. We can't even be sure whether your "date" actually is a date or not.
If the goal is to check by calendar week then try using INTNX() function with week interval.
data want;
set have ;
by SSN date;
week=intnx('week',date,0,'b');
if first.ssn then n_orange=0;
if week ne lag(week) then n_orange=0;
n_orange+1;
event=(n_orange>1) ;
run;
Worked!
Thanks a lot
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.