DATA Step, Macro, Functions and more

Create multiple dataset from source dataset

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

Create multiple dataset from source dataset

I have a source dataset that have multiple departments. I want to create separate dataset for every departmet.

I can do it by creating macro %Create_dept_data(dept_nm) then call this macro multiple time for every department dynamically. 

 

Is there any better way to do it in data step with out using macro.

Note: there can be any number of department in have dataset.

 

 

data have;
input Dept $ EmployeeName $10.;
datalines;
HR Rocky
HR Samy
Finance Souley
Finance Boby
Admin John
Admin Ahmed
;

 


Output
Three dataset
Dataset name: Hr
Hr Rocky
Hr Samy

 

Dataset Name: Finance
Finance Souley
Finance Boby

 

Dataset Name:Finance
Admin John
Admin Ahmed


Accepted Solutions
Solution
‎04-19-2018 11:16 AM
Super User
Super User
Posts: 9,427

Re: Create multiple dataset from source dataset

Splitting same data up into smaller blocks is rarely a good idea, and will make your programming X times harder.

Now you can do it:

proc sort data=have out=loop nodupkey;
  by dept;
run;
data _null_;
  set loop;
  call execute(cat('data ',strip(dept),'set have; where dept="',strip(dept),'"; run;'));
run;

However, again I don't recommend splitting data up.

View solution in original post


All Replies
Super User
Posts: 13,333

Re: Create multiple dataset from source dataset

[ Edited ]

Very often the question first answered should be is it actually necessary to create multiple data sets. You can prepare reports using BY group processing for example to create separate pages/summaries for each level, or combinations of levels that way. Or use a WHERE statement to reduce data for a specific report to the department and/or employees of interest.

 

Having the data in a single set may also be preferable if you have employees that change departments so you get all of the records at one time instead of having to then search among multiple data sets to get all of the records for employees.

Solution
‎04-19-2018 11:16 AM
Super User
Super User
Posts: 9,427

Re: Create multiple dataset from source dataset

Splitting same data up into smaller blocks is rarely a good idea, and will make your programming X times harder.

Now you can do it:

proc sort data=have out=loop nodupkey;
  by dept;
run;
data _null_;
  set loop;
  call execute(cat('data ',strip(dept),'set have; where dept="',strip(dept),'"; run;'));
run;

However, again I don't recommend splitting data up.

PROC Star
Posts: 1,591
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 78 views
  • 4 likes
  • 4 in conversation