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

Can one write to two, or more,  text files using one data step?  I cannot Google this anywhere, here or elsewhere?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@PhilC wrote:

My RL program is needing me to write ~150 text files.


Details help.

How do you know when to write to which file?

Do they all getthe same variables?

What format is the "text file"? Fixed column, delimited or what?

 

I think that you want to look at the FILEVAR option on the File statement.

Suggest that you use a full path in the name of the file.

data _null_;
  set sashelp.class;
  outname=catt("D:\Users\Owner\Documents\",put(age,z2.),".csv");
  file dummy filevar=outname dlm=",";
  put name sex age height weight;

run;

In this case the "fileref" dummy is basically a placeholder as it uses the text of the Filevar=variable for the file name.

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

I'd simply try it:

data _null_;
set sashelp.class;
file "t1.csv" dlm=",";
put name sex age height weight;
file "t2.csv" dlm=",";
put name sex age height weight;
run;
PhilC
Rhodochrosite | Level 12

You answered the question.  So then I change it. (sorry)

 

IF you are writing more than one text file and you want the file to be named by a SAS run-time variable, we can't do this:

 

data _null_;
  set sashelp.class;
  fname=catt(put(age,z2.),".csv");
  file fname dlm=",";
    put name sex age height weight;
run;

How do I save to file named in var FNAME? 

 

I should have mentioned Fopen() and other statements.  I did not anticipate your simple solution.  It adds to my knowledge.  I know I can use a select statement with that solution, but I was really curious about how to avoiding a select statement. 

PhilC
Rhodochrosite | Level 12

My RL program is needing me to write ~150 text files.

ballardw
Super User

@PhilC wrote:

My RL program is needing me to write ~150 text files.


Details help.

How do you know when to write to which file?

Do they all getthe same variables?

What format is the "text file"? Fixed column, delimited or what?

 

I think that you want to look at the FILEVAR option on the File statement.

Suggest that you use a full path in the name of the file.

data _null_;
  set sashelp.class;
  outname=catt("D:\Users\Owner\Documents\",put(age,z2.),".csv");
  file dummy filevar=outname dlm=",";
  put name sex age height weight;

run;

In this case the "fileref" dummy is basically a placeholder as it uses the text of the Filevar=variable for the file name.

PhilC
Rhodochrosite | Level 12

right, not "dummy" because I didn't write out my details.  🙂

 

Yet -- This was what I needed.  

 

My RL code executes like this, within the context of our SASHELP.CLASS  example.

proc sort data=sashelp.class out=class;
  by Age;
data _null_;
  do until (eof);
    set class end=eof;
      by Age;
    if first.Age then do;
      length fname $200;
        retain fname;
        fname=catt(put(age,z2.),".csv");
      file dummy filevar=Fname dlm=",";
      end;
    put name sex age height weight;
  end;
  stop;
run;

I didn't need headers in my RL code, so there are none here. 

 

Thanks Kurt,

Thanks Ballardw,

you are like SAS gods to me.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 2241 views
  • 3 likes
  • 3 in conversation