Below is the code I am using, I wrote some of it and had assistance for the rest, so my knowledge is limited.
The program works fine but I need a small adjustment. For the output file I need the name to have 3 digits. So the first file would be auto001.txt currently it is auto1.txt.
(I could do this outside of SAS but I am going to be doing this in significant volume and would like to get it output in ready to use format)
It is the '&i' in the proc export statement that i need to write as 001 and not 1.
Any help much appreciated.
%let in=n:\private\my documents\hypercompetition\IEEE submission\Excel Data;
%let out=n:\private\my documents\hypercompetition\IEEE submission\Excel Data\output1;
%let iterations=2;
proc datasets kill noprint;run;
%macro RandBetween(min, max);
(&min + floor((1+&max-&min)*rand("uniform")))
%mend;
%macro iter(filename, i);
data rnd;
set in;
if _n_=1;
x = %RandBetween (1, 8844);
run;
data out;
if _n_=1 then set rnd;
set in;
obs=_n_;
if obs lt x then delete;
if obs gt x+998 then delete;
drop x obs;
proc export data=out outfile="&out\&filename.&i..txt" dbms=csv replace; PUTNAMES=NO; run;
%mend;
%macro main(filename);
proc import datafile="&in\&filename..txt" out=in dbms=csv replace; getnames = no; run;
%do i=1 %to &iterations;
%iter(&filename., &i.);
%end;
%mend;
%main(auto);
You want to apply the z3 format to the macro variable i, so try replacing
proc export data=out outfile="&out\&filename.&i..txt" dbms=csv replace;
with
proc export data=out outfile="&out\&filename.%sysfunc(putn(&i,z3.))..txt" dbms=csv replace;
I used this bit of code to figure out the syntax:
%let counter=100;
%macro test;
%do i=1 %to &counter;
%let file=file%sysfunc(putn(&i,z3.));
%put FILE = &file..txt ;
%end;
%mend test;
%test
You want to apply the z3 format to the macro variable i, so try replacing
proc export data=out outfile="&out\&filename.&i..txt" dbms=csv replace;
with
proc export data=out outfile="&out\&filename.%sysfunc(putn(&i,z3.))..txt" dbms=csv replace;
I used this bit of code to figure out the syntax:
%let counter=100;
%macro test;
%do i=1 %to &counter;
%let file=file%sysfunc(putn(&i,z3.));
%put FILE = &file..txt ;
%end;
%mend test;
%test
Alternatively you can do:
%if %length(&i) = 1 %then %let j = 00&i; %else
%if %length(&i) = 2 %then %let j=0&i; %else %let j = &i;
proc export data=out outfile="&out\&filename.&j..txt" dbms=csv replace; PUTNAMES=NO; run;
Shmuel, thank you for your solution as well. I have copied your code for future reference.
Suzanne's solution did what I needed.
I'm so glad I found this board! My first post and I get a homerun response.
Suzanne the code you wrote work perfectly. Thank you.
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!
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.