BookmarkSubscribeRSS Feed
aklr86
Calcite | Level 5

This seems like a really basic question. When creating a variable within a data step, is it possible to refer to that variable within the same data step? When I try to run the following code, I get the error "ERROR: Variable fthursDiag is not on file MAVEN.dd_aow_events". There are ways around this of course, but it is something I've run into a few times and have yet to find a solution for.

data aow1;

     set maven.dd_aow_events;

*-- convert dates;

array date birth_date disease_status_date investigation_status_date diagnosis_date onset_date

    event_create_date event_modification_date;

do over date;

    date=datepart(date);

end;

format birth_date disease_status_date investigation_status_date diagnosis_date onset_date

    event_create_date event_modification_date mmddyy10.;

*-- create variable for first THURSDAY after diagnosis date;

fthursDiag=diagnosis_date;

     if  weekday(fthursDiag)=1 then fthursDiag = fthursDiag+4;

     if  weekday(fthursDiag)=2 then fthursDiag = fthursDiag+3;

     if  weekday(fthursDiag)=3 then fthursDiag = fthursDiag+2;

     if  weekday(fthursDiag)=4 then fthursDiag = fthursDiag+1;

     if  weekday(fthursDiag)=5 then fthursDiag = fthursDiag+0;

     if  weekday(fthursDiag)=6 then fthursDiag = fthursDiag+6;

     if  weekday(fthursDiag)=7 then fthursDiag = fthursDiag+5;

format birth_date diagnosis_date fthursDiag mmddyy10.;

*-- try to restrict data using the newly calculated variable;

where fthursDiag <= date();

run;

4 REPLIES 4
art297
Opal | Level 21

From the documentation (http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000202951.htm 😞

You cannot use variables that are created within the DATA step (for example, FIRST.variable, LAST.variable, _N_, or variables that are created in assignment statements) in a WHERE expression because the WHERE statement is executed before the SAS System brings observations into the DATA or PROC step. When WHERE expressions contain comparisons, the unformatted values of variables are compared.

Of course, you could get around that by using and if statement in place of your current where statement.  However, in looking at your code, shouldn't your current if statements really be a set of if, then, else statements as well?

aklr86
Calcite | Level 5

"However, in looking at your code, shouldn't your current if statements really be a set of if, then, else statements as well?" I don't understand this. Could you be more explicit?

art297
Opal | Level 21

If you don't use the construction that recommended, or precede the 2nd thru 7th if statements with an 'else', the variable will be re-evaluated 7 times and each time using the value that resulted from the previous statement.

ballardw
Super User

If you want to restrict the OUTPUT data to those records with the stated condition use:

IF  fthursDiag <= date();

If you only want to make ONE change to the fthursDiag variable try:

Select (weekday(fthursDiag));

     when (1) fthursDiag = fthursDiag+4;

     when (2) fthursDiag = fthursDiag+3;

     when (3) fthursDiag = fthursDiag+2;

     when (4) fthursDiag = fthursDiag+1;

     when (5) fthursDiag = fthursDiag+0;

     when (6) fthursDiag = fthursDiag+6;

     when (7) fthursDiag = fthursDiag+5;

     otherwise;

end;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 863 views
  • 1 like
  • 3 in conversation