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

I need help with a problem I'm trying to code. I want to assume that a specific time is limited to 30 years. I then want to say if an event happened within those 30 years, a person is receives a flag showing they met requirements for the next 10 years. I plan to do this by indicating at what year the event happened (with a variable called event_year) and then I want a group of flags (met_req0-metreq_29) to be turned from 0 to 1 to indicate the 10 year period they met their requirements. I tried the following code, but 1) I think it's clunky and 2) I get an error because later years (e.g., event_year = 20), it tries to flag variables that don't exist. For example metreq_29 is as far as my array variables go and this code attempts to manipulate variable metreq_20 - metreq_30, the latter of which doesn't exist. Any ideas on the best way to do this?

 

data combine;

set combine;

array metreq (*) metreq_0-metreq_29;

do i = 0 to 29;

if event_year = i then do;

iscert[i] = 1;

iscert[i+1] = 1;

iscert[i+2] = 1;

iscert[i+3] = 1;

iscert[i+4] = 1;

iscert[i+5] = 1;

iscert[i+6] = 1;

iscert[i+7] = 1;

iscert[i+8] = 1;

iscert[i+9] = 1;

iscert[i+10] = 1;

end;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

You use an array without definition (iscert) so it isn't quite clear what you are attempting in the code.

I am guessing this may give you a start.

data combine;
input event_year;
datalines;
0
3
18
25
;
run;

data newcombine;
   set combine;
   array metreq (0:29) metreq_0-metreq_29 ;
   /*initialize array to 0*/
   do i=0 to 29;
      metreq[i] = 0;
   end;
   do i = event_year to (min(29, event_year+10));
      metreq[i] = 1;
   end;
run;

The use of coding structures like:

 

data combine;

    set combine;

<any other code>

run;

has a strong likelihood of causing either lost data or erroneous values as you rerun the same code with modifications. Each run completely replaces the existing data set.

Run the second data step below 3 times without running the first data step and see what happens.

data junk;
   x= 3;
run;

data junk;
   set junk;
   x= x-1;
   if x;
run;

Any modification to an existing variable is dangerous. And you may end up with many variables with intention.

 

View solution in original post

1 REPLY 1
ballardw
Super User

You use an array without definition (iscert) so it isn't quite clear what you are attempting in the code.

I am guessing this may give you a start.

data combine;
input event_year;
datalines;
0
3
18
25
;
run;

data newcombine;
   set combine;
   array metreq (0:29) metreq_0-metreq_29 ;
   /*initialize array to 0*/
   do i=0 to 29;
      metreq[i] = 0;
   end;
   do i = event_year to (min(29, event_year+10));
      metreq[i] = 1;
   end;
run;

The use of coding structures like:

 

data combine;

    set combine;

<any other code>

run;

has a strong likelihood of causing either lost data or erroneous values as you rerun the same code with modifications. Each run completely replaces the existing data set.

Run the second data step below 3 times without running the first data step and see what happens.

data junk;
   x= 3;
run;

data junk;
   set junk;
   x= x-1;
   if x;
run;

Any modification to an existing variable is dangerous. And you may end up with many variables with intention.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 reply
  • 648 views
  • 0 likes
  • 2 in conversation