12.07.2015 Views

%FLATFILE, and Make Your Life Easier - Ftp Sas

%FLATFILE, and Make Your Life Easier - Ftp Sas

%FLATFILE, and Make Your Life Easier - Ftp Sas

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

counts the number of iterations through the DATA step. Theput function changes the numeric counter, _N_, to thecharacter representation of that number so there are no noteswritten to the log that SAS is converting it for us. The leftfunction removes leading blanks in front of the number, so wedon't try to create a macro variable named VAR 1. Then thetwo vertical bars concatenate the left justified number to theword VAR.Notice the second argument to CALL SYMPUT is NAME,which is the name of the variable fromDICTIONARY.COLUMNS. Since NAME is not in quotes, theDATA step has to assume that it is a variable in the ProgramData Vector (PDV). During execution, the DATA step looksinside the PDV for the value of NAME <strong>and</strong> finds the name ofthe first variable.SPECIFY THE FORMAT TO WRITE FOR EACH VARIABLE The next series of statements checks to see if that variablealready has a format. If so, the first IF condition is true <strong>and</strong> amacro variable named FMTn is created whose value is thename of the format. If no format is assigned, the nextstatement checks to see if it is a character variable. Acharacter variable has a value of CHAR in the TYPE columnfrom DICTIONARY.COLUMNS. If the variable is character, acharacter format is created based on the length of thatvariable. For example, if the length of the character variable is12, a format of $12. is created. If a variable is numeric <strong>and</strong> hasno format, a format of BEST10. is assigned.The DATA step continues to loop through, creating 2 macrovariables for each variable in the original data set. Finally, it's extremely helpful to know how many variablesare in the data set you want written out, so the last statementsays if this is the last observation, create a macro variablenamed NUMVAR whose value is the total number of variables.KNOW HOW TO WRITE THE DATA STEP CODEThe general form for writing a data set out to a flat file is:DATA _NULL_ ;SET SAS-data-set ;FILE 'name-of-file-to-write' ;PUT column-pointer variable-name format ...;RUN ;When calling the macro, you will supply the name of the dataset <strong>and</strong> the name of the flat file. The macro will then build thePUT statement for you.The SET StatementNotice the SET statement in the last DATA step. The macrovariables LIB <strong>and</strong> DSN are supplied when calling the macro.So why are there two periods after &LIB? The macro facilitytreats all periods that follow a macro variable reference as adelimiter to end a macro variable name. This is useful if youhave additional text that needs to be the suffix to the value ofthe macro variable. The period is then thrown away. If oneperiod's not enough, use two! The first still works as adelimiter <strong>and</strong> gets thrown away, but the second period is thentreated as text to separate the libref from the data set name.The FILE Statement The FILE statement has double quotes around the macrovariable reference &FILE to allow the macro variable toresolve. The macro facility does not "peek inside" singlequotes.The PUT Statement The word PUT only needs to be in the DATA step once, soit is outside of the %DO loop.The %DO loop will execute for as many times as there arevariables in the original data set, which is determined by&NUMVAR. The code that is generated is sent to the DATAstep compiler. The index counter, I, in the %DO loop is amacro variable <strong>and</strong> will be used to cycle through the macrovariables we created earlier (VAR1 - VARn, FMT1 - FMTn). To retrieve the value from the macro variable VAR1, weneed to precede the macro variable name with an ampers<strong>and</strong>(ie. &VAR1). However, since the number at the end is notalways a 1, we need to substitute the 1 with our index counter,&I. So now we have &VAR&I. So the macro facility wouldscan looking for a macro variable called &VAR. It won't findone, <strong>and</strong> will generate a warning message. Then &I willresolve to 1. But the &VAR did not resolve properly.So if one ampers<strong>and</strong> isn't enough, try two!&&VAR&IWith multiple ampers<strong>and</strong>s, the macro facility takes twoampers<strong>and</strong>s <strong>and</strong> makes them one. The word VAR isn't amacro trigger, so it just tags along for the ride. &I the first timethrough the %DO loop resolves to 1. So now we have&VAR1. Exactly what we wanted. The macro facility then rescans&VAR1, <strong>and</strong> that resolves to the name of the firstvariable in the data set.The same is true for the format, but this time the word FMTtags along for the ride so we have &FMT1.CALCULATE THE COLUMN WIDTHS TO ENSURE THEREIS NO OVERLAPThe +1 moves the column pointer over to the next field to puta space between this column <strong>and</strong> the next. Notice there is nosemicolon inside the %DO loop. The %DO loop continueslooping, writing out the variables on one PUT statement. Afterall of the variables have been written to the PUT statement,the %DO loop ends execution.11 However, the PUT statement still does not have asemicolon to end that statement. That is what the semicolonon the line by itself accomplishes.CALLING THE MACRONow, let's create a flat file on MVS named MYID.FLAT.FILEbased on the data set SASUSER.HOUSES. The code to callthe macro is:%flatfile(lib=sasuser, dsn=houses,file=myid.flat.file)If you are using a directory-based system, you might code:%flatfile(lib=sasuser, dsn=houses,file=flat.dat)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!