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
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.
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;
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;
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: