BookmarkSubscribeRSS Feed
WesBarris
Obsidian | Level 7

I am creating a macro to read a series of .dbf files.  I have a dataset that contains the filenames I want to read.  Here is the code I am trying:

   data test;

      set infilesPlus;

      dbfPath = "&directory\" || file;

      proc import out=value datafile="dbfPath"

         dbms=dbf replace;

      getdeleted=no;

      run;

   end;

The problem I am having is that the proc import function is treating "dbfPath" as a literal string instead of a variable containing a path to a .dbf file.

I have verified that the variable "dbfPath" contains valid paths to files (such as t:\L72mg61\7261G1O.DBF).  What is the trick to make "dbfPath"

be recognized as a variable containing a file instead of a string?

5 REPLIES 5
art297
Opal | Level 21

I don't think you can run a proc, as such, from within a datastep.  You could wrap the proc import within a macro and simply call the macro passing dbfPath into it, or use call execute.

Art

dhana
Fluorite | Level 6

Hi

As Art told we can't use PROCs inside DATA Step. But here is one possible solution.

DATA _NULL_;

  SET DSN END=EOF;

  CALL SYMPUT('FILE'||LEFT(_N_),"&directory"||DBFILE);

  IF EOF THEN CALL SYMPUT('NUM',_N_);

RUN;

%MACRO IMPORT;

   %DO I=1 %TO #

      PROC IMPORT OUT=DSN&I.

        DATAFILE=&&FILE&I.

        DBMS=DBF REPLACE;

      RUN;

   %END;

%MEND IMPORT;

%IMPORT;

Hope this helps.

Thanks

Dhanasekaran R     

Tom
Super User Tom
Super User

You will need to use the data to generate the code for the PROC IMPORT steps.  You could use macro language or you could just use the DATA step to generate the code and then %INCLUDE the code.

You will need to import each file to a different dataset otherwise only the last one will exist.  You also might want to add code to combine them if they are mulitple instances of the same type of data.

filename code temp;

data _null_;

   set infilesPlus;

   file code;

   fileno +1 ;

   put

      'proc import dbms=dbf replace out=value' fileno Z3. ' datafile="&directory\' file +(-1) '";'

    / '  getdeleted=no;'

    / 'run;'

  ;

run;

%inc code / source2 ;

DF
Fluorite | Level 6 DF
Fluorite | Level 6

It might be worth trying the approaches outlined here if possible: http://communities.sas.com/thread/30161 - KSharp and Null give examples of importing multiple files using a single data step.

If you can't adapt that, then perhaps you could use Call Execute to run each version of proc import after your data step completes?

For example (not tested), something like:

data test;

    set infilesPlus;

    dbfPath = "&directory\" || file;

    call execute('proc import out=' & trim(value) & ' datafile="' & trim(dbfPath) & '" dbms=dbf replace;';

    getdeleted=no;

run;

Message was edited by: DF

DF
Fluorite | Level 6 DF
Fluorite | Level 6

Just seen Art297 suggested call execute too - sorry Art!

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
  • 3754 views
  • 6 likes
  • 5 in conversation