The SAS Output Delivery System and reporting techniques

Per observation text file export

Accepted Solution Solved
Reply
Contributor
Posts: 71
Accepted Solution

Per observation text file export

I'm hoping someone can help me out.  I need to export a single variable from each observation into it's own unique text file whose filename is comprised of 2 variables. My data is a bunch of character variables like the one below.  I've simplified it a lot.  In my real dataset, the paragraph variable is actually a bunch of text.

data have;

    input id $ paragraph $ group $;

    datalines;

1 cA1 A

2 cA2 A

3 cA3 A

1 cB1 B

2 cB2 B

3 cB3 B

1 cC1 C

2 cC2 C

3 cC3 C

;

run;

The combination of the variables ID and GROUP are unique.  I'd like this to be the resulting text filename.  The content of each file is the paragraph variable.

In the above dataset, I should end up with 9 files (A1.txt with content cA1, A2.txt with content cA2, etc...).  (Whether or not the file ends in .txt does not matter).

I'm on WinXP and was playing with something like the following:

filename cc 'J:\SAS_PROGRAMS\STATISTICS FRAMEWORK\projects\Camper Satisfaction Sentiment Analysis\_test\name';

data _null_; set have;

length fname $250;

fname = "J:\SAS_PROGRAMS\STATISTICS FRAMEWORK\projects\Camper Satisfaction Sentiment Analysis\_test\" || TRIM(fname);

    fname = id || group;

    file cc filevar=fname;

    put comment;

run;

Removing the filevar option will make this write all obs to "name" text file.  The filevar= option seems to do what I want BUT my computer is locked down and I can't write to the default path.  I get this error: "Insufficient authorization to access C:\Program Files\SASHome\SASFoundation\9.3\1"

  • Is there a way to change the path of where the file is stored when using the filevar option? (X command using chdir doesn't seem to work)
  • Alternatively, is there a different approach?  A DO-loop and text-file-writing-macro ?  My data is about 6,000 observation so I am sort of concerned about performance (which is likely more an issue of writing that many files to a single Windows directory...)

(I also just noticed that fname seems to have some blank spaces...)


Accepted Solutions
Solution
‎08-13-2013 12:18 PM
Super Contributor
Posts: 339

Re: Per observation text file export

ordering matters. The value of fname when file cc filevar=fname occurs is A1 B2 etc. and not a full path extension which is required by the filevar= option as specificed in SAS documentation:

FILEVAR=variable

defines a variable whose change in value causes the
FILE statement to close the current output file and open a new one the next time
the FILE statement executes. The next PUT statement that executes writes to the
new file that is specified as the value of the FILEVAR= variable.

Restriction:The value of a FILEVAR=
variable is expressed as a character string that contains a physical filename.

Here is an example that should solve your issue

%let path=J:\SAS_PROGRAMS\STATISTICS FRAMEWORK\projects\Camper Satisfaction Sentiment Analysis\_test\;

data _null_;

     set have;

     fname="&path"||trim(group)||trim(id)||".txt";

     file cc filevar=fname;

     put paragraph;

run;

Vince

View solution in original post


All Replies
Contributor
Posts: 71

Re: Per observation text file export

Sorry, my put statement should read "put paragraph"....

Solution
‎08-13-2013 12:18 PM
Super Contributor
Posts: 339

Re: Per observation text file export

ordering matters. The value of fname when file cc filevar=fname occurs is A1 B2 etc. and not a full path extension which is required by the filevar= option as specificed in SAS documentation:

FILEVAR=variable

defines a variable whose change in value causes the
FILE statement to close the current output file and open a new one the next time
the FILE statement executes. The next PUT statement that executes writes to the
new file that is specified as the value of the FILEVAR= variable.

Restriction:The value of a FILEVAR=
variable is expressed as a character string that contains a physical filename.

Here is an example that should solve your issue

%let path=J:\SAS_PROGRAMS\STATISTICS FRAMEWORK\projects\Camper Satisfaction Sentiment Analysis\_test\;

data _null_;

     set have;

     fname="&path"||trim(group)||trim(id)||".txt";

     file cc filevar=fname;

     put paragraph;

run;

Vince

Contributor
Posts: 71

Re: Per observation text file export

Thank you so much.  That did the trick!

I was originally trying to work with a Do loop and macro, but then came across the Filevar option.  For some reason, I couldn't quite understand if filevar required a path or just the file name.  Thanks for clarifying it!

Jared

Post a Question
Discussion Stats
  • 3 replies
  • 436 views
  • 1 like
  • 2 in conversation