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

Hi,

 

I try to 

a) create a dataset of non consecutive .csv files in a certain folder

b) loop through this dataset and load each file into a master dataset

c) add one column to each row that contains the origin (filename) of that row 

 

Step (a) works like this:

 

%let servername=\\some\folder\;
libname out "&servername.Users\xxxx";
Filename filelist pipe "dir /b \\some\other\folder\*.txt";

Data out.filelist;
Infile filelist truncover;
Input filename $120.;
Run;

 

 

Step (b) however keeps not using the variable for the filename: 

 

 

data out.completeset_total;
set out.filelist;
do i=1 to 3;
filepath="&servername"||filename;
call symput(fpath,TRIM(filepath));
call symput(fname,TRIM(filename));
put fpath;
filename abc3 "&servername.more\sub\folders\&fpath..txt";
infile abc3 truncover lrecl=1001 firstobs=2 ; input
@1 abc_RS BEST1.
@2 abc_anothercol BEST4.
&fpath. 
end;
run;

Question: How can I make the filename statement loop through the variable fpath correctly? 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

If they're all in the same folder and the same layout you can use the approach outlined here that does everything you were trying to do:

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

 

Or the INFILE statement has examples of keeping the file name used and/or reading from multiple files using the FILEVAR option and the FILENAME option to keep the name of the file.

 

See Example 5:

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


@GijsTerBeek wrote:

Hi,

 

I try to 

a) create a dataset of non consecutive .csv files in a certain folder

b) loop through this dataset and load each file into a master dataset

c) add one column to each row that contains the origin (filename) of that row 

 

Step (a) works like this:

 

%let servername=\\some\folder\;
libname out "&servername.Users\xxxx";
Filename filelist pipe "dir /b \\some\other\folder\*.txt";

Data out.filelist;
Infile filelist truncover;
Input filename $120.;
Run;

 

 

Step (b) however keeps not using the variable for the filename: 

 

 

data out.completeset_total;
set out.filelist;
do i=1 to 3;
filepath="&servername"||filename;
call symput(fpath,TRIM(filepath));
call symput(fname,TRIM(filename));
put fpath;
filename abc3 "&servername.more\sub\folders\&fpath..txt";
infile abc3 truncover lrecl=1001 firstobs=2 ; input
@1 abc_RS BEST1.
@2 abc_anothercol BEST4.
&fpath. 
end;
run;

Question: How can I make the filename statement loop through the variable fpath correctly? 

 

 

 


 

View solution in original post

1 REPLY 1
Reeza
Super User

If they're all in the same folder and the same layout you can use the approach outlined here that does everything you were trying to do:

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

 

Or the INFILE statement has examples of keeping the file name used and/or reading from multiple files using the FILEVAR option and the FILENAME option to keep the name of the file.

 

See Example 5:

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


@GijsTerBeek wrote:

Hi,

 

I try to 

a) create a dataset of non consecutive .csv files in a certain folder

b) loop through this dataset and load each file into a master dataset

c) add one column to each row that contains the origin (filename) of that row 

 

Step (a) works like this:

 

%let servername=\\some\folder\;
libname out "&servername.Users\xxxx";
Filename filelist pipe "dir /b \\some\other\folder\*.txt";

Data out.filelist;
Infile filelist truncover;
Input filename $120.;
Run;

 

 

Step (b) however keeps not using the variable for the filename: 

 

 

data out.completeset_total;
set out.filelist;
do i=1 to 3;
filepath="&servername"||filename;
call symput(fpath,TRIM(filepath));
call symput(fname,TRIM(filename));
put fpath;
filename abc3 "&servername.more\sub\folders\&fpath..txt";
infile abc3 truncover lrecl=1001 firstobs=2 ; input
@1 abc_RS BEST1.
@2 abc_anothercol BEST4.
&fpath. 
end;
run;

Question: How can I make the filename statement loop through the variable fpath correctly? 

 

 

 


 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 7765 views
  • 1 like
  • 2 in conversation