Solved

SAS Data Sets Manipulations

Posted on 2010-09-21
4
760 Views
Last Modified: 2013-11-16
I have four SAS datasets as my inputs:
 masterdata
 data1
 data2
 data3

I need to separate the "masterdata" file into 3 wave files (Example: basically I need to have every "masterdata" record that match "data1" written to my output dataset "newdata1").
So, my output datasets are "newdata1", "newdata2", and "newdatat3".

Note: On all my input datastes my key variables are 'var3', 'var4', and 'var5'.
Here is what I came up with.  Is this correct?  If not, what do I need change or add?

proc sort data=lib.masterdata;
 by var3 var4 var5;
run;

proc sort data=lib.data1;
 by var3 var4 var5;
run;

data lib.newdata1;
 merge lib.masterdata(in=a) lib.newdata1(in=b);
  by var3 var4 var5;
 if a and b;
run;


proc sort data=lib.data2;
 by var3 var4 var5;
run;

data lib.newdata2;
 merge lib.masterdata(in=a) lib.data2(in=b);
  by var3 var4 var5;
 if a and b;
run;


proc sort data=lib.data3;
 by var3 var4 var5;
run;

data lib.newdata3;
 merge lib.masterdata(in=a) lib.data3(in=b);
  by var3 var4 var5;
 if a and b;
run;


     
0
Comment
Question by:labradorchik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:labradorchik
ID: 33725010
Sorry, my code should read as below:

proc sort data=lib.masterdata;
 by var3 var4 var5;
run;

proc sort data=lib.data1;
 by var3 var4 var5;
run;

data lib.newdata1;
 merge lib.masterdata(in=a) lib.data1(in=b);
  by var3 var4 var5;
 if a and b;
run;


proc sort data=lib.data2;
 by var3 var4 var5;
run;

data lib.newdata2;
 merge lib.masterdata(in=a) lib.data2(in=b);
  by var3 var4 var5;
 if a and b;
run;


proc sort data=lib.data3;
 by var3 var4 var5;
run;

data lib.newdata3;
 merge lib.masterdata(in=a) lib.data3(in=b);
  by var3 var4 var5;
 if a and b;
run;

0
 
LVL 9

Accepted Solution

by:
bradanelson earned 300 total points
ID: 33725328
It looks good.  Are you not getting the results you are expecting?  There are lots of ways to do this, and everyone will have there own opinion on the "best way".  However, if it makes sense to you and it works, you should stick with it.

You would get similar results using PROC SQL without the need to SORT the data:
%MACRO NewData(dsn);
  PROC SQL;
    CREATE TABLE lib.new&dsn AS
    SELECT a.*, b.*
    FROM lib.masterdata a INNER JOIN lib.&dsn b
    ON a.var3=b.var3 AND a.var4=b.var4 AND a.var5=b.var5;
  QUIT;
%MEND NewData;

%NewData(data1);
%NewData(data2);
%NewData(data3)


Or you could try and MERGE all three dataset in one MERGE.
proc sort data=lib.masterdata;
 by var3 var4 var5;
run;

proc sort data=lib.data1;
 by var3 var4 var5;
run;

proc sort data=lib.data2;
 by var3 var4 var5;
run;

proc sort data=lib.data3;
 by var3 var4 var5;
run;

data lib.newdata1 lib.newdata2 lib.newdata3;
 merge lib.masterdata(in=a) lib.data1(in=b) lib.data2(in=c) lib.data3(in=d);
  by var3 var4 var5;

  if a and b THEN OUTPUT lib.newdata1;
  if a and c THEN OUTPUT lib.newdata2;
  if a and d THEN OUTPUT lib.newdata3;
run;

Good luck!
0
 
LVL 14

Expert Comment

by:Aloysius Low
ID: 33725726
same comment as bradanelson: i don't see anything wrong with it. but if you have run it and it's not producing the results you are expecting then do post it here so that we can help...
0
 

Author Closing Comment

by:labradorchik
ID: 33727117
bradanelson,
Thank you!!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question