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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.