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

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

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

3 REPLIES 3
ballardw
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

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
  • 3 replies
  • 945 views
  • 4 likes
  • 4 in conversation