While %IF in open code also allows to use %ELSE, it does not work in all circumstances:
%let control=Y;
data
%if &control = X
%then %do;
work.check
%end;
%else %do;
work.test
%end;
;
x = 1;
run;
data
%if &control = X
%then %do;
work.check
%end;
%if &control ne X
%then %do;
work.test
%end;
;
x = 1;
run;
The first data step will fail with a component object failure (but not a macro ERROR message!), while the second works.
Tested on SAS On Demand, and also 9.4TS1M7 on AIX
Here's your code "fixed" as suggested in the Problem Note
131 %let control=Y;
132 options mprint symbolgen mlogic ;
133
134 %if &control = X
SYMBOLGEN: Macro variable CONTROL resolves to Y
135 %then %do;
136 data work.check ;
137 %end;
138 %else %do;
139 data work.test ;
140 %end;
141 ;
142 x = 1;
143 run;
NOTE: The data set WORK.TEST has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.01 seconds
So open code %if can only be used to add statement, or at least statement endings?
This works for me on M7 Win:
%let control=Y;
options mprint symbolgen mlogic ;
data A
%if &control = X %then %do;
B ;
%end;
%else %do;
C ;
%end;
X = 1;
run;
data A
%if &control = X %then %do;
B
%end;
%else %do;
C
%end;
D ;
X = 1;
run;
data A
%if &control = X %then %do;
B
%end;
%else %do;
C
%end;
;
X = 1;
run;
25 %let control=Y;
26 options mprint symbolgen mlogic ;
27 data A
SYMBOLGEN: Macro variable CONTROL resolves to Y
28 %if &control = X %then %do;
29 B ;
30 %end;
31 %else %do;
32 C ;
33 %end;
34 X = 1;
35 run;
NOTE: Compression was disabled for data set WORK.A because compression overhead would increase the size of the data set.
NOTE: Compression was disabled for data set WORK.C because compression overhead would increase the size of the data set.
NOTE: The data set WORK.A has 1 observations and 1 variables.
NOTE: The data set WORK.C has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
36
37 data A
SYMBOLGEN: Macro variable CONTROL resolves to Y
38 %if &control = X %then %do;
39 B
40 %end;
41 %else %do;
42 C
43 %end;
44 D ;
2 The SAS System 13:45 Thursday, November 25, 2021
45 X = 1;
46 run;
NOTE: Compression was disabled for data set WORK.A because compression overhead would increase the size of the data set.
NOTE: Compression was disabled for data set WORK.C because compression overhead would increase the size of the data set.
NOTE: Compression was disabled for data set WORK.D because compression overhead would increase the size of the data set.
NOTE: The data set WORK.A has 1 observations and 1 variables.
NOTE: The data set WORK.C has 1 observations and 1 variables.
NOTE: The data set WORK.D has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.04 seconds
47
48 data A
SYMBOLGEN: Macro variable CONTROL resolves to Y
49 %if &control = X %then %do;
50 B
51 %end;
52 %else %do;
53 C
54 %end;
55 ;
56 X = 1;
57 run;
NOTE: Compression was disabled for data set WORK.A because compression overhead would increase the size of the data set.
NOTE: Compression was disabled for data set WORK.C because compression overhead would increase the size of the data set.
NOTE: The data set WORK.A has 1 observations and 1 variables.
NOTE: The data set WORK.C has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
Well, it's a workaround, but clearly not a solution. This is a bug and needs fixing.
The fact that this was reported with M5 and is not yet fixed is somewhat disturbing. After all, we always tend to brag about SAS' software quality to beginners and outsiders.
Kurt,
I got no problem. Maybe it is sas version problem. Mine is stand-alone PC version SAS 9.4M7 .
NOTE: SAS initialization used: real time 1.76 seconds cpu time 1.35 seconds 1 %let control=Y; 2 3 data 4 %if &control = X 5 %then %do; 6 work.check 7 %end; 8 %else %do; 9 work.test 10 %end; 11 ; 12 x = 1; 13 run; NOTE: The data set WORK.TEST has 1 observations and 1 variables. NOTE: DATA statement used (Total process time): real time 0.05 seconds cpu time 0.01 seconds 14 15 data 16 %if &control = X 17 %then %do; 18 work.check 19 %end; 20 %if &control ne X 21 %then %do; 22 work.test 23 %end; 24 ; 25 x = 1; 26 run; NOTE: The data set WORK.TEST has 1 observations and 1 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.01 seconds
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.