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

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          ...          ...          ...

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

4 REPLIES 4
ballardw
Super User

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;

whysas2015
Calcite | Level 5

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

ballardw
Super User

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..

whysas2015
Calcite | Level 5

Thank you it worked!

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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