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?

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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