BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
EinarRoed
Pyrite | Level 9

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

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.

svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10
If you are using SAS 9.4, you can use code like this in open code, but you need to use %if/%if%then syntax when you are using conditional logic with macro variables.
FreelanceReinh
Jade | Level 19

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.

mkeintz
PROC Star

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.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
EinarRoed
Pyrite | Level 9
Thanks to everyone for great explanations. The code runs as intended now. 🙂

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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
  • 5 replies
  • 632 views
  • 4 likes
  • 5 in conversation