I feel like I'm going insane trying to figure something out. Hoping that someone can explain the logic for me. 😩
We've got a data step with this code:
if &create_testvar_flg. = 1 then do;
attrib testvar_nr format=11.0;
testvar_nr = input(testvar.,best.);
end;
When &create_testvar_flg is set to 1, this runs as intended. The variable is created and filled with values.
However if &create_testvar_flg isn't 1, then the variable is still created. It just isn't loaded. And the log does say "create_testvar_flg resolves to 0".
I've tried removing the that whole part of the code, and can confirm that the new variable isn't created at all then. So there's definitely something iffy about the IF-statement above (pun intended). Is there any reason why it would run the attrib-line even though &create_testvar_flg=0? How can that be prevented?
This is part of reusable code used to load multiple tables. We want the new attribute to be created conditionally, only for some of them.
I am not sure what you are talking about but you have code inside the IF statement that is NOT EXECUTABLE. So it is NOT impacted by whether or not the IF condition is TRUE.
So you are running this code:
attrib testvar_nr format=11.0;
if &create_testvar_flg. = 1 then do;
testvar_nr = input(testvar,32.);
end;
If you do not want to generate the ATTRIB statement then use a macro %IF to prevent it from being generated.
%if &create_testvar_flg. = 1 %then %do;
attrib testvar_nr format=11.0;
testvar_nr = input(testvar,32.);
%end;
PS You cannot have a variable with a period on the end of its name. There is no BEST informat, the concept makes no sense. If you use BEST as an informat then SAS just defaults to the normal numeric informat.
I am not sure what you are talking about but you have code inside the IF statement that is NOT EXECUTABLE. So it is NOT impacted by whether or not the IF condition is TRUE.
So you are running this code:
attrib testvar_nr format=11.0;
if &create_testvar_flg. = 1 then do;
testvar_nr = input(testvar,32.);
end;
If you do not want to generate the ATTRIB statement then use a macro %IF to prevent it from being generated.
%if &create_testvar_flg. = 1 %then %do;
attrib testvar_nr format=11.0;
testvar_nr = input(testvar,32.);
%end;
PS You cannot have a variable with a period on the end of its name. There is no BEST informat, the concept makes no sense. If you use BEST as an informat then SAS just defaults to the normal numeric informat.
Hello @EinarRoed,
The structure of a dataset created with the usual (implicit or explicit) OUTPUT statement of a DATA step cannot depend on IF conditions because these are evaluated during execution whereas the dataset structure is determined during compilation of the DATA step. This is also the reason why the ATTRIB statement is not executable, but declarative (see "Type" in the statement documentation, same for LENGTH, FORMAT, etc.). The %IF-%THEN macro statement (as suggested by Tom) is processed before the DATA step is compiled. Thus the result of the compilation (and hence the dataset structure) will depend on the %IF condition, as desired.
The SAS data step language can conditionally change values of a variable, but it does not offer a way to conditionally include/exclude a variable from the resulting data set(s).
And consider the underlying principle - the sas complier, which transforms your code into executable statements, must make provision for the "conditional" variable, because the compiler must complete its work before any data is processed. The compiler has no way of knowing the data values. Consequently, it must define the complete list of variables intended for the output data set prior to processing even one observation.
Now, there is a way to use hash object programming within a SAS data step to do what you want, but it involves code that is "masked" from the regular data step compiler.
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.