data have;
input a a1 a2 a3;
cards;
1 2 3 4
1 2 3 1
1 1 2 1
1 2 3 1
run;
data want;
set have ;
if a=1 or a1=1 or a2 =1 or a3=1 then a4=1;
run;
I have variables with same prefix "a" 100 variables so i'm writing code like above one but i don't need to write 100 variables names i'm using a:=1 but it not working any suggestions
First, its not a great idea to have lots of variables, normalise them into rows and you will find programming a lot easier.
As for character, concatenate, then find:
if index(catx(',',of a:),"1") then a4=1;
Do something like this
data have;
input a a1 a2 a3;
cards;
1 2 3 4
1 2 3 1
1 1 2 1
1 2 3 1
run;
data want;
set have;
array MyArray{*} a:;
if whichn(1, of MyArray[*]) ne 0 then a4=1;
run;
First, its not a great idea to have lots of variables, normalise them into rows and you will find programming a lot easier.
As for character, concatenate, then find:
if index(catx(',',of a:),"1") then a4=1;
You can use the WHICHC function instead like this
data have;
input a $ a1 $ a2 $ a3 $;
cards;
1 2 3 4
1 2 3 1
1 1 2 1
1 2 3 1
2 2 2 2
run;
data want;
set have;
if whichc("1", of a:) ne 0 then a4="1";
run;
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.