Solved

SAS Data Sets Manipulations

Posted on 2010-09-21
4
745 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
  • 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How does Google get such fast response times? 5 122
MarkLogic 1 49
Has anyone used domo? 1 39
null value 15 65
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now