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

I'm trying to read multiple files into a dataset but I'm struggling to find the "best" way to do this.  I know that you can't use macro variables inside a datalines statement but I want to be able to specify the directory from which I am reading at the top.  This example illustrated what I am trying to do.  Can anyone suggest how I might do this?

%Let srcdir=c:\foo;

%Let basename=bar;

Data One;

   Infile Datalines;

   Length EggFile $152;

   Input EggFile $;

   Infile dummy FileVar=EggFile end=done;

   Do While(not done);

      Input this 1-2 that 4;

      Output;

   End;

   Datalines;

      "&srcdir\&basename.27.Dat"

      "&srcdir\&basename.28.Dat"

      "&srcdir\&basename.29.Dat"

      "&srcdir\&basename.30.Dat"

      "&srcdir\&basename.31.Dat"

      "&srcdir\&basename.32.Dat"

      "&srcdir\&basename.33.Dat"

      "&srcdir\&basename.34.Dat"

Run;

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

If this is a specific list of files, why not take them out of the DATALINEs statement and put them in a FILENAME statement?  Something like:

filename mylist ("&srcdir\&basename.27.Dat","&srcdir\&basename.28.Dat"

      ,"&srcdir\&basename.29.Dat","&srcdir\&basename.30.Dat"

      ,"&srcdir\&basename.31.Dat","&srcdir\&basename.32.Dat"

      ,"&srcdir\&basename.33.Dat","&srcdir\&basename.34.Dat");

data want;

  infile mylist;

  input this 1-2  that 4;

run;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
mkeintz
PROC Star

If this is a specific list of files, why not take them out of the DATALINEs statement and put them in a FILENAME statement?  Something like:

filename mylist ("&srcdir\&basename.27.Dat","&srcdir\&basename.28.Dat"

      ,"&srcdir\&basename.29.Dat","&srcdir\&basename.30.Dat"

      ,"&srcdir\&basename.31.Dat","&srcdir\&basename.32.Dat"

      ,"&srcdir\&basename.33.Dat","&srcdir\&basename.34.Dat");

data want;

  infile mylist;

  input this 1-2  that 4;

run;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

If you want to read all of the DAT files in one directory

filename mylist "&srcdir.\*.dat";

Ksharp
Super User

You don't need to put macro into DATALINES actually.

%Let srcdir=c:\foo;

%Let basename=bar;

Data One;

   Infile Datalines;

   Length EggFile $152;

   Input EggFile $;

  EggFile="&srcdir"||"\"||"&basename"||EggFile ;

   Infile dummy FileVar=EggFile end=done;

   Do While(not done);

      Input this 1-2 that 4;

      Output;

   End;

   Datalines;

      27.Dat

     28.Dat

  ;

Run;

Ksharp

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
  • 3 replies
  • 950 views
  • 0 likes
  • 4 in conversation