*
;
/*
     Subject: Re: How to read .dbf?
        Date: Sat, 08 Feb 1997 20:35:01 GMT
        From: rob.thomas@his.com.removethis (Rob Thomas)
    Reply-To: Rob Thomas
Organization: Advanced Data Analysis
  Newsgroups: comp.soft-sys.sas

On Wed, 05 Feb 1997 13:37:16 -0800, "Jim Y. Wan"
 wrote:

>I received a file with extension .dbf? How do I read it into SAS 6.11 
>for Windows? TIA.

I'm assuming you don't have the SAS Access module or an earlier
version of PC-SAS that includes proc dbf.

The next option is my preference.  Conceptual Software has a couple
of packages that are quite inexpensive that will do the trick.  I've
been using DBMS/COPY for years now, and I think it's the best thing
going. Visit www.conceptual.com.

Another option is SAS code that reads the dbf data and converts the
data to a SAS dataset.  I got the original code from the SAS BBS
site in '93, and modified it so it is more flexible.  It was written
by another user and posted to the BBS several years ago. THe code is
at the bottom of this post.

Finally, since I have DBMSCOPY and can convert the data for you.

======================================================================
Advanced Data Analysis, Est.'89. Over 19 years experience with general
statistical consulting and multi-platform SAS programming. Also: data 
conversions across platforms, media, and applications. Data storehouse
and conversion for '90 census, health, nutrition, and other government
databases. //www.his.com/rltstats, rob.thomas@his.com, 301-949-8065
======================================================================


/*******************************************************************/
/* The majority of this code is in the public domain. The portion  */
/* of the data step that deals with reading *.dbf formatted files  */
/* was retrieved by Rob Thomas from the SAS institute BBS (SIBBS). */
/*                                                                 */
/* My additions and enhancements deal with removing stray charac-  */
/* ters and allowing extremely large lrecls.                       */
/*******************************************************************/

*libname sasdata "d:\";    *<=== P L E A S E assign your SAS Library;

%macro convert(dbfname) ;

%let fname="d:\&dbfname..dbf";
%let dsname="sasdata.&dbfname.";
filename dbfile &fname;
filename dbf2sas "d:\&dbfname..sas";

data one ;
  infile dbfile recfm=n;
  length varname $ 8 hedfmt infmt $ 9 droplist $ 200;
  input @1 has_memo ib1.
  @2 dbyear ib1.
  @3 dbmon ib1.
  @4 dbday ib1.
  @5 numrecs ib4.
  @9 headlen ib2.
  @11 reclen ib2. ;

  if has_memo ne 3 then do;
    put 'WARNING: The input file has a memo file associated with
it!';
    put 'WARNING: Data will be lost!';
    end;

  put dbyear= dbmon= dbday= numrecs= headlen= reclen=;
  numflds=round(((headlen-34)/32),1);
  put numflds=;
  pos=33;

  file dbf2sas;
  if headlen le 200 then do;
    hdfmt='$char' || left(put(headlen,3.)) || '.;';
    put 'data ' &dsname '(drop=header del);';
    put 'infile "'&fname '"  recfm=n end=end;';
    put 'input header 'hdfmt;
    end;

  else do;
    put 'data '&dsname ';';
    put 'length del $ 1;';
    put 'infile "' &fname '" recfm=n end=end;';
    put 'input';
    hedfmt='$char' || '200.';
    numsects=int(headlen/200)+1;
    lenlast=headlen-((numsects-1)*200);
    droplist='del';

    do pcs=1 to numsects-1;
      hedname='h'||left(put(pcs,3.));
      droplist=trim(droplist)||' '||hedname;
      put hedname hedfmt;
      end;

    hedfmt=compress('$char'||left(put(lenlast,3.))||'.') ;
    hedname='h'||left(put(numsects,2.));
    droplist=trim(droplist)||' '||hedname;
    put hedname hedfmt';';
    end;

  put 'do until(end);';
  put '  input';
  put '  del $char1.';
  nul=put(0,ib1.);

  do flds=1 to numflds;
    input @pos fldtemp $10. +1 fld_type $1. +4
    fld_len ib1. fld_dec ib1.;
    varname = left(scan(compress(fldtemp,'_*$#@!~`'),1,nul)) ;
    if (fld_type='C' | fld_type='L' | fld_type='D') then
    infmt='$char'||trim(left(put(fld_len,3.)))||'.';
    else if fld_type='N' then
    infmt=trim(put(fld_len,3.))||'.'||left(put(fld_dec,2.));
    else if fld_type='M' then do;
      infmt=trim(put(fld_len,3.))||'.'||left(put(fld_dec,2.));
      droplist=trim(droplist)||' '||varname;
      end;
    put '  ' varname infmt;
    pos+32;
   end;
  put '  ;';
  put '  output;'/'end;';
  if length(droplist) gt 8 then put 'drop ' droplist ';';
  put 'run;';
  stop;
 run;

 %include "d:\&dbfname..sas" ;

run ;

%mend convert ;
*
;