Here is my sample data:
data a;
input ZIP Member_Count;
datalines;
1254 234
9936 123
7529 454
9154 678
;
run;
data b;
input ZIP Member_Count;
datalines;
2156 555
9936 433
3346 225
9154 782
;
run;
data c;
input ZIP Script_Count;
datalines;
2156 232
2299 656
2222 222
9154 690
;
run;
data d;
input ZIP Script_Count;
datalines;
2156 555
9936 433
9154 225
2290 782
;
run;
And here is what I want:
data want;
input ZIP amember bmember cscripts dscripts;
datalines;
1254 234 . . .
9936 123 433 . 433
7529 454 . . .
9154 678 782 690 225
2156 . 555 232 555
2299 . . 656 .
2222 . . 222 .
2290 . . . 782
3346 . 225 . .
;;
run;
Basically, I want to combine the data from each dataset but keep distinct ZIP Codes and put values for each zip in columns after it.
The order of the ZIPS in the output dataset doesn't matter.
Why not multiple sorts (with embedded variable renames), and one merge?
proc sort data=a (rename=(member_count=amember));
by zip;
run;
proc sort data=b (rename=(member_count=bmember));
by zip;
run;
proc sort data=c (rename=(script_count=cscript));
by zip;
run;
proc sort data=d (rename=(script_count=dscript));
by zip;
run;
data want;
merge a b c d;
by zip;
run;
First, sort each data set by ZIP. Then combine them:
data want;
merge a (rename=(member_count=amember))
b (rename=(member_count=bmember))
c (rename=(script_count=cscripts))
d (rename=(script_count=dscripts));
by zip;
run;
It can also be done using SQL, in straightforward fashion if that is your preferred tool.
Why not multiple sorts (with embedded variable renames), and one merge?
proc sort data=a (rename=(member_count=amember));
by zip;
run;
proc sort data=b (rename=(member_count=bmember));
by zip;
run;
proc sort data=c (rename=(script_count=cscript));
by zip;
run;
proc sort data=d (rename=(script_count=dscript));
by zip;
run;
data want;
merge a b c d;
by zip;
run;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.