DATA Step, Macro, Functions and more

data step

Reply
Frequent Contributor
Posts: 87

data step

For some reason, below if statment does not resolve properly. though dy is not equal to 2 it would only use the first set statement. Can you explain whats going wrong?

%let lsm = 10;
%let lsy = 2013;

data x;
dy = day(today());
if dy = 2 then do;
set air&lsy&lsm;
end;
else do;
set sashelp.air sashelp.cars;
end;
run;

************log************************

17         %let lsm = 10;
18         %let lsy = 2013;
19        
20         data x;
21         dy = day(today());
22         if dy = 2 then do;
23          set air&lsy&lsm;
ERROR: File WORK.AIR201310.DATA does not exist.
24          end;
25         else do;
26         set sashelp.air sashelp.cars;
27         end;
28         run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.X may be incomplete.  When this step was stopped there were 0 observations and 18 variables.
WARNING: Data set WORK.X was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds

Super User
Posts: 10,460

Re: data step

I sugest that you try something like:

If 3 = 2 then do;

I suspect the problem has to do with sas expecting any referenced data set to exist even if not executed.

An alternative may be to wrap this behavior in a macro so the code doesn't appear to the data step at all.

%macro dummy();

     %let dy = %sysfunc(day(%sysfunc(today())));

     data x;

     %if &dy = 2 %then %do;

          set set air&lsy&lsm;

     %end;

     %else %do;

          <your other set statment>

     %end;

    run;

%mend;

%dummy;

This approach would allow setting the macro variables as parameters to the macro a well.

Super User
Super User
Posts: 6,495

Re: data step

SAS processes the SET statements during the compilation part of the DATA STEP.  That is how it knows what variables are in the input data sets.  So even though during the execution phase the dataset will not actually be read it will be referenced to find the variable names.  Even if your code worked it has a potential problem if it started to execute right at midnight. The first few iterations of the data step could end up taking data from a different dataset than the iterations that execute after midnight when the value of DY will change.

Use macro logic to generate the proper dataset name.

%let lsm = 10;

%let lsy = 2013;

%let dslist = %sysfunc(ifc(1=%sysfunc(today(),day.),air&lsy&lsm,sashelp.air sashelp.cars));

data x;

  set &dslist;

run;

Contributor
Posts: 69

Re: data step

I don't know if your question was a general one or if that program exists in the context of something else. The following would get rid of the error but may not be optimal depending on what else you're doing.

Recently I've been creating empty datasets that I use as a base on which to append when looping a macro repeatedly and creating similar outputs that I want to be in one dataset, but do not want to have to write them all out into one set statement.

So, before that data step you could have:

data WORK.AIR201310;

     attrib VARIABLES AND ATTRIBUTES HERE;

     stop;

run;

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