DATA Step, Macro, Functions and more

If 0 then set statement

Accepted Solution Solved
Reply
Occasional Contributor V_R
Occasional Contributor
Posts: 5
Accepted Solution

If 0 then set statement

Hi All,

 

I have came across following expression.

 

IF 0 then set abc;

how does this if 0 then set statement works

request you to explain with examples


Accepted Solutions
Solution
‎04-18-2018 12:33 AM
Super User
Posts: 6,632

Re: If 0 then set statement

Well, 0 is false so the SET statement never executes.  You would get the same result by coding:

 

if 5=4 then set abc;

 

However, the statement still has an effect over the results of the DATA step.  SAS executes DATA steps in two phases.  During the first phase, it looks through all the statements, checks for syntax errors, and sets up storage space for all the variables that the DATA step will process.  Just the presence of the SET statement (even if it never executes) forces SAS to define all the variables that are part of ABC.  All the variable names, lengths, types, etc. will become part of the new data set being constructed.

View solution in original post


All Replies
Solution
‎04-18-2018 12:33 AM
Super User
Posts: 6,632

Re: If 0 then set statement

Well, 0 is false so the SET statement never executes.  You would get the same result by coding:

 

if 5=4 then set abc;

 

However, the statement still has an effect over the results of the DATA step.  SAS executes DATA steps in two phases.  During the first phase, it looks through all the statements, checks for syntax errors, and sets up storage space for all the variables that the DATA step will process.  Just the presence of the SET statement (even if it never executes) forces SAS to define all the variables that are part of ABC.  All the variable names, lengths, types, etc. will become part of the new data set being constructed.

Occasional Contributor V_R
Occasional Contributor
Posts: 5

Re: If 0 then set statement

Posted in reply to Astounding

Thanks a lot for quick response

Moderator
Posts: 316

Re: If 0 then set statement

The "if 0 then set" logic ensures that the code compiles (prepares variables, etc), but it never executes.

 

It's a very effective way of ensuring the structure of your target data set mimics the structure of your source data set, even if your source data set structure changes over time.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 289 views
  • 1 like
  • 3 in conversation