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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

10 REPLIES 10
art297
Opal | Level 21

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;

Yura2301
Quartz | Level 8

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

Astounding
PROC Star

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.

Yura2301
Quartz | Level 8

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!

Yura2301
Quartz | Level 8

Hi Arthur,

Thanks, it's what I need!

art297
Opal | Level 21

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

;

Tom
Super User Tom
Super User

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

Yura2301
Quartz | Level 8

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!

art297
Opal | Level 21

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

Yura2301
Quartz | Level 8

Hi again Arthur,

It also works.

Thanks a lot for help guys!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 10 replies
  • 1470 views
  • 6 likes
  • 4 in conversation