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

Hi, i have been using c/c++/c#  as my primary language before I jumped into SAS.

A 'and' operator in data statement confuses me.

First, the entire program look like this:

data work.empsalary;
set work.people (in = inemp)
work.money(in = insal);
if insal and inemp;
run;

what shall I interpret 'and' in line 4?

To rephrase this in c grammar, which is correct?

1) if (insal & inemp)

2) if (insal && inemp)

3) if (insal = 1 && inemp =1)

 

Needing your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@jimmychoi wrote:

Hi, i have been using c/c++/c#  as my primary language before I jumped into SAS.

A 'and' operator in data statement confuses me.

First, the entire program look like this:

data work.empsalary;
set work.people (in = inemp)
work.money(in = insal);
if insal and inemp;
run;

what shall I interpret 'and' in line 4?

To rephrase this in c grammar, which is correct?

1) if (insal & inemp)

2) if (insal && inemp)

3) if (insal = 1 && inemp =1)

 

Needing your help!


There is nothing different about AND in SAS than in any other programming language. It is a binary operator that evaluates to TRUE when both of its two inputs are true.

Note that In SAS any number that is not zero or missing is TRUE.

 

You can use the character & as a short-hand for the AND operator (especially if you want to confuse others that might have to use your program).  So (1) is exactly the same as the original statement.

 

It doesn' t really make much sense to write something like A AND AND B and SAS will flag that as an error if you do.  But if you write two &'s next to each other as you have in (2) and (3) then the macro processor will see that as a macro trigger and convert the double &'s into a single &.  So (2)  and (1) are generating exactly the same statement.  

106  %put "if (insal && inemp)";
"if (insal & inemp)"

 In (3) you have changed the two conditions slightly. You are now testing whether INSAL and INEMP are exactly 1 instead of just testing whether they are TRUE.  But since they are being set by the IN= dataset option they will only ever have a value of 1 or 0 so the RESULT of (3) will be exactly the same as the result of (1) and (2).

 

Of course the real questions you should be asking yourself is:

 

Why does that line cause EMPSALARY to have zero observations no matter what is in PEOPLE and SALARY?

 

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

Your program reads dataset people, then reads dataset money, and outputs... nothing.

 

The reason is that variables inemp and insal take value TRUE when the data was read from dataset people or money, respectively. They cannot be true at the same time.

PG
Tom
Super User Tom
Super User

@jimmychoi wrote:

Hi, i have been using c/c++/c#  as my primary language before I jumped into SAS.

A 'and' operator in data statement confuses me.

First, the entire program look like this:

data work.empsalary;
set work.people (in = inemp)
work.money(in = insal);
if insal and inemp;
run;

what shall I interpret 'and' in line 4?

To rephrase this in c grammar, which is correct?

1) if (insal & inemp)

2) if (insal && inemp)

3) if (insal = 1 && inemp =1)

 

Needing your help!


There is nothing different about AND in SAS than in any other programming language. It is a binary operator that evaluates to TRUE when both of its two inputs are true.

Note that In SAS any number that is not zero or missing is TRUE.

 

You can use the character & as a short-hand for the AND operator (especially if you want to confuse others that might have to use your program).  So (1) is exactly the same as the original statement.

 

It doesn' t really make much sense to write something like A AND AND B and SAS will flag that as an error if you do.  But if you write two &'s next to each other as you have in (2) and (3) then the macro processor will see that as a macro trigger and convert the double &'s into a single &.  So (2)  and (1) are generating exactly the same statement.  

106  %put "if (insal && inemp)";
"if (insal & inemp)"

 In (3) you have changed the two conditions slightly. You are now testing whether INSAL and INEMP are exactly 1 instead of just testing whether they are TRUE.  But since they are being set by the IN= dataset option they will only ever have a value of 1 or 0 so the RESULT of (3) will be exactly the same as the result of (1) and (2).

 

Of course the real questions you should be asking yourself is:

 

Why does that line cause EMPSALARY to have zero observations no matter what is in PEOPLE and SALARY?

 

hashman
Ammonite | Level 13

You need to understand that when two (or more) data sets are listed in the SET statement, they are read in as one single file consisting of the data sets listed in the statement as if they were concatenated. The data set option IN=<variable> supplied for any of those data sets is used to judge whether the current record comes from that data set. SAS sets <variable> to either 1 or 0. If, for a particular data set, <variable>=1, the record comes from this data set; if <variable>=0, it does not.

 

In SAS, a numeric variable with a value that is anything but 0 or missing (hence including value 1) is evaluated to 1; otherwise, it is evaluated to 0. In SAS, the Boolean value of 1=True and that of 0=False. Thus, the statement:

 

if <expression> 

 

evaluates <expression> and sets the result IF faces to 1 or 0 depending on whether <expression> resolves to [zero or missing] or [something else]. Thus, if you have a statement:

 

set A (in=from_a) B (in=from_b) ;  

 

from_a is set to 1 for all records from A, and to 0 from all records from B. Conversely, from_b is set to 1 for all records from B and to 0 for all records from A. Therefore, the expression:

 

from_a and from_b 

 

always evaluates to False since the only possibilities are (from_a=1,from_b=0) and (from_a=0, from_b=1). Your statement:

 

if insal and inemp ;

 

is what is called the "subsetting IF". If it's true, the statements between it and the bottom of the step - including the implicit OUTPUT statement - are executed; otherwise, program control moves back to the SET statement to read the next record. Since in your case the condition is never true, the implicit OUTPUT statement is never executed, which is why no records are written to the output data set EMPSALARY.    

 

I know, from the standpoint of C/C++/C# all these SAS conventions and implied things may look odd. But once you've understood their meaning and gotten used to them, they become natural and save you a whole slew of coding. This is precisely the reason why SAS has put so much automation in its language.

 

Paul D.   

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 1413 views
  • 2 likes
  • 4 in conversation