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
No.That is not right.
However, if you are using SAS/IML , there is a function ELEMENT() can do that.
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;
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;
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;
That assumes single digit values?
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.
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.