data test; set test2; if card_type = 'F' then product = 'EC'; else if customer = 'C' then product = 'bc_card'; else if card_type in ('dg', 'dp', 'bg') then product = 'others'; run;
does these statement make sense?
else if does not have to focus on the same variable?
here was referring to card_type but suddenly changed to customer
does SAS allow this?
Yes it will work.
if <a condition> then
do;
<code to run when true>
end;
else
do;
<code to run when false>
end;
Bart
@HeatherNewton wrote:
does these statement make sense?
Since we don't know the subject matter or the purpose of the code, this is impossible for us to answer.
@HeatherNewton wrote:
data test; set test2; if card_type = 'F' then product = 'EC'; else if customer = 'C' then product = 'bc_card'; else if card_type in ('dg', 'dp', 'bg') then product = 'others'; run;does these statement make sense?
else if does not have to focus on the same variable?
here was referring to card_type but suddenly changed to customer
does SAS allow this?
Correct: the "else if" condition need not have the same variable as its preceding "if" or preceding "else if". Not only does "SAS allow this" but most programing languages would allow it.
But the restriction you are concerned about is true with certain forms of the SELECT group. such as:
select (color);
when ('red') cx=1;
when ('blue') cx=2;
when ('green') cx=3;
when ('yellow') cx=4;
otherwise cx=0;
end;
The "when" statements are effectively "else if" tests - i.e. once a when condition is true, all subsequent tests are skipped. And in this case, where the SELECT statement has a single test expression, all the subsequent WHENs must evaluate that same expression.
Note if there is no argument to the select statement, you could use different test variables in the WHEN statements, just like your "else if" sample.
This is a good one to play with to figure out if/how it works.
You can make work.test2, then run your step:
data test;
set test2;
if card_type = 'F' then product = 'EC';
else if customer = 'C' then product = 'bc_card';
else if card_type in ('dg', 'dp', 'bg') then product = 'others';
run;
and check the log to make sure there are no errors/warnings/bad notes.
Then check the results with something like:
proc freq data=test;
tables card_type*customer*product/missing list;
run;
As others have indicated, the statements will run without error. However, there is a flaw. If those statements are creating a new variable named PRODUCT, it will be defined as two characters long. The first mention of PRODUCT (= 'EC') defines its length. So the values you will get are "EC", "bc", and "ot". The fix is simple: add your own LENGTH statement before SAS sets the length for you:
data test;
set test2;
length product $ 7;
if card_type = 'F' then product = 'EC';
else if customer = 'C' then product = 'bc_card';
else if card_type in ('dg', 'dp', 'bg') then product = 'others';
run;
The length of 7 allows room for the longest value being assigned ("bc_card").
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!
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.