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

dear all,

 

I am trying to split a dataset and export them as .txt file by following codes

 

%macro split(k=);

data test;
set Sa_step5.Pat_name_with_address_dis end=eof nobs=count;
if eof then call symput('nobs', left(count));
run;

%let n=%sysfunc(int(%eval(&nobs/&k)));

%put &nobs &n;

%do i=1 %to &n;
data Sa_step6.Pat_name_with_address_dis&i test;
set test;
if _N_<=&k then output Sa_step6.Pat_name_with_address_dis&i;
else output test;
run;

PROC EXPORT DATA=Sa_step6.Pat_name_with_address_dis&i
            OUTFILE= "C:\Users\70660\Desktop\Pat_name_with_address_dis\Pat_name_with_address_dis&i.txt" 
            DBMS=tab REPLACE;
			delimiter=',';
RUN;

%end;
%mend;

%split(k=1000);

however, I create the file like 'Pat_name_with_address_dis1txt' rather than 'Pat_name_with_address_dis1.txt', could you give me some suggestions about this?

 

thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
r_behata
Barite | Level 11

Try Inserting an extra dot '..' after the macro variable as the single dot will be interpreted as the macro variable boundary. 

PROC EXPORT DATA=Sa_step6.Pat_name_with_address_dis&i
            OUTFILE= "C:\Users\70660\Desktop\Pat_name_with_address_dis\Pat_name_with_address_dis&i..txt" 
            DBMS=tab REPLACE;
			delimiter=',';
RUN;

View solution in original post

4 REPLIES 4
Reeza
Super User

Add another period after the macro variable.

FYI - its faster to use a data set option to filter your data rather than to create a temporary data set.

 

Your code isn't going to split your data set by the way, it'll create cumulative data. Ie your first data set will have 1000 records, your second will have 2000, your third would have 3000 if the size was 1000 per data set. Is that what you want?

@France wrote:

dear all,

 

I am trying to split a dataset and export them as .txt file by following codes

 

%macro split(k=);

data test;
set Sa_step5.Pat_name_with_address_dis end=eof nobs=count;
if eof then call symput('nobs', left(count));
run;

%let n=%sysfunc(int(%eval(&nobs/&k)));

%put &nobs &n;

%do i=1 %to &n;
data Sa_step6.Pat_name_with_address_dis&i test;
set test;
if _N_<=&k then output Sa_step6.Pat_name_with_address_dis&i;
else output test;
run;

PROC EXPORT DATA=Sa_step6.Pat_name_with_address_dis&i
            OUTFILE= "C:\Users\70660\Desktop\Pat_name_with_address_dis\Pat_name_with_address_dis&i.txt" 
            DBMS=tab REPLACE;
			delimiter=',';
RUN;

%end;
%mend;

%split(k=1000);

however, I create the file like 'Pat_name_with_address_dis1txt' rather than 'Pat_name_with_address_dis1.txt', could you give me some suggestions about this?

 

thanks in advance


 

 

 

r_behata
Barite | Level 11

Try Inserting an extra dot '..' after the macro variable as the single dot will be interpreted as the macro variable boundary. 

PROC EXPORT DATA=Sa_step6.Pat_name_with_address_dis&i
            OUTFILE= "C:\Users\70660\Desktop\Pat_name_with_address_dis\Pat_name_with_address_dis&i..txt" 
            DBMS=tab REPLACE;
			delimiter=',';
RUN;
Tom
Super User Tom
Super User

No need for macro code or separate SAS dataset to just generate multiple delimited files from a single source dataset.

Try this little program to split SASHELP.CLASS into files with max of 10 records.

%let path=%sysfunc(pathname(work));

data _null_;
  set sashelp.class ;
  link file;
  put (_all_) (+0);
  return;
file:
  fileno=1+int(_n_/10);
  length filename $300;
  filename = catx('/',"&path",cats('filename_',fileno,'.txt'));
  file out dsd dlm='09'x filevar=filename ;
return;
run;

If you want a header row on each file then add more logic to test if you are starting a new file and write it.

FreelanceReinh
Jade | Level 19

I'd suggest

fileno=1+int((_n_-1)/10);

to split, e.g., 20 observations into 10+10 rather than 9+10+1.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2335 views
  • 3 likes
  • 5 in conversation