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

Hi,

 

I'm trying to import multiple TXT files from a local directory that are delimited by the character '$'. The files each have different headers and I want to output them into my work directory as separate data sets.

 

proc import datafile='C:\Directory\*.txt' dbms=dlm out=work.file_name;
delimiter='$';
run;

Is there a command line for the 'out=' portion of the code?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
singhsahab
Lapis Lazuli | Level 10

Hello Dear,

 

This can be accomplish by using Macro . Here you can go ...

 

%let path=C:\Directory;

%MACRO IMPORT_TXT(FILENAME=,OUTFILENAME=);
proc import datafile="&path.\&FILENAME..txt" dbms=dlm out=work.&OUTFILENAME replace;
delimiter='$';
run;
%MEND;

/* Calling Macro to import First file  */
%IMPORT_TXT(FILENAME=test1,OUTFILENAME=test1);

/* Calling Macro to import First file  */
%IMPORT_TXT(FILENAME=test2,OUTFILENAME=test2);

Just you have to call macro by passing parameters.

 

Thanks..

View solution in original post

5 REPLIES 5
singhsahab
Lapis Lazuli | Level 10

Hello Dear,

 

This can be accomplish by using Macro . Here you can go ...

 

%let path=C:\Directory;

%MACRO IMPORT_TXT(FILENAME=,OUTFILENAME=);
proc import datafile="&path.\&FILENAME..txt" dbms=dlm out=work.&OUTFILENAME replace;
delimiter='$';
run;
%MEND;

/* Calling Macro to import First file  */
%IMPORT_TXT(FILENAME=test1,OUTFILENAME=test1);

/* Calling Macro to import First file  */
%IMPORT_TXT(FILENAME=test2,OUTFILENAME=test2);

Just you have to call macro by passing parameters.

 

Thanks..

mnguye17
Fluorite | Level 6

Thanks, I appreciate the effort!

Reeza
Super User

Here's a fully fledged macro. Point it to the folder and specify the extension it imports all the files. If your file names will not be valid SAS data set names you'll have to modify it to account for that. 

 

https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n0ctmldxf23ixtn1kqsoh5bsgmg8.htm&docse...

 

Several other useful macro examples are here:

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

 

 


@mnguye17 wrote:

Hi,

 

I'm trying to import multiple TXT files from a local directory that are delimited by the character '$'. The files each have different headers and I want to output them into my work directory as separate data sets.

 

proc import datafile='C:\Directory\*.txt' dbms=dlm out=work.file_name;
delimiter='$';
run;

Is there a command line for the 'out=' portion of the code?

 

Thanks


 

mnguye17
Fluorite | Level 6

So, I was able to run the code and it imported all my text files from the directory as individual data sets like I originally wanted. But, I'm having trouble retaining the name of the original text file in the imported data set. It simply renames to Dsn# due to this line of code

          proc import datafile="&dir\%qsysfunc(dread(&did,&i))" out=dsn&cnt 

Is there a way to retain the name of the original name from the text file in the out=dsn&cnt portion?

Tom
Super User Tom
Super User

You cannot put it into the dataset as a variable. Not using PROC IMPORT. 

 

Put you could put it into the dataset label.

proc import datafile="&dir\%qsysfunc(dread(&did,&i))"
 out=dsn&cnt (label="&dir\%qsysfunc(dread(&did,&i))")

 

If the names of the files are like XXX.txt then you might be able to parse out the XXX and use that as the name.  But then your program will break if the XXX part is not valid as a dataset name.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 5815 views
  • 4 likes
  • 4 in conversation