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;
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.