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

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

1 ACCEPTED SOLUTION

Accepted Solutions
SuzanneDorinski
Lapis Lazuli | Level 10

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

4 REPLIES 4
SuzanneDorinski
Lapis Lazuli | Level 10

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
Shmuel
Garnet | Level 18

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;

 

 

Meature
Fluorite | Level 6

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

Suzanne's solution did what I needed.

Meature
Fluorite | Level 6

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 1159 views
  • 8 likes
  • 3 in conversation