BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
shubham_d
Fluorite | Level 6

data haveTableOne;
input id code1 $ code2 $ code3 $;
datalines;
1 ABC EFG XYZ
2 ABC . .
3 . ABC XYZ
;

run;

IDCode 1Code 2Code 3
1ABCEFGXYZ
2ABC--
3-ABCXYZ

 

wantTableOne, here I want the data in this manner

ID

Overall  

Codes

1ABC
1EFG
1XYZ
2ABC
3ABC
3XYZ

 

Here, I have a different dataset where I want to merge the above code

 

data haveTableTwo;
input id var1 $ var2 $ var3 $;
datalines;
1 A B C
2 X Y Z
3 X Y Z
;
run;

idvar1var2var3
1ABC
2EFG
3XYZ


data wantTableTwo, here the id, var1-var3 would repeat as per the no. of codes present

 

IDvar1var2var3Overall_codes
1ABCABC
1ABCEFG
1ABCXYZ
2EFGABC
3XYZABC
3XYZXYZ

 

It's okay to not have wantTableOne & directly get the wantTableTwo. 

I hope the question is clear, any kind of help is appreciated, thanks! 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

One way:

data haveTableOne;
input id code1 $ code2 $ code3 $;
datalines;
1 ABC EFG XYZ
2 ABC . .
3 . ABC XYZ
;

run;

proc transpose data=havetableone 
   out=wanttable1 (drop=_name_ rename=(col1=OverallCode) where=( not missing(overallcode)));
  by id;
  var code1-code3;
run;

data haveTableTwo;
input id var1 $ var2 $ var3 $;
datalines;
1 A B C
2 X Y Z
3 X Y Z
;

data wanttabletwo;
   merge havetabletwo
         wanttable1
   ;
   by id;
run;

Warnings: If you have multiple rows with the same ID in the Havetableone this may not work.

Also the BY statements in the Proc Transpose and the last data step are going to expect the data to be sorted by ID.

 

 

View solution in original post

6 REPLIES 6
Quentin
Super User

Can you show the code you have tried?

 

Are you familiar with PROC TRANSPOSE? Arrays? The MERGE statement? PROC SQL?

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
shubham_d
Fluorite | Level 6
New to SAS but I've used Arrays & Merge statement. Once I get the wantTableOne, I can use the output statement & merge it onto table two. Still thinking of how I can create wantTableOne
ballardw
Super User

One way:

data haveTableOne;
input id code1 $ code2 $ code3 $;
datalines;
1 ABC EFG XYZ
2 ABC . .
3 . ABC XYZ
;

run;

proc transpose data=havetableone 
   out=wanttable1 (drop=_name_ rename=(col1=OverallCode) where=( not missing(overallcode)));
  by id;
  var code1-code3;
run;

data haveTableTwo;
input id var1 $ var2 $ var3 $;
datalines;
1 A B C
2 X Y Z
3 X Y Z
;

data wanttabletwo;
   merge havetabletwo
         wanttable1
   ;
   by id;
run;

Warnings: If you have multiple rows with the same ID in the Havetableone this may not work.

Also the BY statements in the Proc Transpose and the last data step are going to expect the data to be sorted by ID.

 

 

shubham_d
Fluorite | Level 6
Hey! Thanks, this was an easy-to-understand solution.
MayurJadhav
Quartz | Level 8

You can use array to combine data from all the character variables and put it under one variable and then perform the merge on haveTableTwo & wantTableOne datasets. 

 

data haveTableOne;
	input id code1 $ code2 $ code3 $;
	datalines;
1 ABC EFG XYZ
2 ABC . .
3 . ABC XYZ
;
run;

/* combine data from character variable into one variable */
data wantTableOne (where=(Overall_codes ne ''));
	set haveTableOne;
	array cvars {*} _character_;

	do i=1 to dim(cvars);
		Overall_codes=cvars{i};
		output;
	end;
	drop i code1-code3;
run;

data haveTableTwo;
	input id var1 $ var2 $ var3 $;
	datalines;
1 A B C
2 X Y Z
3 X Y Z
;
run;

data wantTableTwo;
	merge haveTableTwo wantTableOne;
	by id;
run;

Output:

MayurJadhav_0-1676329392925.png

 

Mayur Jadhav
BI Developer. Writer. Creative Educator.

SAS Blog → https://learnsascode.com
YouTube Channel: → https://www.youtube.com/@imayurj
shubham_d
Fluorite | Level 6
Hey! Thanks, this worked for me

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 6 replies
  • 1113 views
  • 6 likes
  • 4 in conversation