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

Hello,

 

My problem seems simple but I am quite new to SAS so I would appreciate some help.

I need to create a dataset from several CSV files that appear in a directory. It seems that it is easy to use a command data-infile-input to do that. However, the problem is that I have around 90 variables (columns) so writing each one of those next to the input statement seems quite inefficient.

 

Is there any other way to append all these csv files and keep all the variables available?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
sh0e
Obsidian | Level 7

Although writing out the full INPUT statement may seem 'inefficient', it is really best practice because it gives you full control. Also, you may be able to generate the INPUT statement if you have the field names available in electronic form.

 

The lazy, er, efficient approach will be to use PROC IMPORT as 'CSV' is one of the available DBMS options.

 

Hope this sends you on the right solution path.

View solution in original post

2 REPLIES 2
sh0e
Obsidian | Level 7

Although writing out the full INPUT statement may seem 'inefficient', it is really best practice because it gives you full control. Also, you may be able to generate the INPUT statement if you have the field names available in electronic form.

 

The lazy, er, efficient approach will be to use PROC IMPORT as 'CSV' is one of the available DBMS options.

 

Hope this sends you on the right solution path.

Tom
Super User Tom
Super User

If you have metadata on the files (note that CSV files have no place to store metadata, at best the CSV might have column names in the first row) then you can easily use it to generate the code to read the files.

 

If you have no information on what is in the files you can use PROC IMPORT and SAS will make an intellegent guess as to what type of fields you have basd on the data in the file and generate a data step to read the file.

 

If the files are all in the same format you can read them all in a single data step if you want.  If you don't care what the variable names are you can just name them something like VAR1 to VAR90.

 

data want ;
   infile 'my_directory/*.csv' dsd firstobs=2 truncover eov=eov;
   input @;
   if eov then input;
   eov=0;

   length var1-var90 $100 ;
   input var1-var90 ;
run;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 1253 views
  • 1 like
  • 3 in conversation