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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

View solution in original post

3 REPLIES 3
Reeza
Super User

FILEVAR option on the INFILE statement. See example 5 in the documentation. 

 

http://documentation.sas.com/?docsetId=lestmtsref&docsetTarget=n1rill4udj0tfun1fvce3j401plo.htm&docs...

 

And older walk through

https://support.sas.com/techsup/technote/ts581.pdf

 

 

Another option:

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

 


@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!


 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

dsm
Calcite | Level 5 dsm
Calcite | Level 5
super helpful, thanks so much!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 6215 views
  • 2 likes
  • 3 in conversation