BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lordy
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

5 REPLIES 5
ballardw
Super User

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
Obsidian | Level 7
Did not work - code still flagging sunday
ballardw
Super User

@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.

Tom
Super User Tom
Super User

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;
Lordy
Obsidian | Level 7

Worked!

Thanks a lot

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1082 views
  • 0 likes
  • 3 in conversation