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"
(I also just noticed that fname seems to have some blank spaces...)
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
Sorry, my put statement should read "put paragraph"....
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
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.