Hello everyone,
I have 180 delimited datasets and want to import them. These files are named monthly. For example: Stockprice_1998_01, Stockprice_1999_02, ... , Stockprice_2014_06.
My question is that how I can write a macro statement or even a do loop to import all the datasets and not write the proc import statement for each dataset.
Any suggestion would be greatly appreciated.
Thanks
Change the area's in red to match your directory and files structure.
data try01;
length filename txt_file_name $256;
retain txt_file_name;
*import all files in the directory path that has the name month_*.txt where the * is wildcard operator to imply any file starting with month_ and ends in .txt;
infile "Path\monthly_*.txt" eov=eov filename=filename truncover;
input@;
*This piece of code is used to extract the file name so you know where each record comes from;
if _n_ eq 1 or eov then do;
txt_file_name = scan(filename, -2, ".\");
eov=0;
end;
*Input statement here is used to input the dataset, which must be the same across all files;
input
*Place input code here;
;
run;
If all the files have the same structure then use a wild card statement:
data try01;
length filename txt_file_name $256;
retain txt_file_name;
infile "Path\*.txt" eov=eov filename=filename truncover;
input@;
if _n_ eq 1 or eov then do;
txt_file_name = scan(filename, -2, ".\");
eov=0;
end;
input
*Place input code here;
;
run;
Thanks Reeza. Yes, my datasets have exactly same variables with the sam length and same format. But I didn't understand how I can write the import statement once and run it for all the datasets, named monthly. Would you please explain it more?
Thanks,
Change the area's in red to match your directory and files structure.
data try01;
length filename txt_file_name $256;
retain txt_file_name;
*import all files in the directory path that has the name month_*.txt where the * is wildcard operator to imply any file starting with month_ and ends in .txt;
infile "Path\monthly_*.txt" eov=eov filename=filename truncover;
input@;
*This piece of code is used to extract the file name so you know where each record comes from;
if _n_ eq 1 or eov then do;
txt_file_name = scan(filename, -2, ".\");
eov=0;
end;
*Input statement here is used to input the dataset, which must be the same across all files;
input
*Place input code here;
;
run;
please explain your scan function as I couldn't understand the usage of ".\"
txt_file_name = scan(filename, -2, ".\");
us a filename to your directory with all those files.
filename infils "&path" ;
Then use:
data ..... ;
length filename $256 ;
infile infils("monthly_*.txt") filename=filename ... ; .
SAS(R) 9.4 Statements: Reference, Third Edition (infile stament) . When you want to have the membername use the last word of that long long name separated by the folder separation that is \ at windows / at Unix. As babloo is running at Unix, Reeza at Windows turn that slash.
RTM - Scan function scans the file path for only the file name, using the delimiters of period and slash. If you're using unix as Jaap indicates change the sign of the slash.
Thanks.
If its in Enterprise guide 4.3 or DI Studio 4.21,
how to import?
Regards,
Naveen
Hi Naveen,
Which code do you mean?
SASCode for importing files
You should ask your own question rather than post here as your question is not related.
The code for importing was provided above.
Please post your own question and expand on what you need in a single discussion
Enterprise Guide and SAS DI have different purposes and ways of importing. Are you importing text files? excel files? Are there multiple files or a single file? What exactly is the issue?
In EG You can use the Import Task, why not use that?
Thanks.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.