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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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