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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

5 REPLIES 5
Tom
Super User Tom
Super User

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;

cloudforest
Calcite | Level 5

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.

art297
Opal | Level 21

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;

cloudforest
Calcite | Level 5

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

Peter_C
Rhodochrosite | Level 12

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

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!

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
  • 6421 views
  • 3 likes
  • 4 in conversation