BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
arde
Obsidian | Level 7

Hi,

 

I have sas file with 200 rows. I need to split them into 4 rows per csv file.  So I created  variable A and numbered each row sequential from 1 to 200.

 

I then programmed the following:

 

ods csv file="export.csv";
proc print data=test (where=(A between 1 and 4)) noobs;
run;

 

I now have to change the "1" and "4" manually until I get to "197" and "200".  Is there a way to automate this?

 

Thank you for time

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You can write CSV files using a simple data step, no need for either ODS or PROC EXPORT.

You even use the CALL VNEXT() function to let you write the header row to each file.

So here is an example using SASHELP.CLASS as the example dataset and the current WORK directory as the destination for writing the files that creates a series of files named export1.csv, export2.csv etc.

%let path=%sysfunc(pathname(work));
data _null_;
  do _n_=1 to 4 ;
    set sashelp.class;
    if _n_=1 then link names;
    put (_all_)(+0);
  end;
return;
names:
  length __name_ $32 __fileno 8 __filename $256;
  __fileno+1;
  __filename = cats("&path/export",__fileno,'.csv');
  file csv dsd filevar=__filename;
  do while(1);
    call vnext(__name_);
    if lowcase(__name_) = '__name_' then leave;
    put __name_ @;
  end;
  put; 
run;

So for your problem just change the setting of the macro variable PATH to the place you want to create the file.  Change the SET statement to reference the dataset you want to write.  Can change the line that is building the unique filename to reflect how you want the filenames created.

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

You can write CSV files using a simple data step, no need for either ODS or PROC EXPORT.

You even use the CALL VNEXT() function to let you write the header row to each file.

So here is an example using SASHELP.CLASS as the example dataset and the current WORK directory as the destination for writing the files that creates a series of files named export1.csv, export2.csv etc.

%let path=%sysfunc(pathname(work));
data _null_;
  do _n_=1 to 4 ;
    set sashelp.class;
    if _n_=1 then link names;
    put (_all_)(+0);
  end;
return;
names:
  length __name_ $32 __fileno 8 __filename $256;
  __fileno+1;
  __filename = cats("&path/export",__fileno,'.csv');
  file csv dsd filevar=__filename;
  do while(1);
    call vnext(__name_);
    if lowcase(__name_) = '__name_' then leave;
    put __name_ @;
  end;
  put; 
run;

So for your problem just change the setting of the macro variable PATH to the place you want to create the file.  Change the SET statement to reference the dataset you want to write.  Can change the line that is building the unique filename to reflect how you want the filenames created.

arde
Obsidian | Level 7
awesome, thank you so much!
Ksharp
Super User
%let n=4;

data have;
 set sashelp.class end=last;
 if mod(_n_,&n.)=1 then _id_+1;
 if last then call symputx('_id_',_id_);
run;

%macro export_csv(dsn=,path=);
%do i=1 %to &_id_.;
data temp(drop=_id_);
 set &dsn.(where=(_id_=&i.));
run;
proc export data=temp outfile="&path.\export_&i..csv" dbms=csv replace;
run;
%end;
%mend;
%export_csv(dsn=have,path=c:\temp)

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 16. 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
  • 4 replies
  • 1662 views
  • 2 likes
  • 4 in conversation