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
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 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.
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.