Convert dataset to txt file

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Convert dataset to txt file

I am trying to convert a sas data set to a .txt file.  This seems to be fairly straightforward if I wanted to convert the set into a single text file, but I need each observation to be exported to its own.txt file and have data from two of the variables being the .txt file names.

So if here is my data set:

variable 1       variable 2     variable 3

1                       a                  x

2                       b                  y

3                       c                  z

I want to create 3 text files, 1_a.txt, 2_b.txt and 3_c.txt, containing x, y, z in the text file, respectably.

I've heard this can be done with a sas macro, but I am not familiar with how to write macros and don't know where to start. Any help would be appreciated!


Thanks!


Accepted Solutions
Solution
‎12-20-2012 06:08 PM
PROC Star
Posts: 7,477

Re: Convert dataset to txt file

Posted in reply to cloudforest

I would guess that it was simply how you defined it.  The following works for me and should for you:

data _null_;

  set sashelp.class ;

  _filename=cats("c:\art\",name,"_",age)||'.txt';

  file dummy filevar=_filename ;

  put variable3 ;

run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,060

Re: Convert dataset to txt file

Posted in reply to cloudforest

Use the FILEVAR option on the FILE statement and you can do it in a single data step without any macro code.

data _null_;

  set have ;

  _filename=catx('_',variable1,variable2)||'.txt';

  file dummy filevar=_filename ;

  put variable3 ;

run;

Occasional Contributor
Posts: 6

Re: Convert dataset to txt file

Thanks for your help!

Using this code, how can I specify the directory I want the txt files to be exported to? When I tried to insert a path to a folder after the file option, I get the error message "A Physical file reference (ie PHYSICAL FILE REFERENCE") or an aggregate file storage reference (ie AGGREGATE(MEMBER)) reference cannot be used with the FILEVAR=option.

Solution
‎12-20-2012 06:08 PM
PROC Star
Posts: 7,477

Re: Convert dataset to txt file

Posted in reply to cloudforest

I would guess that it was simply how you defined it.  The following works for me and should for you:

data _null_;

  set sashelp.class ;

  _filename=cats("c:\art\",name,"_",age)||'.txt';

  file dummy filevar=_filename ;

  put variable3 ;

run;

Occasional Contributor
Posts: 6

Re: Convert dataset to txt file

It works! Thank you both so much for your help!

Valued Guide
Posts: 2,177

Re: Convert dataset to txt file

Posted in reply to cloudforest

beware if your data step might write to the same file more than once and the data are not in "filevar" order.

I found the file seemed to be closed when the filevar value changed.

A second write to the file over-wrote the first, if the data are not in "filevar" order.

but here for CLOUDFOREST, it should not be a problem because only one row should be written to each file

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 2141 views
  • 3 likes
  • 4 in conversation