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!
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;
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;
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.
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;
It works! Thank you both so much for your help!
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
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.