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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1506 views
  • 2 likes
  • 2 in conversation