Basic data step question

Reply
Occasional Contributor
Posts: 5

Basic data step question

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;

PROC Star
Posts: 7,363

Re: Basic data step question

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?

Occasional Contributor
Posts: 5

Re: Basic data step question

"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?

PROC Star
Posts: 7,363

Re: Basic data step question

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.

Super User
Posts: 10,514

Re: Basic data step question

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;

Ask a Question
Discussion stats
  • 4 replies
  • 211 views
  • 1 like
  • 3 in conversation