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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Vince28_Statcan
Quartz | Level 8

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

3 REPLIES 3
jaredp
Quartz | Level 8

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

Vince28_Statcan
Quartz | Level 8

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

jaredp
Quartz | Level 8

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 1360 views
  • 1 like
  • 2 in conversation