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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 1373 views
  • 3 likes
  • 3 in conversation