BookmarkSubscribeRSS Feed
sindhura1
Calcite | Level 5

There are multiple files in my unix folder(csv,txt).I want to convert them all into sas datasets through SAS EG.

 

Could someone help with this please?

8 REPLIES 8
Kurt_Bremser
Super User

You can run PROC IMPORT on them, but be advised that this involves guessing and is therefore unreliable.

If the files share the same structure, you need to run the same data step on them to get identical (in terms of variable attributes) results.

sindhura1
Calcite | Level 5
I am able to import one file using proc import
But how to import series of files?
Kurt_Bremser
Super User

Please be more specific. Are the datasets resulting from the import meant to be appended (same structure), or do they share common key columns which need to be used later on in joins, or are they completely independent and have no relationship at all?

 

A list of files in a folder is easily done, either by running the UNIX ls command in a FILENAME PIPE, or by using the FILENAME, DOPEN, DNUM and DREAD functions in a data step.

Search for "list files in directory" here on the communities, as this question has been asked and answered numerous times.

sindhura1
Calcite | Level 5
The files are independent of each other.They need not be appended. they have to be converted into individual datasets
Tom
Super User Tom
Super User

Use EG to help you write some SAS code to read the files.  

Do you know what is in the files?  If so just write a data step to read them.

If you don't know are you ok with SAS guessing how to define the variables?  If so just use PROC IMPORT.

sindhura1
Calcite | Level 5
yes i am able to do proc import with single file.But unable to do with multiple files
Tom
Super User Tom
Super User

@sindhura1 wrote:
yes i am able to do proc import with single file.But unable to do with multiple files

Get the list of files:

data files;
  input 'ls /myfolder/*.csv' pipe truncover;
  input filename $256. ;
run;

Then generate code to convert each one.  Here is a simple process, but you might need to make it more complicated if the base names of your files are not valid SAS member names (or not unique).

data _null_;
  set files;
  call execute(catx(' '
    ,'proc import datafile=',quote(trim(filename))
    ,'dbms=csv'
    ,'out=',scan(filename,-2,'/.'),'replace'
    ,';'
    ,'run;'
  ));
run;
Reeza
Super User
I generally don't recommend this approach at all but here's one way.
I guarantee it's infinitely better to go through the files, make sure variables are coded correctly otherwise you run into issues later on.

This was designed for Excel but you can easily change to be for CSV.
https://github.com/statgeek/SAS-Tutorials/blob/master/Import_all_files_one_type

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 8 replies
  • 7101 views
  • 2 likes
  • 4 in conversation