%MACRO ARCTOSAS(INFILE='info.e00',OUTSAS=outsas,WORKFILE=work1, DUMMY=dummy); %put Begin arctosas execution - version in sasmacro library******; %* this macro creates a SAS dataset from an ARC/INFO export file 1)create an ARC/INFO export (.e00) file, $ARC arc: EXPORT INFO ARCFILE.EXT OUTNAME NONE where ARCFILE.EXT is ARC file OUTNAME is export file name (OUTNAME.E00) NONE specifies no compression 2)download the export (.e00) file to the PC 3.a)invoke SAS on the PC b)allocate SAS library, filenames c)include the file that contains the ARCTOSAS macro d)supply the ARCTOSAS macro call with the following arguments: INFILE is the ARC/INFO export file name OUTSAS is the one-level (temporary) or two-level (permanent) SAS output dataset WORKFILE is a SAS work dataset (temporary) DUMMY is a 5-character or less SAS variable name that is not the name of an INFO variable in the .e00 dataset (the default values are shown in the argument list of the macro statement) e)submit the resulting SAS dataset will have as many variables as there are INFO items. the macro will attempt to give SAS names that are identical or close to the INFO item names. if it fails, the SAS names will be VAR1-VARn. the LABEL for the SAS dataset will be the value of the INFILE parameter (the .e00 dataset name) the LABELS for the SAS variables will be the exact corresponding INFO item names the length and type of the SAS variables are shown below --info item-- --SAS variable-- binary (4,8) real (8) char (1-4096) char ($1-$200) date (8) date (8) floating (4,8) real (8) integer (4,8) real (8) good luck ! program developed by Barbara Jackson Lockheed Martin Energy Research inc Oak Ridge National Laboratory PO 2008, ms 6038 Oak Ridge TN 37831-6038 (423)574-8680 2/90 copyright "SAS macros for converting between ARC/INFO import/export files and SAS data sets", SAS Institute, Inc., 'Proceedings of the Sixteenth Annual SAS Users Group International Conference', Cary, NC: SAS Institute Inc., 1991, pages 1310-1315. "The submitted manuscript has been authored by a contractor of the U.S. Government under contract No. DE-AC05-84OR21400. Accordingly, the U.S. Government retains a nonexclusive, royalty-free license to publish or reproduce the published form of this contribution, or allow others to do so, for U.S. Government purposes." ; options mprint; data _null_; %GLOBAL NUMVARS INFOLINS DATALIN NUMDATA; infile &INFILE firstobs=3 obs=3; %PUT you have chosen infile = &INFILE; %PUT you have chosen outfile = &OUTSAS; %PUT work file is &WORKFILE; %PUT dummy variable name is &DUMMY; input numvars 39-42 numdata 52-56; /* numvars includes redefined items*/ infolins = numvars + 3; /* last information record */ datalin = infolins + 1; /* first data record */ call symput('NUMVARS' ,left(put(numvars,5.))); call symput('INFOLINS',left(put(infolins,5.))); call symput('DATALIN' ,left(put(datalin,5.))); call symput('NUMDATA' ,left(put(numdata,5.))); run; %PUT number of variables = &NUMVARS; %PUT number of data records = &NUMDATA; %PUT number of data lines = &INFOLINS; %PUT first data record = &DATALIN ; data &WORKFILE; infile &INFILE firstobs=4 obs=&infolins ; attrib sasname length=$8; attrib fmt length=$10; retain startcol endcol; input varname $16. arclen $3. @30 len $3. @33 dec_n $2. @36 type 1.; if dec_n < 0 then dec_n = 0; nochars = length(varname); count = _n_; if _n_ = 1 then endcol = 0; sasname = substr(varname,1,8); %* replace invalid characters in sasname with '_' ; if indexc(sasname,'#-/*()\|$@!%&.?<>') gt 0 then sasname = translate(sasname,'X________________','#-/+()\|$@!%&.?<>'); if indexc(sasname,'''') gt 0 then sasname = translate(sasname,'_',''''); %* assign a format value according to type assign the width of this field; select (type); when (1) do; /* date */ fmt = 'yymmdd8.'; width = 8; end; when (2) do; /* char */ %*character variable is max length of 200; fmt = '$char'||compress(min(200,len))||'.'; width = len; end; when (3) do; /*integer*/ fmt = compress(arclen)||'.'; width = arclen; end; when (4) do; /*numeric*/ fmt = '14.'||left(dec_n); width = 14; end; when (5) do; /*binary */ fmt = '11.'||left(dec_n); width = 11; end; when (6) if arclen='4' then do; /*floating*/ fmt = '14.'||left(dec_n); width = 14; end; else if arclen='8' then do; fmt = '24.'||left(dec_n); width = 24; end; end; %* assign the start and end column number for this field; startcol = endcol + 1; endcol = startcol + width -1; %* sort the sasnames according to length of name and alphabetical order. if any names are duplicates, then append an integer to the end of the name. a name may have up to 8 characters. ; proc sort data=&WORKFILE; by nochars sasname; data &WORKFILE; set &WORKFILE; retain i; if _n_ = 1 then i = 0; if sasname = lag(sasname) then do; i = i + 1; if nochars <= 7 then sasname = compress(sasname)||left(put(i,3.)); else sasname = substr(sasname,1,7)||left(put(i,3.)); end; else i = 0; %* check to see if there are still duplicate sasnames if so, set a macro variable to flag this condition; proc sort data=&WORKFILE; by sasname; %LET FLAG=good; data _null_; set &WORKFILE; if sasname = lag(sasname) then call symput ('FLAG','bad'); run; %* sort the workfile dataset back to its orginal order; proc sort data=&WORKFILE; by count; proc print data=&WORKFILE; %* create macro variables MVi from sas variable names FMi from formats TYi from data type STi start column for this field logical record SCi start column for this field physical record WIi for width of this field SRi first 80-col record for this variable NRi SRi + 1 LRi last 80-col record for this variable LAi label for this variable NUMBYTES total width of the logical record NUMRECS number of physical records in a logical record ENDDATA number of last physical data record ; data _null_; set &WORKFILE end = eof; call symput ('MV'||left(put(_n_,3.)),sasname); call symput ('LA'||left(put(_n_,3.)),varname); call symput ('FM'||left(put(_n_,3.)),fmt); call symput ('TY'||left(put(_n_,3.)),type); call symput ('ST'||left(put(_n_,5.)),left(put(startcol,8.))); sc1 = mod(startcol,80); if sc1 = 0 then sc1 = 80; call symput ('SC'||left(put(_n_,3.)) ,left(put(sc1,8.))); call symput ('WI'||left(put(_n_,3.)),left(put(width,8.))); call symput ('SR'||left(put(_n_,3.)) ,left(put(ceil(startcol/80),8.))); st1 = ceil(startcol/80) + 1; call symput ('NR'||left(put(_n_,3.)) ,left(put(st1,8.))); call symput ('LR'||left(put(_n_,3.)) ,left(put(ceil((startcol+width-1)/80),8.))); if eof then do; call symput ('NUMBYTES',endcol); call symput ('NUMRECS',compress(ceil(endcol/80))); call symput ('ENDDATA',ceil(endcol/80)*&NUMDATA+&INFOLINS); end; run; * get length of first data record; data _null_; infile &INFILE firstobs=&datalin obs=&datalin length=lg; input ; call symput ('CHARLEN',compress(lg)); run; %PUT charlen is &CHARLEN; %DO i = 1 %TO &NUMVARS; %PUT MVi *&&MV&i* STi *&&ST&i* SCi *&&SC&i* WIi *&&WI&i* SRi *&&SR&i NRi *&&NR&i* LRi *&&LR&i*; %END; %*****************************************************; %MACRO INPUTEM; %* this macro generates an input statement; %IF &NUMRECS = 1 %THEN %STR(input &DUMMY.1 $char&CHARLEN..;); %ELSE %STR(input (&DUMMY.1-&DUMMY&NUMRECS) ($char80.);); %MEND INPUTEM; %*****************************************************; %MACRO GETREC(I); %* this macro concatenates physical records together to make the big logical record; %LOCAL j; &DUMMY&&SR&I %IF &&LR&I > &&SR&I %THEN %DO j = &&NR&I %TO &&LR&I; ||&DUMMY&j %END; %MEND GETREC; %*****************************************************; %MACRO VARS2; %* this macro extracts variables from big logical record string; options symbolgen; %DO i = 1 %TO &NUMVARS; %IF &&TY&i = 1 %THEN %STR( var&i = input(translate(substr(%GETREC(&i), &&SC&i,&&WI&i),'0',' '),&&FM&i); ); %ELSE %STR(var&i = input(substr(%GETREC(&i), &&SC&i,&&WI&i),&&FM&i); ); %END; options nosymbolgen; %MEND VARS2; %*****************************************************; %MACRO VARS; %* this macro extracts variables from big logical record string; %LOCAL i; %DO i = 1 %TO &NUMVARS; %IF &&TY&i = 1 %THEN %STR(var&i = input(translate(substr(&BIGREC,&&ST&i,&&WI&i),'0',' '),&&FM&i); ); %ELSE %STR(var&i = input(substr(&BIGREC,&&ST&i,&&WI&i),&&FM&i);); %END; %MEND VARS; %*****************************************************; %MACRO CONCAT; %* this macro concatenates physical records together to make the big logical record; %LOCAL i; &DUMMY.1 %IF &NUMRECS > 1 %THEN %DO i = 2 %TO &NUMRECS; ||&&DUMMY&i %END; %MEND CONCAT; %*****************************************************; %MACRO LABELEM; %* this macro generates a label statement; %LOCAL i; label %DO i = 1 %TO &NUMVARS; var&i = "&&LA&i" %END; ; %MEND LABELEM; %*********************************************************; %MACRO RENAMEM; %* this macro generates rename statement; %LOCAL i; rename %DO i = 1 %TO &NUMVARS; var&i = &&MV&i %END; ; %MEND RENAMEM; %*********************************************************; %* read the arc/info input file and create SAS output file; data &OUTSAS (label = "&INFILE"); infile &INFILE firstobs=&DATALIN flowover lrecl=80 linesize=80 obs = &ENDDATA ; %LABELEM; /* generate label statment */ %INPUTEM; /* input physical records */ %VARS2; /* input variables from string */ drop &DUMMY.1-&DUMMY&NUMRECS; run; proc print data=&OUTSAS; proc contents data=&OUTSAS; run; %* if sasnames are valid rename all the variables; %IF &FLAG=good %THEN %DO; data &OUTSAS (label = "&INFILE"); set &OUTSAS; %RENAMEM; /* rename variables to sasname */ proc contents data=&OUTSAS; proc print data=&OUTSAS (obs=10); run; %END; run; %MEND ARCTOSAS; /* *****************comment out *libname arc 'c:\sas\arci\'; options mprint; %arctosas(INFILE='d:\sas\arci\moxone.e00',OUTSAS=moxone); *********************end commetn, jgb********************* */