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

Hi Friends,

 

I have following sas dataset

Name       Age       Country

Sam         15          CA

Rohan      13         USA

Mich         16         IND

 

the result required is separate CSVs for each record in above dataset. In this case it will be three CSVs with one record each.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

And another example if you want to use a FILEVAR approach.

 

https://gist.github.com/statgeek/047bc83a85672f4dd546

 

Change the folders/myfolders section and you can run the code and test it yourself and then adapt it for your data needs. 

Two other things to think about:

  1. Filename changing for each record, what are the rules
  2. Does each file need a header record

 

PROC SORT DATA=SASHELP.CARS OUT=CARS;
BY make;
RUN;


DATA _NULL_;

SET cars; *Dataset to be exported;
BY make; *Variable that file is to be split on;

*Create path to file that is to be exported;
if first.make then out_file=cats('/folders/myfolders/', trim(make));

file temp filevar=out_file dlm=',' dsd;

*If first value of make then output column names;
if first.make then 
put 'Make, Model, MPG_HIGHWAY, MPG_CITY';

*Output variables;
put make model mpg_highway mpg_city;

run;

View solution in original post

5 REPLIES 5
Reeza
Super User

Do you need to control the names? 

If not ODS CSVALL is a good option. 

 



ods csvall body = '/home/userName/Demo1/demo.csv' newfile=bygroup;

options nobyline;

proc print data=have noobs;
by name;
var name age country;
run;

ods csvall close;
options byline;
Reeza
Super User

And another example if you want to use a FILEVAR approach.

 

https://gist.github.com/statgeek/047bc83a85672f4dd546

 

Change the folders/myfolders section and you can run the code and test it yourself and then adapt it for your data needs. 

Two other things to think about:

  1. Filename changing for each record, what are the rules
  2. Does each file need a header record

 

PROC SORT DATA=SASHELP.CARS OUT=CARS;
BY make;
RUN;


DATA _NULL_;

SET cars; *Dataset to be exported;
BY make; *Variable that file is to be split on;

*Create path to file that is to be exported;
if first.make then out_file=cats('/folders/myfolders/', trim(make));

file temp filevar=out_file dlm=',' dsd;

*If first value of make then output column names;
if first.make then 
put 'Make, Model, MPG_HIGHWAY, MPG_CITY';

*Output variables;
put make model mpg_highway mpg_city;

run;
Suminder
Calcite | Level 5

the rules are as follows:

1) 

the first csv will be named as output1.csv
the second csv will be named as output2.csv

2)

output csvs does not require headers.

 

note:

there is a column in the source dataset that is rownumber... 

Tom
Super User Tom
Super User

@Suminder wrote:

the rules are as follows:

1) 

the first csv will be named as output1.csv
the second csv will be named as output2.csv

2)

output csvs does not require headers.

 

note:

there is a column in the source dataset that is rownumber... 


Build the filename as a variable in the data step that is writing the files.  You can then use the FILEVAR= option on the FILE statement to have SAS make many different files.

data _null_;
  set have ;
  length fname $200 ;
  fname = cats('output',rownumber,'.csv');
  file dummy filevar=fname dsd ;
  put Name Age Country ;
run;

Remember to add the directory to the FNAME value or else output1.csv etc will be created in the current working directory of process that is running SAS.

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