BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
Astounding
PROC Star

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.

mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

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
  • 2 replies
  • 805 views
  • 1 like
  • 3 in conversation