DATA Step, Macro, Functions and more

proc import inside a data step

Reply
Contributor
Posts: 44

proc import inside a data step

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?

PROC Star
Posts: 7,363

proc import inside a data step

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

Frequent Contributor
Posts: 75

Re: proc import inside a data step

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     

Super User
Super User
Posts: 6,502

Re: proc import inside a data step

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 ;

Frequent Contributor
Frequent Contributor
Posts: 94

Re: proc import inside a data step

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

Frequent Contributor
Frequent Contributor
Posts: 94

Re: proc import inside a data step

Just seen Art297 suggested call execute too - sorry Art!

Ask a Question
Discussion stats
  • 5 replies
  • 1012 views
  • 6 likes
  • 5 in conversation