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

Hi,

 

I was wondering if anybody could help me with a problem I encountered with if, then, else statements.

 

data sasdata.new;
set sasdata.auditvars3;
if A1=11001101 then AI2=.;
else AI2=A1;
run;

 

How would I do it if I needed A1=1001101 and A1=1100010 to both equal "." when put into the new A2 column.

 

thanks 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

The IF clause tests a logical condition, which is not restricted to a simple condition like

if A1=11001101 then ...

It can be a compound condition constructed with AND's, OR's and parentheses, such as:

 

if (A1=11001101 or A1=1100010) then ...

 

You also can have more than a single ELSE following an initial IF:

if a1=11001101 then AI2=.
else if a1=1100010 then AI2=.;
else AI2=A1;

 

And I should have added that this is a "flow-control" capability of every computer programming language that I have seen - not just SAS.

--------------------------
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

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

The IF clause tests a logical condition, which is not restricted to a simple condition like

if A1=11001101 then ...

It can be a compound condition constructed with AND's, OR's and parentheses, such as:

 

if (A1=11001101 or A1=1100010) then ...

 

You also can have more than a single ELSE following an initial IF:

if a1=11001101 then AI2=.
else if a1=1100010 then AI2=.;
else AI2=A1;

 

And I should have added that this is a "flow-control" capability of every computer programming language that I have seen - not just SAS.

--------------------------
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

--------------------------
ballardw
Super User

And if you have more values the IN operator is available to avoid writing a bunch of "OR" clauses;

The IN operator tests a single variable against a list of explicit values:

 

if A1 in (11001101 1100010) then

is equivalent to

if (A1=11001101 or A1=1100010) then

 

If you later need to add a third or fourth or … item just place them inside the ()  with the existing values.

There are also some short cuts for dealing with sequential integer values:  var in (3:10 27.4) would test for all the integers from 3 through 10 and the value 27.4.

Character values must be inside quotes and lists do not work but this potentially saves a great many OR's in the code.

 

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