BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
abdulla
Pyrite | Level 9

Hi I have the following data

I want to create a column "want" in which if TIS is equal to any of the value in columns A1 to A8. I tried the following but I did not get the value I want for "Want" column. 

 

Data want,

set have;

if TIS=(A1--A8) then want=1;

else want=0;

run;

 

TISA1A2A3A4A5A6A7A8want
73727323       0
514951225064      0
1541421349534225154151413341  1
334135773572737273737379   0
2098209920432041204520242098514951431
495349116719      0
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input TIS A1 A2 A3 A4 A5 A6 A7 A8;
infile datalines missover;
datalines;
7372 7323                                    
5149 5122 5064                               
1541 4213 4953 4225 1541 5141 3341           
3341 3577 3572 7372 7373 7379                
2098 2099 2043 2041 2045 2024 2098 5149 5143 
4953 4911 6719                               
run;

data want;
   set have;
   array a{*} A1--A8;
   if tis in a then want = 1;
   else want = 0;
run;

 

Result:

 

TIS   A1   A2   A3   A4   A5   A6   A7   A8   want 
7372  7323 .    .    .    .    .    .    .    0 
5149  5122 5064 .    .    .    .    .    .    0 
1541  4213 4953 4225 1541 5141 3341 .    .    1 
3341  3577 3572 7372 7373 7379 .    .    .    0 
2098  2099 2043 2041 2045 2024 2098 5149 5143 1 
4953  4911 6719 .    .    .    .    .    .    0 

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input TIS A1 A2 A3 A4 A5 A6 A7 A8;
infile datalines missover;
datalines;
7372 7323                                    
5149 5122 5064                               
1541 4213 4953 4225 1541 5141 3341           
3341 3577 3572 7372 7373 7379                
2098 2099 2043 2041 2045 2024 2098 5149 5143 
4953 4911 6719                               
run;

data want;
   set have;
   array a{*} A1--A8;
   if tis in a then want = 1;
   else want = 0;
run;

 

Result:

 

TIS   A1   A2   A3   A4   A5   A6   A7   A8   want 
7372  7323 .    .    .    .    .    .    .    0 
5149  5122 5064 .    .    .    .    .    .    0 
1541  4213 4953 4225 1541 5141 3341 .    .    1 
3341  3577 3572 7372 7373 7379 .    .    .    0 
2098  2099 2043 2041 2045 2024 2098 5149 5143 1 
4953  4911 6719 .    .    .    .    .    .    0 
Kurt_Bremser
Super User

With a proper data structure, this is extremely simple:

proc transpose
  data=have
  out=long (where=(col1 ne .))
;
by tis;
var a:;
run;

data want;
set long;
where tis = col1;
by tis;
if last.tis;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 863 views
  • 2 likes
  • 3 in conversation