BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rajeshm
Quartz | Level 8

 

in the below code if condition should not be executed but how i was able to print n varible  value? 

 

data _null_;
if -2 then set sashelp.class nobs=n;
call symput ('nrows',n);
run;

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

Two reasons:

  • Actually, your SET statement IS executed. SAS interpretes anything not zero or missing as a TRUE boolean value, so "if -2" is equivalent to "if 1"
  • The NOBS option sets the variable at compile time, as @PeterClemmensen already remarked.

The last reason is why you can get away with stuff like this:

data test;
  do i=1 to n;
    set x.data1 nobs=n;
    output;
    end;
run;

So the N variable is defined and set before the loop which contains the SET statement begins execution.

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

All SET statements are always executed at compile time to to build the pdv, the IF -statement prevents execution at runtime, so that no observations are read.

PeterClemmensen
Tourmaline | Level 20

Because the n variable in nobs=n is set at compile time

s_lassen
Meteorite | Level 14

Two reasons:

  • Actually, your SET statement IS executed. SAS interpretes anything not zero or missing as a TRUE boolean value, so "if -2" is equivalent to "if 1"
  • The NOBS option sets the variable at compile time, as @PeterClemmensen already remarked.

The last reason is why you can get away with stuff like this:

data test;
  do i=1 to n;
    set x.data1 nobs=n;
    output;
    end;
run;

So the N variable is defined and set before the loop which contains the SET statement begins execution.

CFC_SAS_Communities_400x225.jpgCFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2426 views
  • 3 likes
  • 4 in conversation