data haveTableOne;
input id code1 $ code2 $ code3 $;
datalines;
1 ABC EFG XYZ
2 ABC . .
3 . ABC XYZ
;
run;
ID | Code 1 | Code 2 | Code 3 |
1 | ABC | EFG | XYZ |
2 | ABC | - | - |
3 | - | ABC | XYZ |
wantTableOne, here I want the data in this manner
ID | Overall Codes |
1 | ABC |
1 | EFG |
1 | XYZ |
2 | ABC |
3 | ABC |
3 | XYZ |
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;
id | var1 | var2 | var3 |
1 | A | B | C |
2 | E | F | G |
3 | X | Y | Z |
data wantTableTwo, here the id, var1-var3 would repeat as per the no. of codes present
ID | var1 | var2 | var3 | Overall_codes |
1 | A | B | C | ABC |
1 | A | B | C | EFG |
1 | A | B | C | XYZ |
2 | E | F | G | ABC |
3 | X | Y | Z | ABC |
3 | X | Y | Z | XYZ |
It's okay to not have wantTableOne & directly get the wantTableTwo.
I hope the question is clear, any kind of help is appreciated, thanks!
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.
Can you show the code you have tried?
Are you familiar with PROC TRANSPOSE? Arrays? The MERGE statement? PROC SQL?
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.
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:
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!
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.
Ready to level-up your skills? Choose your own adventure.