BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Nietzsche
Lapis Lazuli | Level 10

Hi I am currently on page 133 of the official Specialist Prep Guide. 

 

Two things I wish to confirm with the community because book does not explain it regarding to IF/THEN (maybe in later chapters). 

First:  is IF first.dept the same thing as if first.dept=1 or if first.dept=TRUE? SAS assumes that user mean true (like in python)?

 

Secondly: if nothing is stated after the IF statement like IF last.dept; it just means end everything the IF statement? 

 

 

 

Nietzsche_0-1667767303052.png

 

SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Nietzsche wrote:

Hi I am currently on page 133 of the official Specialist Prep Guide. 

 

Two things I wish to confirm with the community because book does not explain it regarding to IF/THEN (maybe in later chapters). 

First:  is IF first.dept the same thing as if first.dept=1 or if first.dept=TRUE? SAS assumes that user mean true (like in python)?

Yes.  Do you know WHY ?   Do you know what result SAS returns from the evaluation of a boolean expression (such as first.dept=1) ?   Do you know what truth value SAS uses when given a number like 1 to interpret as a boolean?

 

Hint remember that SAS has only two types of variables.  Fixed length character strings and floating point numbers.

Spoiler
The result of a boolean expression is either 1 (true) or 0 (false).

Zero or missing is considered FALSE.  Any other value is considered TRUE.

 

Secondly: if nothing is stated after the IF statement like IF last.dept; it just means end everything the IF statement? 

Sort of.  They are actually two different statements.  Without the THEN you have a SUBSETTING IF statement instead.  Basically if the condition is not true this particular iteration of the data step stops immediately.  So statements after that are not executed. Note this includes any implied OUTPUT statement that might be executed when the iteration reaches the end of the data step code.

 

View solution in original post

6 REPLIES 6
yabwon
Onyx | Level 15

Hi,

 

Case 1, SAS treats zero (0) and missing numeric values (.) as False, all other (numbers) are treated as True, see: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p1n8bsqqd03xppn17pgvjpjlbhhs.htm#n09r5v4...

 

Case 2, it is the IF-subsetting, see here: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p1cxl8ifdt8u0gn12wqbji8o5fq1.htm

 

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Kurt_Bremser
Super User

FIRST. and LAST. variables are automatic variables created for every variable in the BY statement. They will not be included in resulting datasets.

They are Boolean variables and can only have the values 0 (false) or 1 (true).

The first IF is a "normal" IF with a THEN branch which will be executed depending on the result of the condition.

The second IF is a subsetting IF. If the condition results in false, the rest of the data step statements are skipped and the next data step iteration starts; no implicit output is performed.

Tom
Super User Tom
Super User

@Nietzsche wrote:

Hi I am currently on page 133 of the official Specialist Prep Guide. 

 

Two things I wish to confirm with the community because book does not explain it regarding to IF/THEN (maybe in later chapters). 

First:  is IF first.dept the same thing as if first.dept=1 or if first.dept=TRUE? SAS assumes that user mean true (like in python)?

Yes.  Do you know WHY ?   Do you know what result SAS returns from the evaluation of a boolean expression (such as first.dept=1) ?   Do you know what truth value SAS uses when given a number like 1 to interpret as a boolean?

 

Hint remember that SAS has only two types of variables.  Fixed length character strings and floating point numbers.

Spoiler
The result of a boolean expression is either 1 (true) or 0 (false).

Zero or missing is considered FALSE.  Any other value is considered TRUE.

 

Secondly: if nothing is stated after the IF statement like IF last.dept; it just means end everything the IF statement? 

Sort of.  They are actually two different statements.  Without the THEN you have a SUBSETTING IF statement instead.  Basically if the condition is not true this particular iteration of the data step stops immediately.  So statements after that are not executed. Note this includes any implied OUTPUT statement that might be executed when the iteration reaches the end of the data step code.

 

Nietzsche
Lapis Lazuli | Level 10

so let me get this straight.

 

The intention of that subsetting statement without anything code if the IF statement is simply to control the execution (ie, writing to output) only if the subsetting IF is TRUE?

SAS Base Programming (2022 Dec), Preparing for SAS Advanced Programming (Cancelled).
Tom
Super User Tom
Super User

@Nietzsche wrote:

so let me get this straight.

 

The intention of that subsetting statement without anything code if the IF statement is simply to control the execution (ie, writing to output) only if the subsetting IF is TRUE?


That is an oversimplification, but in a simple data step you can think of that way.

For example:

data males;
  set sashelp.class ;
  if sex='M';
run;

So that type of usage is what led to the SUBSETTING IF name.

 

Play around and see how it works.  

For example run this data step and check the log.

data males;
   set sashelp.class;
   put  'Before the IF' _n_ = sex= ;
   if sex='M';
   put 'After the IF' _n_= sex= ;
run;

Log:

1419  data males;
1420     set sashelp.class;
1421     put  'Before the IF' _n_ = sex= ;
1422     if sex='M';
1423     put 'After the IF' _n_= sex= ;
1424  run;

Before the IF_N_=1 Sex=M
After the IF_N_=1 Sex=M
Before the IF_N_=2 Sex=F
Before the IF_N_=3 Sex=F
Before the IF_N_=4 Sex=F
Before the IF_N_=5 Sex=M
After the IF_N_=5 Sex=M
Before the IF_N_=6 Sex=M
After the IF_N_=6 Sex=M
Before the IF_N_=7 Sex=F
Before the IF_N_=8 Sex=F
Before the IF_N_=9 Sex=M
After the IF_N_=9 Sex=M
Before the IF_N_=10 Sex=M
After the IF_N_=10 Sex=M
Before the IF_N_=11 Sex=F
Before the IF_N_=12 Sex=F
Before the IF_N_=13 Sex=F
Before the IF_N_=14 Sex=F
Before the IF_N_=15 Sex=M
After the IF_N_=15 Sex=M
Before the IF_N_=16 Sex=M
After the IF_N_=16 Sex=M
Before the IF_N_=17 Sex=M
After the IF_N_=17 Sex=M
Before the IF_N_=18 Sex=M
After the IF_N_=18 Sex=M
Before the IF_N_=19 Sex=M
After the IF_N_=19 Sex=M
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.MALES has 10 observations and 5 variables.

So you can see the second PUT statement only runs on observations where SEX was equal to 'M'.

Sajid01
Meteorite | Level 14

As  mentioned in your post the data set company.usa is being sorted using the by variable dept  and the sorted values are in work. temp.
This means all rows having the same value of the variable dept are contiguous.
For clarity consider that the rows for the variable dept="HR" are being processed in the data step.
First question :
First:  is IF first.dept the same thing as if first.dept=1 or if first.dept=TRUE? SAS assumes that user mean true .
This statement essentially answers the question : Is this the first value of the department HR?

Second Question :
if nothing is stated after the IF statement like IF last.dept; it just means end everything the IF statement? 
This answers the question : Is the value of the variable  dept is the last value for dept="HR" ?
If it is indeed the last value then last,dept becomes true.
In this question the values of payroll is being calculated for each department. So when the last value of variable dept is reached (last,dept is true)  then the observation or result  is output to the dataset budget. 
Thus if the source dataset work.temp has two dept's.  then the output budget will have two rows.|

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 6 replies
  • 859 views
  • 6 likes
  • 5 in conversation