I have tried double quotes, single quotes and ampersand.
I have the following local variable:
setUpDirFilename = '/uds/data/newdata/20102011/SUM/'||setUpInstDir||'/'setUpFilename;
When I display the contents of this variable I see '/uds/data/newdata/20102011/SUM/I110/110sum1011'. Have tried to use this variable in a FILE statement so that I can write records out to it. No luck no matter what I have tried. Please help.
Thank you!
JackieO
Ok, so variables are not available outside of the data step.
You're mixing data step logic from a data set here and macro logic for one. Can you please post your full code within the data step? I'm assuming you're trying to create multiple filenames? Is this to read in or export files out?
There are two ways to create a filename, note you're missing the fileref, myFile.
filename myFile 'path to file';
If you can explain a bit more about what you're trying to do, I can suggest the correct approach.
Just based on your shorter post, not on the really long one ...
You sort of created a macro variable. It's name is FILENAME and its value is setUpDirFilename.
The %LET statement is not part of the DATA step, and so the value assigned is the actual set of characters "setUpDirFilename".
In a DATA step, if you want to transfer the value of the DATA step variable setUpDirFilename to the macro variable FILENAME, use a different tool:
call symputx('filename', setUpDirFilename);
When referring to &filename after that DATA step, do not enclose it in single quotes. If you need quotes, use double quotes.
One of the examples you have shown is correct:
file "&filename";
Ok. Here's an example of writing records to a data set within a data step that creates CSV files.
This writes a single record to each file with the folder having the persons name and the file also having the person's name.
%let parent_path = C:\_localdata\temp;
data demo;
*data set to be exported;
set sashelp.class;
*first create directory for folder, assume it doesn't exist;
rc = dcreate(name, "&parent_path");
*create path to file with file name;
out_file_name = catx('\', "&parent_path", name, catt(name, '.csv'));
*file declaration, note fileVAR uses variable created in the data step;
file out filevar=out_file_name dlm=',';
*write header row;
put 'name, age, sex, height, weight';
*write detail row;
put name age sex height weight;
run;
And here's a second, more realistic scenario - create a file for each Sex individually in a folder called M/F with teh file called M/F respectively.
%let parent_path = C:\_localdata\temp;
proc sort data=sashelp.class out=class;
by sex;
run;
data demo;
*data set to be exported;
set class;
by sex;
*first create directory for folder, assume it doesn't exist;
rc = dcreate(sex, "&parent_path");
*create path to file with file name;
out_file_name = catx('\', "&parent_path", sex, catt(sex, '.csv'));
*file declaration, note fileVAR uses variable created in the data step;
file out filevar=out_file_name dlm=',';
*write header row;
if first.sex then
put 'name, age, sex, height, weight';
*write detail row;
put name age sex height weight;
run;
I hope that helps clarify what you need to do. Note that in both of these I create a data set, called demo. You can change it to a data _null_ step, once you have it working but I like to leave it as is when testing.
@JackieO wrote:
Hi Reeza!
I append all the master files for a given academic year, semester and record format (we have S, E, M, D , L and P). So the beginning of the code that I sent to you was appending
files from 20102011 SUMMER RECORD S. This is how our edit application processes multiple record formats together. The SSN correction table contains multiple
institutions. I've tried to "flag" the institutions in the correction table and sort them in with the other two files for the DATA step. The written external files
should be "driven" by the institution flag. The output files should be written out to a similar directory structure so that the original master files are kept safe. Long story short...
you are correct.
I looked at Example 5. So this is done outside of a DATA step? I've never done that. I'm really a dinosaur I guess, but I am eager to learn new things. I will play around
with this and do some Google searching. Please don't give up on me. 🙂
JackieO
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.