SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Split data into multiple files by two levels of BY variables

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Split data into multiple files by two levels of BY variables

I have the following dataset and need to split it into separate files based on DATE and ID. I need to use a combination of the two variables as the file names, for example, "JAN2001_1.CSV". I do not know ex ante how many groups there are. Any help would be greatly appreciated!

DATE                   ID       VAR1     VAR2     VAR3    

JAN2001               1          ...          ...          ...

JAN2001               2          ...          ...          ...

JAN2002               1          ...          ...          ...

JAN2002               2          ...          ...          ...


Accepted Solutions
Solution
‎07-14-2015 12:27 PM
Super User
Posts: 10,500

Re: Split data into multiple files by two levels of BY variables

Do you have your data in a SAS data set? If so, your data set name goes where I put "have".

The path you want your data to go to replaces "c:\folder\subfolder".

Put the names of the variables you want to output to the file on the put statement.

The program is basically complete for the splitting. Nothing more to do.

If you haven't read the data into SAS yet, that is the first step. If that is the case, you may want to sort by Date and ID after reading it and before the Data _null_ code..

View solution in original post


All Replies
Super User
Posts: 10,500

Re: Split data into multiple files by two levels of BY variables

Create a variable to hold the output file name, the FILEVAR options and Put:

data _null_;

     set have;

     length MyOut $ 200;

     MyOut = cats("c:\folder\subfolder",date,"_",ID,".CSV");

     file outfile Filevar=MyOut dlm=',';

     put DATE ID VAR1 VAR2 VAR3;

run;

New Contributor
Posts: 3

Re: Split data into multiple files by two levels of BY variables

Sorry I really don't know how the rest of the program would look like. Can you provide more information? Thank you.

Solution
‎07-14-2015 12:27 PM
Super User
Posts: 10,500

Re: Split data into multiple files by two levels of BY variables

Do you have your data in a SAS data set? If so, your data set name goes where I put "have".

The path you want your data to go to replaces "c:\folder\subfolder".

Put the names of the variables you want to output to the file on the put statement.

The program is basically complete for the splitting. Nothing more to do.

If you haven't read the data into SAS yet, that is the first step. If that is the case, you may want to sort by Date and ID after reading it and before the Data _null_ code..

New Contributor
Posts: 3

Re: Split data into multiple files by two levels of BY variables

Thank you it worked!

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 479 views
  • 2 likes
  • 2 in conversation