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

Hello again,

 

I was wondering if anyone can help point me in the direction of what code I should try to make what I'm hoping for work. 

 

Basically, I have observation in Person-Months for a span of 5 years (i.e. ID1-1... ID1-60, ID2-1... ID2-60, etc.).  I have a variable that indicates an event of interest during the month, and I was hoping to create a new dataset that has data only from 2 years after the first event.  I've tried using a IF/THEN with a KEEP statement, but am having trouble identifying the two years post event.

 

For reference, my dataset has 3 variables: ID, MonthYear, and Event (binary)

 

Does anyone have an idea?

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

If your data are already in id, monthyear order (otherwise if you sort it to be in that order), then I think that the following will do what you want:

 

data want;
  set have;
  by id;
  if first.id then counter=0;
  if counter eq 0 and event eq 1 then do;
    counter=1;
    output;
  end;
  else if 1<=counter<=24 then do;
    counter+1;
    output;
  end;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post

2 REPLIES 2
art297
Opal | Level 21

If your data are already in id, monthyear order (otherwise if you sort it to be in that order), then I think that the following will do what you want:

 

data want;
  set have;
  by id;
  if first.id then counter=0;
  if counter eq 0 and event eq 1 then do;
    counter=1;
    output;
  end;
  else if 1<=counter<=24 then do;
    counter+1;
    output;
  end;
run;

Art, CEO, AnalystFinder.com

 

cyoung
Fluorite | Level 6

That worked perfectly!  Thank you so much!!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 2 replies
  • 875 views
  • 1 like
  • 2 in conversation