Help using Base SAS procedures

Write to file from data step.

Accepted Solution Solved
Reply
Regular Contributor
Posts: 161
Accepted Solution

Write to file from data step.

Hi,

I need dynamically create files and fill them by needed text in datastep, example of code below:

data test;

     filePath="C:...\file.txt";

     rc1=filename("newFile",filePath);

     fid=fopen("newFile","A");

     put "txttxt";

     rcClose=fclose(fid);

run;

File creates(with zero length) but file content is missing, instead "txttxt" text writes to SAS log.

Factually put statament write text not into created file but to SAS log.

So how can I write needed text into the file?

Thanks!


Accepted Solutions
Solution
‎08-16-2012 10:30 AM
PROC Star
Posts: 7,467

Re: Write to file from data step.

Like Astounding suggested, call execute is definitely one way of doing what I think you are trying to accomplish.  e.g.:

data _null_;

  informat file_path text_to_write $50.;

  input file_path text_to_write &;

  call execute(

     'data _null_; file "'

     ||strip(file_path)

     ||'"; put "'

     ||text_to_write

     ||'"; run;'

     );

  cards;

c:\test1.txt   this is a test

c:\test2.txt   some other text

;

View solution in original post


All Replies
PROC Star
Posts: 7,467

Re: Write to file from data step.

Not really sure what you are trying to do, but think you are over-complicating the process.  Are you just trying to do something like?:

data _null_;

     file "C:...\file.txt";

     put "txttxt";

run;

Regular Contributor
Posts: 161

Re: Write to file from data step.

Hi Artur,

I should generate filename "on fly" depends on value from input dataset.

So if you will try example similar to your but with small change:

data _null_;

     path="C:...\file.txt";

     file path;

     put "txttxt";

run;

It will not work becouse filename in file statement should be predefined before dataset...

Super User
Posts: 5,497

Re: Write to file from data step.

Yura,

Some of the other details of the problem might be important here.  Think about these possibilities.

Could you use two DATA steps instead of one?  The first one captures the destination file as a macro variable.  The second one uses the macro variable in the FILE statement.

Are you writing to just one file per DATA step?  You might be able to define a temporary file as the path:

file 'C:.../temp.txt';

Then have the DATA step generate a system command to copy temp.txt to the real destination, and another system command to delete temp.txt.  You would probably use CALL EXECUTE to do that, something along the lines of:

if eof then call execute('x copy temp.txt ' || true_destination || '; x delete temp.txt;');

There are definitely some details to work out (syntax for the system commands, where to add quotes within it).  It's wasteful if the files are large, but it wasn't clear from your example if they were large or small.

Good luck.

Regular Contributor
Posts: 161

Re: Write to file from data step.

Posted in reply to Astounding

Hi  ,

I should write many files from datastep.

Offcource I can use few data step or some macro with loop and then file statement for each created file, but it will increaze execution time, and it is critical...

In real task I should create ~20-30 files each time from one datastep.

May be these functions - fopen fclose etc. were created just for managing(delete,create files) but in this case why it( fopen func) has ,for example, 'A'(append) prameter( modifier) if there is no possibility wite from same datastep into this file...

Should be some possibility route any text into created from same data step file(I hope so)...

Thanks!

Regular Contributor
Posts: 161

Re: Write to file from data step.

Posted in reply to Astounding

Hi Arthur,

Thanks, it's what I need!

Solution
‎08-16-2012 10:30 AM
PROC Star
Posts: 7,467

Re: Write to file from data step.

Like Astounding suggested, call execute is definitely one way of doing what I think you are trying to accomplish.  e.g.:

data _null_;

  informat file_path text_to_write $50.;

  input file_path text_to_write &;

  call execute(

     'data _null_; file "'

     ||strip(file_path)

     ||'"; put "'

     ||text_to_write

     ||'"; run;'

     );

  cards;

c:\test1.txt   this is a test

c:\test2.txt   some other text

;

Super User
Super User
Posts: 7,039

Re: Write to file from data step.

It is easy to dynamically select the filename to write to using the FILEVAR option on the FILE statement.

data _null_;

  set sashelp.class;

  filename = catx('\','c:\temp',catx('.',sex,'txt'));

  file out filevar=filename;

  put name;

run;

C:\temp>more F.txt

Joyce

Judy

Louise

Mary

C:\temp>more M.txt

Philip

Robert

Ronald

Thomas

William

Regular Contributor
Posts: 161

Re: Write to file from data step.

Hi Tom,

Thanks, it also works, looks like it is the most qucik solution.

I have one more small issue, if few times write put statement it obviosly write text into two separate lines, but I need store them into one line, for example:

data _null_;

  set sashelp.class;

  filename = catx('\','c:\temp',catx('.',sex,'txt'));

  file out filevar=filename;

  put "txt1";

  put "txt2";

run;

And result in file will be :

txt1

txt2

But I need "txt1txt2".

So may be you also know how to write all these lines into one file row?

Concatenations two needed character variables isn't ok in  my case becouse they summury length can be more then 32767...

Thanks!

PROC Star
Posts: 7,467

Re: Write to file from data step.

Yura,

I agree that the solution Tom offered is definitely the easiest one.  As for your latest question, just use an @ symbol in your put statement.  e.g.,

put "txt1" @;

put "txt2";

Regular Contributor
Posts: 161

Re: Write to file from data step.

Hi again Arthur,

It also works.

Thanks a lot for help guys!

🔒 This topic is solved and locked.

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

Discussion stats
  • 10 replies
  • 440 views
  • 6 likes
  • 4 in conversation