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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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