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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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