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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5239 views
  • 2 likes
  • 3 in conversation