BookmarkSubscribeRSS Feed
taylor_sf
Calcite | Level 5

Hey! 

 

I have a single massive dataset that contains 6 variables.  One of the variables is a report date. I am wanting to create a loop macro that will breakdown the single dataset into individual datasets based on the report date.  There is roughly 175 different report dates and don't want to have to do them individually.  I would also like the macro to rename each dataset with the report date in the title ex: Output_07312018.  

 

Can anyone help?  I know this is vague but don't know where to even start.

 

Thanks 

5 REPLIES 5
Reeza
Super User

Why do you want to do this?

It's usually not recommended to do this, it's more inefficient in SAS than doing by group processing or creating a view/temp data set within a loop.

And how big is 'big' to you? What's the total number of records and the expected in each data set?

 

SAS programmers are often asked to break large data sets into smaller ones. Conventional wisdom says that this is also a pointless chore, since you can usually achieve what you want (that is, process a certain subset of data) by applying a WHERE= option or FIRSTOBS=/OBS= combination. Splitting a data set creates more files, which occupy more disk space and forces more I/O operations. I/O and disk access is often the most expensive part of your SAS processing, performance-wise.

 

If you feel you really need to, the blog post here details some of the ways this can be done dynamically.

https://blogs.sas.com/content/sasdummy/2015/01/26/how-to-split-one-data-set-into-many/

 


@taylor_sf wrote:

Hey! 

 

I have a single massive dataset that contains 6 variables.  One of the variables is a report date. I am wanting to create a loop macro that will breakdown the single dataset into individual datasets based on the report date.  There is roughly 175 different report dates and don't want to have to do them individually.  I would also like the macro to rename each dataset with the report date in the title ex: Output_07312018.  

 

Can anyone help?  I know this is vague but don't know where to even start.

 

Thanks 


 

taylor_sf
Calcite | Level 5

I am needing to provide the SAS data to an external vendor as monthly csv file through a secured website.  It should be a one time occurrence.

 

The monthly datasets would range anywhere from 300k to 500k records each.

PaigeMiller
Diamond | Level 26

The WHERE statement can do this for you. You don't need separate data sets. Depending on how many of these csv files you need to create, you may or may not need a loop.

--
Paige Miller
Reeza
Super User

You could use ODS CSV with the BYGROUP and NEWFILE options but it doesn't name the files by the BY group value, it names them with Report1.csv report2.csv etc. 

 

ods csvall file='C:\_localdata\demo.csv' newfile=bygroup;

*make sure only data goes to report;
title;footnote;*no title/footnotes;
ods noptitle;*supress proc title;
options nobyline; *no by lines to indicate by group;


proc print data=class noobs;
by sex;
run;

ods csvall close;

To get the full names you likely need a small macro or you could do it in a data step with CALL EXECUTE or DOSUBL but it can be harder to understand those at first. 

 

I would suggest writing a program to get it working for one month and then following the steps here to create a macro instead.

 

https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

 

I used CALL EXECUTE but you could loop the macro through your list of dates as well. If you can make the base program and run into issues, post back your code with details of what is not working and we can help you fix it. Or someone else may just post an answer. 

 


@taylor_sf wrote:

I am needing to provide the SAS data to an external vendor as monthly csv file through a secured website.  It should be a one time occurrence.

 

The monthly datasets would range anywhere from 300k to 500k records each.


 

ballardw
Super User

Anytime you only want records associated with a subset based on the value of the report data you can use a WHERE statement.

 

Of course your dates are actually SAS date values and not text or random numbers that look vaguely date-like such as 01032017 aren't they?

 

The principal works with any variable in any data set. SAS supplies a training data set, SASHELP.CLASS that is small but easily demonstrates many features.

For instance if I want to print the records from that set for only the females:

 

Proc Print data=sashelp.class;

   Where sex='F';

run;

Some procedures don't support the WHERE statement but you can use a data set option to do the same thing such as

 

Proc Means data=sashelp.class (where=( age>13));

   var height weight;

run;

which runs the default statistics on the variables Height and Weight in the data set for only the children who are older than 13.

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 884 views
  • 0 likes
  • 4 in conversation