BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
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
1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

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;

rajeshalwayswel
Pyrite | Level 9
If suppose if mentioned variables are characters(I mean character variable) in place of whichn function which we can use ?
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
PeterClemmensen
Tourmaline | Level 20

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;
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2754 views
  • 2 likes
  • 3 in conversation