In data step, how to make the if statement work: if variable in ('a', 'b', 'c')

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 130
Accepted Solution

In data step, how to make the if statement work: if variable in ('a', 'b', 'c')

I try to write something like:

data want;

set have;

if var1 in ('aa', 'bb', 'cc') ;

run;

var1 is characteristic with string value aa, bb, cc, dd, ee, ff, gg.

Does the if statement above work only when values within the parentheses are numeric?

Or, how to make it work?

Thanks!


Accepted Solutions
Solution
‎03-24-2015 05:57 PM
Grand Advisor
Posts: 10,210

Re: In data step, how to make the if statement work: if variable in ('a', 'b', 'c')

You don't need to permanently change the case:

if lowcase(var1) in

and of course if you have that leading space issue:

if strip(lowcase(var1)) in ('aa', <etc>

View solution in original post


All Replies
Esteemed Advisor
Posts: 5,198

Re: In data step, how to make the if statement work: if variable in ('a', 'b', 'c')

Works for me. If you are mixing char/numeric you will get a warning or note.

Have you the right case in var1?

SAS doesn't support CI style string comparisons.

Data never sleeps
Frequent Contributor
Posts: 130

Re: In data step, how to make the if statement work: if variable in ('a', 'b', 'c')

Thank you for your quick response. I will double check, so looks like it is not the if statement's problem.

Grand Advisor
Posts: 10,210

Re: In data step, how to make the if statement work: if variable in ('a', 'b', 'c')

If there are leading spaces, x= '  aa', then the comparison will fail. Try:

if strip(var1) in ('aa','bb','cc');

Also case sensitive just in case var1 actually is Aa, AA or aA then the comparison will fail.

Frequent Contributor
Posts: 130

Re: In data step, how to make the if statement work: if variable in ('a', 'b', 'c')

thanks for remind me that! so it would be better to first convert all value to lowercase or uppercase.

Solution
‎03-24-2015 05:57 PM
Grand Advisor
Posts: 10,210

Re: In data step, how to make the if statement work: if variable in ('a', 'b', 'c')

You don't need to permanently change the case:

if lowcase(var1) in

and of course if you have that leading space issue:

if strip(lowcase(var1)) in ('aa', <etc>

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 215 views
  • 3 likes
  • 3 in conversation