I have hundreds of comma delimited text files that have two columns and hundreds of thousands of rows, Each file is named after the participant (e.g., 1, 3, 15, etc...). What I need is two-fold, and both may not be possible in the same step.
I'd like to:
1) batch import each file and
2) create a new variable based on each file name. So, I'd like a variable called "subj" that has the value 1 in every row for file 1, and so on...
I've searched around and can't find any information on creating a variable from the import filename. Any advice?
Thanks!
Do you want all this data combined into one dataset at the end (which if the data is the same would be the best method):
data want (keep=fname a b); infile ".../*.csv" filename=fname; input a b; run;
If you want separate ones then, something like:
filename tmp pipe 'dir ".../*.csv" /b'; data _null_; infile tmp dlm="¬"; call execute(cats('data want',put(_n_,best.),'; infile "',_infile_,'"; input a b; run;')); run;
This would create wantX with X being incremental for each file - mainly to show how to do it.
FILEVAR option on the INFILE statement. See example 5 in the documentation.
And older walk through
https://support.sas.com/techsup/technote/ts581.pdf
Another option:
@dsm wrote:
I have hundreds of comma delimited text files that have two columns and hundreds of thousands of rows, Each file is named after the participant (e.g., 1, 3, 15, etc...). What I need is two-fold, and both may not be possible in the same step.
I'd like to:
1) batch import each file and
2) create a new variable based on each file name. So, I'd like a variable called "subj" that has the value 1 in every row for file 1, and so on...
I've searched around and can't find any information on creating a variable from the import filename. Any advice?
Thanks!
Do you want all this data combined into one dataset at the end (which if the data is the same would be the best method):
data want (keep=fname a b); infile ".../*.csv" filename=fname; input a b; run;
If you want separate ones then, something like:
filename tmp pipe 'dir ".../*.csv" /b'; data _null_; infile tmp dlm="¬"; call execute(cats('data want',put(_n_,best.),'; infile "',_infile_,'"; input a b; run;')); run;
This would create wantX with X being incremental for each file - mainly to show how to do it.
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.