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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

13 REPLIES 13
Reeza
Super User

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;

m1986MM
Obsidian | Level 7

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,

Reeza
Super User

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;

Babloo
Rhodochrosite | Level 12

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

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

jakarman
Barite | Level 11

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 --<-----
Reeza
Super User

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

m1986MM
Obsidian | Level 7

Thanks.

venkatnaveen
Obsidian | Level 7

Can u send the code to my mail id:

venkatnaveenmnba@gmail.com

im not getting explanation

venkatnaveen
Obsidian | Level 7

If its in Enterprise guide 4.3 or DI Studio 4.21,

how to import?

Regards,

Naveen

m1986MM
Obsidian | Level 7

Hi Naveen,

Which code do you mean?

venkatnaveen
Obsidian | Level 7

SASCode for importing files

Reeza
Super User

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?

venkatnaveen
Obsidian | Level 7

Thanks.

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
  • 13 replies
  • 4325 views
  • 6 likes
  • 5 in conversation