DATA Step, Macro, Functions and more

Bootstrapping need output file to be 001 not 1, etc.

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Bootstrapping need output file to be 001 not 1, etc.

[ Edited ]

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);


Accepted Solutions
Solution
‎07-15-2017 04:17 PM
Frequent Contributor
Posts: 89

Re: Bootstrapping need output file to be 001 not 1, etc.

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

View solution in original post


All Replies
Solution
‎07-15-2017 04:17 PM
Frequent Contributor
Posts: 89

Re: Bootstrapping need output file to be 001 not 1, etc.

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
Trusted Advisor
Posts: 1,554

Re: Bootstrapping need output file to be 001 not 1, etc.

Posted in reply to SuzanneDorinski

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;

 

 

New Contributor
Posts: 3

Re: Bootstrapping need output file to be 001 not 1, etc.

Shmuel, thank you for your solution as well. I have copied your code for future reference.

Suzanne's solution did what I needed.

New Contributor
Posts: 3

Re: Bootstrapping need output file to be 001 not 1, etc.

Posted in reply to SuzanneDorinski

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 590 views
  • 8 likes
  • 3 in conversation