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
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.
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.
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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.
Ready to level-up your skills? Choose your own adventure.