BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8
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?

5 REPLIES 5
yabwon
Onyx | Level 15

#Maxim4

 

Yes it will work.

if <a condition> then
  do;
    <code to run when true>
  end;
else
  do;
    <code to run when false>
  end;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



PaigeMiller
Diamond | Level 26

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

--
Paige Miller
mkeintz
PROC Star

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

 

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

--------------------------
Quentin
Super User

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;
BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Astounding
PROC Star

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").

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 5 replies
  • 434 views
  • 4 likes
  • 6 in conversation