DATA Step, Macro, Functions and more

How to import multiple datasets using macro

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

How to import multiple datasets using macro

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


Accepted Solutions
Solution
‎11-20-2014 08:25 PM
Super User
Posts: 19,851

Re: How to import multiple datasets using macro

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;

View solution in original post


All Replies
Super User
Posts: 19,851

Re: How to import multiple datasets using macro

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;

Contributor
Posts: 65

Re: How to import multiple datasets using macro

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,

Solution
‎11-20-2014 08:25 PM
Super User
Posts: 19,851

Re: How to import multiple datasets using macro

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;

Super Contributor
Posts: 436

Re: How to import multiple datasets using macro

please explain your scan function as I couldn't understand the usage of ".\"

txt_file_name = scan(filename, -2, ".\");

Trusted Advisor
Posts: 3,215

Re: How to import multiple datasets using macro

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.  

---->-- ja karman --<-----
Super User
Posts: 19,851

Re: How to import multiple datasets using macro

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.

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Contributor
Posts: 65

Re: How to import multiple datasets using macro

Thanks.

Frequent Contributor
Posts: 89

Re: How to import multiple datasets using macro

Can u send the code to my mail id:

venkatnaveenmnba@gmail.com

im not getting explanation

Frequent Contributor
Posts: 89

Re: How to import multiple datasets using macro

If its in Enterprise guide 4.3 or DI Studio 4.21,

how to import?

Regards,

Naveen

Contributor
Posts: 65

Re: How to import multiple datasets using macro

Posted in reply to venkatnaveen

Hi Naveen,

Which code do you mean?

Frequent Contributor
Posts: 89

Re: How to import multiple datasets using macro

SASCode for importing files

Super User
Posts: 19,851

Re: How to import multiple datasets using macro

Posted in reply to venkatnaveen

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?

Frequent Contributor
Posts: 89

Re: How to import multiple datasets using macro

Thanks.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 13 replies
  • 1389 views
  • 6 likes
  • 5 in conversation