Hello I am trying to import csv files in my work library.
%let directory = E:\saved ;
filename files pipe "dir /b &directory\*.csv";
data file_list;
length file_name $40;
infile files length=reclen;
input file_name $varying40. reclen;
run;
%macro csv;
proc sql noprint;
select count(*) into :vol_of_files
from work.file_list;
quit;
%do i=1 %to &vol_of_files.;
data work.file_list_v2;
set work.file_list (firstobs = &i.);
run;
proc sql outobs=1;
create table work.file_list_v3 as
select *
from work.file_list_v2;
quit;
proc sql;
select * into :file_name
from work.file_list_v3;
quit;
data file_list_v4 (keep=file_name_new);
set file_list_v3;
file_name_new = reverse(substr(reverse(&file_name.),5));
run;
proc sql;
select * into :file_name_v2
from work.file_list_v4;
quit;
proc import out=&file_name_v2.
datafile="&directory.\&file_name."
dbms=csv replace;
getnames=yes;
run;
%end;
%mend csv;
%csv
The idea that I had, was to store the file names into a table file_list as values of a variable file_name.
Later I find the length of the table, and I want to iterate thought the values of this column. My logic gives an error and I am not sure.
Any suggestions?
Thanks
When you encounter an ERROR, ALWAYS post the log, using the {i} button.
My bad. I thought I had done so.
Thanks for mentioning this.
You do not create macro variable file_name before you first use it (the select into that creates it follows later).
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.