BookmarkSubscribeRSS Feed
GeorgeSAS
Lapis Lazuli | Level 10

Hello,

 

I want simplify a complex if statement:

 

data one;

set had;

 if a in(1, 2, 3) or  b in (1,2,3)  or c in (1,2,3) then x=123;

run;

 

My question is can I simplify the if statement like this?:

 

data need;

set had;

 if (a or b or c) in (1,2,3) then x=123;

run;

 

Thanks

 

8 REPLIES 8
Ksharp
Super User

No.That is not right.

However, if you are using SAS/IML , there is a function ELEMENT() can do that.

ChrisNZ
Tourmaline | Level 20

No you can't do this.

 

If you have a long list of value, you can store them once only:

%let list_values=1 2 3;

 if a in(&list_values) or  b in (&list_values) or c in (&list_values) then x=123;

 

If you have a long list of variables you can use a macro loop to generate the statements.

 

%macro loop;

%local i list_values list_variables;

%let list_values=1 2 3;

%let list_variables= a b c;

if 0

%do i=1 %to 3;

or %scan(&list_variables, &i) in (&list_values)

%end;

 then x=123;

%mend;

%loop;

Reeza
Super User

Can A/B/C variables have any value at all, if not there may be other options. 

 

An array is probably easier and faster than a macro, especially if the values aren't 1/2/3.

 

data want;
set have;
array var_list(*) a b c;
flag=0;
do i=1,2,3;
	if whichn(i, of var_list(*))>0 then do;
		flag=123;
		leave;
	end;
end;
run;

 

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

This example concatenates the three values, then strips out all occurences of 1,2,3, and thus any length > 0 indicates a character not in the list 1,2,3.

data want;
  a=1; b=3; c=3; 
  if lengthn(compress(cats(a,b,c),"123"))=0 then x=123;
run;
Reeza
Super User

That assumes single digit values?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, thats all he posted in the OP.  It could be that b=12 would pass that logic check, but be inaccurate as > 3, but he hasn't mentioned that.  If it is the case, then use catx() with a delimiter, and remove delimter+digit.

GeorgeSAS
Lapis Lazuli | Level 10
any value. (1,2,3) just an example, maybe there are more characters such as ('abc','qwe',.....more)
RW9
Diamond | Level 26 RW9
Diamond | Level 26

In which case then no.  If there is no logical reason to arrive at a formula then one wont exist.  As mentioned above arrays are probably your best bet, i.e.:

 

data want;
  set have;
  array tmp <list of variables>;
  do over tmp;
    if tmp in (<set of values>) then result_value=<result>;
  end;
run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 1131 views
  • 2 likes
  • 5 in conversation