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.

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 701 views
  • 0 likes
  • 3 in conversation