BookmarkSubscribeRSS Feed
helloSAS
Obsidian | Level 7

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

3 REPLIES 3
ballardw
Super User

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.

Tom
Super User Tom
Super User

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;

cau83
Pyrite | Level 9

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 705 views
  • 1 like
  • 4 in conversation