Using INFILE And INPUT Statements To Introduce External Data Into . - SAS

Transcription

Using INFILE and INPUTStatements to Introduce ExternalData into the SAS System– the interactive session –Andrew T. Kuligowski

Using INFILE and INPUT IntroductionIntroductionBasic INFILE / INPUTConditional INPUTColumn and Line PointersInformatsUsing INFILE and INPUT Statements to Introduce External Data into the SAS System2

Using INFILE and INPUT IntroductionVariable Length FilesComma Separated Value FilesDynamic Data ExchangeHTMLINPUT FunctionConclusionUsing INFILE and INPUT Statements to Introduce External Data into the SAS System3

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */DATA SasConf;INFILE DATALINES;INPUT ConfNameConfYearConfCityConfST ;DATALINES;SUGI2006 San Francisco CAPHARMASUG 2006 Bonita Springs FL Some DATALINES removed to fit example on screen MWSUGPNWSUGSUGI;2006 Dearborn2006 Seaside2007 OrlandoMIORFLUsing INFILE and INPUT Statements to Introduce External Data into the SAS System4

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */OPEN UP CONFYR1.TXTDATA SasConf;(directory to be announced)INFILE DATALINES;INPUT ConfName(This is your input data.)ConfYearConfCityAdd code around it, saveConfST ;as TC1.sasDATALINES;SUGI2006 San Francisco CAPHARMASUG 2006 Bonita Springs FL Some DATALINES removed to fit example on screen MWSUGPNWSUGSUGI;2006 Dearborn2006 Seaside2007 OrlandoMIORFLDO NOT RUN IT YET!!Using INFILE and INPUT Statements to Introduce External Data into the SAS System5

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */DATA SasConf;INFILE DATALINES;Define the source of theINPUT ConfNameConfYearexternal data to the DATAConfCitystep. Usually followed byConfST ;DATALINES;a file name or a fileSUGI2006 San FraPHARMASUG 2006 Bonita reference (a “nickname”INFILE Some DATALINES removed MWSUGPNWSUGSUGI;for an external source).2006 Dearbor2006 Seaside2007 Orlando Mayalso include optionalparameters describing thedata source.Using INFILE and INPUT Statements to Introduce External Data into the SAS System6

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */DATA SasConf;INFILE DATALINES;Define the format of theINPUT ConfNameConfYeardata to be processed.ConfCityConfST ;DATALINES;This is the commandSUGI2006 San FraPHARMASUG 2006 Bonita which actually causes theINPUT Some DATALINES removed MWSUGPNWSUGSUGI;data to be transferredthe external sourceinto the DATA step.2006 Dearbor2006 Seaside from2007 OrlandoUsing INFILE and INPUT Statements to Introduce External Data into the SAS System7

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */DATA SasConf;INFILE DATALINES;Input statement containsINPUT ConfNameConfYearthe name of each variableConfCityto be read in.ConfST ;DATALINES;SUGI2006 San FraPHARMASUG 2006 Bonita Fields separated onList Input Some DATALINES removed MWSUGPNWSUGSUGI;external file by one orblanks (or otherdelimiter).2006 Dearbor2006 Seaside more2007 OrlandoUsing INFILE and INPUT Statements to Introduce External Data into the SAS System8

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */DATA SasConf;INFILE DATALINES;A special file referenceINPUT ConfNameConfYearthat tells SAS there will beConfCityinstream data followingConfST ;DATALINES;the conclusion of theSUGI2006 San FraPHARMASUG 2006 Bonita DATA Step.DATALINES Some DATALINES removed MWSUGPNWSUGSUGI;2006 Dearbor Terminated with2006 Seaside2007 Orlando (semicolon)a ;Also known as CARDSUsing INFILE and INPUT Statements to Introduce External Data into the SAS System9

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */DATA SasConf;INFILE DATALINES4; DATALINES4 can be usedINPUT ConfNameConfYearwhen the instream dataConfCitycould contain aConfST ;DATALINES4;semicolon in the firstSUGI2006 San FraPHARMASUG 2006 Bonita position.(A quick aside ) Some DATALINES removed MWSUGPNWSUGSUGI;;;;2006 Dearbor Terminated with ;;;;2006 Seaside2007 Orlando (4 consect. semicolons)CARDS4 also works.Using INFILE and INPUT Statements to Introduce External Data into the SAS System10

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */DATA SasConf;INFILE DATALINES;INPUT ConfNameConfYearConfCityConfST ;DATALINES;SUGI2006 SanPHARMASUG 2006 Boni Some DATALINES rem MWSUGPNWSUGSUGI;2006 Dear2006 Seas2007 I200620062006200620062006200620062007San FranciscoBonita ideOrlandoCAFLPACAGATXMIORFL This is the entire instream dataset. NOW RUN IT.Using INFILE and INPUT Statements to Introduce External Data into the SAS System11

Using INFILE and INPUT Basic INFILE / INPUTNOTE: Invalid data for ConfName in line 9 1-9.NOTE: Invalid data for ConfCity in line 9 16-18.NOTE: Invalid data for ConfST in line 9 20-28.RULE: ---- ----1---- ----2---- ----3--9SUGI2006 San Francisco CAConfName . ConfYear 2006 ConfCity . ConfST .ERROR 1 N 1NOTE: Invalid data for ConfName in line 10 1-9.NOTE: Invalid data for ConfCity in line 9 16-18.NOTE: Invalid data for ConfST in line 9 20-28.10PHARMASUG 2006 Bonita Springs FLConfName . ConfYear 2006 ConfCity . ConfST .ERROR 1 N 2 messages repeated for each line of input data Using INFILE and INPUT Statements to Introduce External Data into the SAS System12

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */DATA SasConf;INFILE DATALINES;Input statement containsINPUT ConfName the name of each variableConfYearConfCity to be read in PLUS formatConfST ;modifiers that specifyDATALINES;SUGI2006 San Fra some additional piece ofPHARMASUG 2006 BonitaModified List Inputinformation about fields toDearborSeaside be processed. Some DATALINES removed MWSUGPNWSUGSUGI;200620062007 OrlandoUsing INFILE and INPUT Statements to Introduce External Data into the SAS System13

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */DATA SasConf;INFILE DATALINES;Additional informationINPUT ConfName about field to be read –ConfYearConfCity Field is to be treated asConfST ;character rather thanDATALINES;SUGI2006 San Fra numeric.PHARMASUG 2006 Bonita Format Modifier Some DATALINES removed MWSUGPNWSUGSUGI;2006 Dearbor2006 Seaside2007 OrlandoNOW RUN IT.Using INFILE and INPUT Statements to Introduce External Data into the SAS System14

Using INFILE and INPUT Basic INFILE / INPUTNOTE: The data set WORK.SASCONF has 9observations and 4 variables. Results of a PROC PRINT ConfObs ConfName Year ConfCityConfST1SUGI2006 SanFrancisc2PHARMASU 2006 BonitaSprings3NESUG2006 PhiladelPA4WUSS2006 IrvineCA5SESUG2006 AtlantaGA6SCSUG2006 IrvingTX7MWSUG2006 DearbornMI8PNWSUG2006 SeasideOR9SUGI2007 OrlandoFLUsing INFILE and INPUT Statements to Introduce External Data into the SAS System15

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */DATA SasConf;LENGTH ConfName 9.Additional informationConfCity 14.;INFILE DATALINES;about field to be read –INPUT ConfName Character field can haveConfYearConfCity & single embedded blanks.ConfST ;DATALINES;SUGI2006 San FraPHARMASUG 2006 Bonita In this example, override& Format ModifierLENGTHdefault length of 8 forDearborSeaside character variable.OrlandoNOW RUN IT. Some DATALINES removed MWSUG2006PNWSUG2006SUGI2007; Using INFILE and INPUT Statements to Introduce External Data into the SAS System16

Using INFILE and INPUT Basic INFILE / INPUTNOTE: SAS went to a new line when INPUTstatement reached past the end of a line.NOTE: The data set WORK.SASCONF has 8observations and 4 variables. Results of a PROC PRINT Conf ConfObs ConfNameConfCityYear ST1SUGISan Francisco2006 CA2PHARMASUG Bonita Springs 2006 NESUG3WUSSIrvine2006 CA4SESUGAtlanta2006 GA5SCSUGIrving2006 TX6MWSUGDearborn2006 MI7PNWSUGSeaside2006 OR8SUGIOrlando2007 FLUsing INFILE and INPUT Statements to Introduce External Data into the SAS System17

Using INFILE and INPUT Basic INFILE / INPUTWhat happened?Behind the scenes, reading the 2nd line INPUT ConfName PHARMASUGOKConfYear 2006 OKConfCity & Bonita Springs FLRead “Bonita” blank “Springs” blank “FL”.End of line, skip to next line.ConfST ; NESUG(Ignore rest of this input line.)So what happened to the “FL” in our output?LENGTH ConfName 9. ConfCity 14.;Bonita Springs FL (“ FL” in position 15-17)Using INFILE and INPUT Statements to Introduce External Data into the SAS System18

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */DATA SasConf;LENGTH ConfName 9.INFILE option. PreventsConfCity 17.;INFILE DATALINESSAS from moving to theTRUNCOVER ;INPUT ConfName next input line if End ofConfYearLine encountered inConfCity & middle of an INPUT.ConfST ;IF ConfST " " THDefault is FLOWOVER.ConfST SUBSTR(CoConfCity SUBSTR(END;DATALINES;TRUNCOVER All DATALINES removed ; Using INFILE and INPUT Statements to Introduce External Data into the SAS System19

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */DATA SasConf;LENGTH ConfName 9. MISSOVER would alsoConfCity 17.;INFILE DATALINESwork. The difference –TRUNCOVER ;INPUT ConfName when INPUT reaches endConfYearof line in the middle of aConfCity & variable, MISSOVER setsConfST ;IF ConfST " " TH that value to missing,ConfST SUBSTR(Co while TRUNCOVERConfCity SUBSTR(keeps the partial value.END;DATALINES;(A quick aside ) All DATALINES removed ; Using INFILE and INPUT Statements to Introduce External Data into the SAS System20

Using INFILE and INPUT Basic INFILE / INPUT/* INTRO EXAMPLE */(Additional coding logicDATA SasConf;LENGTH ConfName 9. added to deal with specialConfCity 17.;case of field reachingINFILE DATALINESTRUNCOVER ; maximum length.)INPUT ConfName ConfYearConfCity & ConfST ;IF ConfST " " THEN DO;ConfST SUBSTR(ConfCity,16);ConfCity SUBSTR(ConfCity,1,14);END;DATALINES;NOW RUN IT. All DATALINES removed to fit example on screen ; Using INFILE and INPUT Statements to Introduce External Data into the SAS System21

Using INFILE and INPUT Basic INFILE / INPUTNOTE: The data set WORK.SASCONF has 9observations and 4 variables. Results of a PROC PRINT ConfObs ConfNameYear ConfCity1SUGI2006 San Francisco2PHARMASUG 2006 Bonita Springs3NESUG2006 Philadelphia4WUSS2006 Irvine5SESUG2006 Atlanta6SCSUG2006 Irving7MWSUG2006 Dearborn8PNWSUG2006 Seaside9SUGI2007 OrlandoUsing INFILE and INPUT Statements to Introduce External Data into the SAS SystemConfSTCAFLPACAGATXMIORFL22

Using INFILE and INPUT Conditional INPUT/* “RECORD TYPE” EXAMPLE */FILENAME SASCONF ‘ directory .CONFYR3.DATA’;DATA SasConf;INFILE SASCONF ;INPUT @ 1 RecordType CHAR1. @ ;IF RecordType ‘R’ THEN DO;INPUT @ 3ConfName CHAR9.@ 13ConfYear4.@ “LOC ” ConfCity CHAR14.@ 36ConfST CHAR2. ;OUTPUT;END;ELSE INPUT; /* optional */RUN;Using INFILE and INPUT Statements to Introduce External Data into the SAS System23

Using INFILE and INPUT Conditional INPUT/* “RECORD TYPE” EXAMPLE */FILENAME SASCONF ‘ directory .CONFYR3.DATA’;DATA SasConf;Modify existing code,INFILE SASCONF ;INPUT @ 1 RecordType CHAR1. @ ; save as TC2.sasIF RecordType ‘R’ THEN DO;INPUT @ 3ConfName CHAR9.@ 13ConfYear4.@ “LOC ” ConfCity CHAR14.@ 36ConfST CHAR2. ;OUTPUT;END;ELSE INPUT; /* optional */RUN;DO NOT RUN IT YET!!Using INFILE and INPUT Statements to Introduce External Data into the SAS System24

Using INFILE and INPUT Conditional INPUT/* “RECORD TYPE” EXFILENAME SASCONF ‘UDATA SasConf;INFILE SASCONF ;INPUT @ 1 RecordTIF RecordType INPUT @ 3@ 13@ “LOC ”@ 36OUTPUT;END;ELSE INPUT; /*RUN;ISRRRRRRI‘SASCONF3.DATA’SUGI2006 LOC San FranciscoPHARMASUG 2006 LOC Bonita SpringsNESUG2006 LOC PhiladelphiaWUSS2006 LOC IrvineSESUG2006 LOC AtlantaSCSUG2006 LOC IrvingMWSUG2006 LOC DearbornPNWSUG2006 LOC SeasideSASGBLFRM 2007 LOC OrlandoThis is the entire dataset. It is basically thesame data that we used in our previousexample, except: A record type has been added. LOC has been inserted.Using INFILE and INPUT Statements to Introduce External Data into the SAS System25

Using INFILE and INPUT Conditional INPUT/* “RECORD TYPE” EXAMPFILENAME SASCONF ‘ dirDATA SasConf;Assign a file reference toINFILE SASCONF ;INPUT @ 1 RecordType an external data sourceIF RecordType ‘R’ (which is an MVS datasetINPUT @ 3Co in this example).@ 13Co@ “LOC ” Co@ 36Co This file ref. will be usedOUTPUT;by the INFILE statement.END;ELSE INPUT; /* opt This statement occursRUN;FILENAMEoutside of the DATA step.Using INFILE and INPUT Statements to Introduce External Data into the SAS System26

Using INFILE and INPUT Conditional INPUT/* “RECORD TYPE” EXAMPFILENAME SASCONF ‘ dirDATA SasConf;Formats and columnINFILE SASCONF ;INPUT @ 1 RecordType pointers work together toIF RecordType ‘R’ perform the task ofINPUT @ 3Co reading data based on a@ 13Co@ “LOC ” Co specified list of variables.@ 36CoOUTPUT;END;ELSE INPUT; /* optRUN;Formatted InputUsing INFILE and INPUT Statements to Introduce External Data into the SAS System27

Using INFILE and INPUT Conditional INPUT@ “At sign” ColumnPointer Control/* “RECORD TYPE” EXAMPFILENAME SASCONF ‘ dirDATA SasConf;INFILE SASCONF ;INPUT @ 1 RecordType Move the column pointerIF RecordType ‘R’ to an absolute position onINPUT @ 3Co@ 13Co the input dataset.@ “LOC ” Co@ 36CoContinue reading dataOUTPUT;from that point.END;ELSE INPUT; /* optRUN;ALSO NOTE STRING!!Using INFILE and INPUT Statements to Introduce External Data into the SAS System28

Using INFILE and INPUT Conditional INPUTFormats CHARnn. Read charactervalue, nn positions long,keeping leading blanks.E */ctory .CONFYR3.DATA’; CHAR1. @ ;THEN DO;fName CHAR9.( nn. would read character value,fYear4.nn positions long, dropping leading fCity CHAR14.blanks.)fST CHAR2. ;nn. Read numeric value,nn positions long, store innumeric SAS variable.onal */Using INFILE and INPUT Statements to Introduce External Data into the SAS System29

Using INFILE and INPUT Conditional INPUT@ “Trailing At sign”Hold the line pointer onthe current input line. Donot advance to the nextline unless triggered to doso by another INPUTstatement or by the RUNstatement.E */ctory .CONFYR3.DATA’; CHAR1. @ ;THEN DO;fName CHAR9.fYear4.fCity CHAR14.fST CHAR2. ;onal */Using INFILE and INPUT Statements to Introduce External Data into the SAS System30

Using INFILE and INPUT Conditional INPUT(A quick aside)@@ “Double Trailing Atsign”Hold the line pointer onthe current input line. Donot advance to the nextline unless triggered to doso by another INPUTstatement.or by the RUNstatement.E */ctory .CONFYR3.DATA’; CHAR1. @ ;THEN DO;fName CHAR9.fYear4.fCity CHAR14.fST CHAR2. ;onal */Using INFILE and INPUT Statements to Introduce External Data into the SAS System31

Using INFILE and INPUT Conditional INPUT/* “RECORD TYPE” EXAMPFILENAME SASCONF ‘ dirDATA SasConf;Processes no data, butINFILE SASCONF ;INPUT @ 1 RecordType advances the internal lineIF RecordType ‘R’ pointer to the next lineINPUT @ 3Co (releasing any hold on that@ 13Co@ “LOC ” Co line, if applicable –@ 36Co as in this example).OUTPUT;END;ELSE INPUT; /* optRUN;Null InputNOW RUN IT.Using INFILE and INPUT Statements to Introduce External Data into the SAS System32

Using INFILE and INPUT Conditional INPUTNOTE: The infile SASCONF is:File Name USERID.SASCONF.DATA,Lrecl 80,Recfm FB,Blksize 960NOTE: 9 records were read from the infile SASCONF.NOTE: The data set WORK.SASCONF has6 observations and 5 variables. Results of a PROC PRINT Record ConfConfConfObs Type NameYear ConfCityST1RNESUG 2006 Philadelphia PA2RWUSS2006 IrvineCA3RSESUG 2006 AtlantaGA4RSCSUG 2006 IrvingTX5RMWSUG 2006 DearbornMI6RPNWSUG 2006 SeasideORUsing INFILE and INPUT Statements to Introduce External Data into the SAS System33

Using INFILE and INPUT Column and Line Pointers/* “Pointers” EXAMPLE */FILENAME SASCONF2 ‘USERID.SASCONF2.DATA’;DATA UGLYINPUT;OPEN UP CONFYR3.sasLENGTH ConfName 9. ;Pos1 9;Pos2 3;Str1 "LOC ";INFILE SASCONF2 ;INPUT #1 ConfCity 22-35 1ConfST (-39)RecordType Pos1ConfYear (-1*(Pos1 6))ConfName Do not even try totype this one in!DO NOT RUN IT YET!!Using INFILE and INPUT Statements to Introduce External Data into the SAS System34

Using INFILE and INPUT Column and Line Pointers/ @ "20"Year2Digit2@1RecordType2@ Pos2ConfName2@ Str1ConfCity2@ (Pos2*12 1) ConfST2ConfYear2 2000 Year2Digit2 ;RUN; 9. 14. ;Using INFILE and INPUT Statements to Introduce External Data into the SAS System35

Using INFILE and INPUT Column and Line Pointers/* “PointersFILENAME SASDATA UGLYINLENGTH CoPos1 9;Pos2 3;Str1 "LOINFILE SASINPUT #1 CCRCC‘USERID.SASCONF2.DATA’I SUGI2006 LOC San Francisco CAS PHARMASUG 2006 LOC Bonita Springs FLR NESUG2006 LOC PhiladelphiaPAR WUSS2006 LOC IrvineCAR SESUG2006 LOC AtlantaGAR SCSUG2006 LOC IrvingTXR MWSUG2006 LOC DearbornMIR PNWSUG2006 LOC SeasideORI SUGI2007 LOC OrlandoFLR SESUG2007 LOC Hilton HeadSCThis is the entire dataset. It is basically the samedata that we used in our previous example, except: LOC has been added before City Name. continued Blank Padding has been restored.Using INFILE and INPUT Statements to Introduce External Data into the SAS System36

Using INFILE and INPUT Column and Line Pointers/* “PFILENDATA Move the line pointer to the xth line of theLENPos current input buffer. The size of the inputPos buffer can be overridden by the N optionStr on the INFILE statement.INFINPUT #1 ConfCity 22-35 1ConfST (-39)RecordType Pos1ConfYear (-1*(Pos1 6))ConfName # Absolute Line Pointer Control continued Using INFILE and INPUT Statements to Introduce External Data into the SAS System37

Using INFILE and INPUT Column and Line Pointers/* “PFILENDATA Read the current variable betweenLENPos positions x and y of the current inputPos – in this example, columns 22 & 35.StrINFINPUT #1 ConfCity 22-35 1ConfST (-39)RecordType Pos1ConfYear (-1*(Pos1 6))ConfName Column Specificationline continued Using INFILE and INPUT Statements to Introduce External Data into the SAS System38

Using INFILE and INPUT Column and Line Pointers/* “PFILENDATALENPosPosStrINFINPColumn InputStart and end positions are specified foreach input variable. Character data cancontain embedded blanks and canexceed 8 characters in length, but it mustbe aligned consistently throughout thefile.RecordType ConfYearConfName Pos1 (-1*(Pos1 6)) continued Using INFILE and INPUT Statements to Introduce External Data into the SAS System39

Using INFILE and INPUT Column and Line Pointers/* “PFILENDATALENPos Move the column pointer X positionsPos this example, 1 position to the right.StrINFILE SASCONF2 ;INPUT #1 ConfCity 22-35 1ConfST (-39)RecordType Pos1ConfYear (-1*(Pos1 6))ConfName Relative ColumnPointer Control– in continued Using INFILE and INPUT Statements to Introduce External Data into the SAS System40

Using INFILE and INPUT Column and Line Pointers/* “PFILENDATALENPos Move the column pointer X positionsPos this example, 1 position to the right.StrINFILE SASCONF2 ;INPUT #1 ConfCity 22-35 1ConfST (-39) Relative ColumnPointer Control– inAnd in this example, 39 positions to theleft. The denotes movement – positivenumber is default, but not mandatory. continued Using INFILE and INPUT Statements to Introduce External Data into the SAS System41

Using INFILE and INPUT Column and Line Pointers/* “PFILENDATALENPos1 9;PosStr And in thisINF right. (TheINP Relative ColumnPointer Controlexample, 9 positions to thevariable Pos1 was assignedthe numeric value 9 and is substituted.)RecordType ConfYearConfName Pos1 (-1*(Pos1 6)) continued Using INFILE and INPUT Statements to Introduce External Data into the SAS System42

Using INFILE and INPUT Column and Line Pointers/* “PFILENDATALENPos1 9;PosStr And in this example, 15 positions to theINF left. (The variable Pos1 was assigned theINP numeric value 9, add 6, and multiply by - Relative ColumnPointer Control1 to get -15, which is substituted.)ConfYearConfName (-1*(Pos1 6)) continued Using INFILE and INPUT Statements to Introduce External Data into the SAS System43

Using INFILE and INPUT Column and Line PointersPos2 3;Str1 "LOC "; first part of input line removed for space limitations / @ "20"Year2Digit2@1RecordType2 @ Pos2ConfName2 9.@ Str1ConfCity2 14.ConRUN;/ (Implied) RelativeLine Pointer ControlMove the line pointer one line forward.No flexibility – use 3 slashes to move 3lines forward, cannot move backwards.Using INFILE and INPUT Statements to Introduce External Data into the SAS System44

Using INFILE and INPUT Column and Line PointersPos2 3;Str1 "LOC "; first part of input line removed for space limitations / @ "20"Year2Digit2@1RecordType2 @ Pos2ConfName2 9.@ Str1ConfCity2 14.@Absolute Column PointerConRUN; Move the column pointer to theControlspecifiedposition – in this example, 1 position tothe right of the next occurrence of thetext string “20” – NOT the 20th position.Using INFILE and INPUT Statements to Introduce External Data into the SAS System45

Using INFILE and INPUT Column and Line PointersPos2 3;Str1 "LOC "; first part of input line removed for space limitations / @ "20"Year2Digit2@1RecordType2 @ Pos2ConfName2 9.@ Str1ConfCity2 14.@Absolute Column Pointer ControlConRUN; And then back to the 1st position of thecurrent line.Using INFILE and INPUT Statements to Introduce External Data into the SAS System46

Using INFILE and INPUT Column and Line PointersPos2 3;Str1 "LOC "; first part of input line removed for space limitations / @ "20"Year2Digit2@1RecordType2 @ Pos2ConfName2 9.@ Str1ConfCity2 14.@Absolute Column Pointer ControlConRUN; Then move the column pointer toposition 3 on the current line. (Thevariable Pos2 was assigned the numericvalue 3 and is substituted.)Using INFILE and INPUT Statements to Introduce External Data into the SAS System47

Using INFILE and INPUT Column and Line PointersPos2 3;Str1 "LOC "; first part of input line removed for space limitations / @ "20"Year2Digit2@1RecordType2 @ Pos2ConfName2 9.@ Str1ConfCity2 14.@Absolute Column Pointer ControlConRUN; Next, move the column pointer to theposition immediately after the value“LOC ” on the current line. (The variableStr1 was assigned the string value“LOC ” and is substituted.)Using INFILE and INPUT Statements to Introduce External Data into the SAS System48

Using INFILE and INPUT Column and Line PointersPos2 3;@ Absolute ColumnStr1 "LOC ";Pointer Control first part of input line/ @ "20"Year2Digit2@1RecordType2 @ Pos2ConfName2 9.@ Str1ConfCity2 14.@ (Pos2*12 1) ConfST2 ;ConfYear2 2000 Year2Digit2 ;RUN;Finally, move the column pointer toposition 37 of the current line. (Thevariable Pos2 was assigned the numericvalue 3, multiply by 12, and add 1 to get37, which is substituted.)Using INFILE and INPUT Statements to Introduce External Data into the SAS System49

Using INFILE and INPUT Column and Line PointersPos2 3; And oneStr1 "LOC "; first part of input line/ @ "20"Year2Digit2@1RecordType2@ Pos2ConfName2@ Str1ConfCity2@ (Pos2*12 1) ConfST2ConfYear2 2000 Year2Digit2 ;RUN;more thing 9. 14. ;We need to adjust the value for the year toinclude the century. (Remember, we usedthe “20” to find the year – we never actuallyNOW RUN IT.read those 2 digits!)Using INFILE and INPUT Statements to Introduce External Data into the SAS System50

Using INFILE and INPUT Column and Line Pointers/* Column pointer controls w/character values. */DATA THE data;INFILE CARDS;Tricks withINPUT # 1 @"the" NEXT8 1 CHAR8.# 1 @"the " NEXT8 2 CHAR8. text column# 1 @"The" NEXT8 3 CHAR8. pointers# 1 @"The " NEXT8 4 CHAR8.@"The" NEXT8 5 CHAR8.;CARDS;The theme of the Thebes theater's etc. ;OPEN UP CONFYR4.sasDon't try to typethis one in, either.Using INFILE and INPUT Statements to Introduce External Data into the SAS System51

Using INFILE and INPUT Column and Line Pointers/* Column pointer controls w/character values. */DATA THE data;INFILE CARDS;Lower case,INPUT # 1 @"the" NEXT8 1 CHAR8.# 1 @"the " NEXT8 2 CHAR8. no trailing# 1 @"The" NEXT8 3 CHAR8. blank.# 1 @"The " NEXT8 4 CHAR8.@"The" NEXT8 5 CHAR8.;CARDS;The theme of the Thebes theater's etc. ;NEXT8 1 “me of th”Using INFILE and INPUT Statements to Introduce External Data into the SAS System52

Using INFILE and INPUT Column and Line Pointers/* Column pointer controls w/character values. */DATA THE data;INFILE CARDS;Lower case,INPUT # 1 @"the" NEXT8 1 CHAR8.# 1 @"the " NEXT8 2 CHAR8. trailing# 1 @"The" NEXT8 3 CHAR8. blank.# 1 @"The " NEXT8 4 CHAR8.@"The" NEXT8 5 CHAR8.;CARDS;The theme of the Thebes theater's etc. ;NEXT8 2 “Thebes t”Using INFILE and INPUT Statements to Introduce External Data into the SAS System53

Using INFILE and INPUT Column and Line Pointers/* Column pointer controls w/character values. */DATA THE data;INFILE CARDS;Upper case,INPUT # 1 @"the" NEXT8 1 CHAR8.# 1 @"the " NEXT8 2 CHAR8. no trailing# 1 @"The" NEXT8 3 CHAR8. blank.# 1 @"The " NEXT8 4 CHAR8.@"The" NEXT8 5 CHAR8.;CARDS;The theme of the Thebes theater's etc. ;NEXT8 3 “ theme o”Using INFILE and INPUT Statements to Introduce External Data into the SAS System54

Using INFILE and INPUT Column and Line Pointers/* Column pointer controls w/character values. */DATA THE data;INFILE CARDS;Upper case,INPUT # 1 @"the" NEXT8 1 CHAR8.# 1 @"the " NEXT8 2 CHAR8. trailing# 1 @"The" NEXT8 3 CHAR8. blank.# 1 @"The " NEXT8 4 CHAR8.@"The" NEXT8 5 CHAR8.;CARDS;The theme of the Thebes theater's etc. ;NEXT8 4 “theme of”Using INFILE and INPUT Statements to Introduce External Data into the SAS System55

Using INFILE and INPUT Column and Line Pointers/* Column pointer controls w/character values. */DATA THE data;INFILE CARDS;Upper case,INPUT # 1 @"the" NEXT8 1 CHAR8.# 1 @"the " NEXT8 2 CHAR8. trailing# 1 @"The" NEXT8 3 CHAR8. blank,# 1 @"The " NEXT8 4 CHAR8.@"The" NEXT8 5 CHAR8.;continue onCARDS;same inputThe theme of the Thebes theater's e;line.NEXT8 5 “bes thea”NOW RUN IT.Using INFILE and INPUT Statements to Introduce External Data into the SAS System56

Using INFILE and INPUT RELAX FOR AInformatsCOUPLE OFMINUTES!Informats are used to interpretincoming data into a form thatSAS can understand.They specify whether an inputvalue is character or numeric, itslength, the number of decimalplaces (if applicable), and otherspecial conditionsUsing INFILE and INPUT Statements to Introduce External Data into the SAS System57

Using INFILE and INPUT InformatsTwo methods to assign aninformat to a SAS variable.1) Temporary – Specify the informaton the INPUT statement.INPUT # 1 @"the"NEXT8 1 CHAR8.(This method has already beendemonstrated in this presentation.)Using INFILE and INPUT Statements to Introduce External Data into the SAS System58

Using INFILE and INPUT InformatsTwo methods to assign aninformat to a SAS variable.2) Permanent – Specify the informatwith a separate statement.INFORMAT NEXT8 1 CHAR8. DEFAULT CHAR20.;ATTRIB variables INFORMAT informat;Using INFILE and INPUT Statements to Introduce External Data into the SAS System59

Using INFILE and INPUT InformatsFive types of SAS Informats:1) Numeric2) Character3) Date / Time4) Column-binarynot covered5) User-DefinedPROC FORMATUsing INFILE and INPUT Statements to Introduce External Data into the SAS System60

Using INFILE and INPUT Informats (Numeric)A few examples of SAS Informatsw.dRead numeric value of width wwith d decimal places.COMMAw.dRead numeric value of width wwith d decimal places. Ignoreembedded commas (and other accountingsymbols, such as “ ”, percent signs, etc.). Aleft parenthesis at the beginning of the valuecauses the subsequent numeric value to betreated as a negative.Using INFILE and INPUT Statements to Introduce External Data into the SAS System61

Using INFILE and INPUT Informats (Character)A few examples of SAS Informats w. Read character value of width w,ignore leading blanks (if any). CHARw. Read character value of width w,include leading blanks (if any). ASCIIw. On IBM Mainframe, convert fromASCII to EBCDIC. On othersystems, treat as CHAR. EBCDICw.On IBM Mainframe, treat as CHAR. On other systems,convert from EBCDIC to ASCII.Using INFILE and INPUT Statements to Introduce External Data into the SAS System62

Using INFILE and INPUT Informats (Date / Time)A few examples of SAS InformatsDATEw.Read date (ddMMMyy orddMMMyyyy) into numeric field.JULIANw. Read Julian date (yyddd oryyyyddd) into numeric field.TIMEw.Read time (hh:mm:ss.ss) intonumeric field.Using INFILE and INPUT Statements to Introduce External Data into the SAS System63

Using INFILE and INPUT Variable Length FilesDATA SasConf;/*INFILE 'C:\Documents and ' TRUNCOVER;*/LENGTH ConfName 14. ConfST 2. ;INFILE DATALINES TRUNCOVER;/*INPUT ConfName CHAR14.ConfYear4.ConfST CHAR2. ; */INPUT ConfNameConfYearOPEN UP CONFYR5a.TXTConfST ;DATALINES;(directory to be announced) DO NOT RUN IT.Using INFILE and INPUT Statements to Introduce External Data into the SAS System64

Using INFILE and INPUT Variable Length FilesDATA SasConf;/*INFILE 'C:\DocumeDocuments\SASHOW07\LENGTH ConfName INFILE DATALINES/*INPUT ConfName CConfYearConfST CINPUT ConfNameConfYearConfST ;DATALINES; Input dataSUGI 2006 CAPHARMASUG 2006 FLNESUG 2006 PAWUSS 2006 CASESUG 2006 GASCSUG 2006 TXMWSUG 2006 MIPNWSUG 2006 ORSASGLOBALFORUM 2007 FL No longer column-driven.Using INFILE and INPUT Statements to Introduce External Data into the SAS System65

Using INFILE and INPUT Variable Length FilesDATA SasConf;/*INFILE 'C:\DocumentsDocuments\SASHOW07\CoLENGTH ConfName 14INFILE DATALINESGet rid/*INPUT ConfName CHARConfYearConfST CHAR Why?INPUT ConfNameConfYearConfST ;DATALINES; FORMATS andPOINTERSof them!Using INFILE and INPUT Statements to Introduce External Data into the SAS System66

Using INFILE and INPUT Variable Length FilesDATA Sa

Using INFILE and INPUT Statements to Introduce External Data into the SAS System 11 Using INFILE and INPUT Basic INFILE / INPUT SUGI 2006 San Francisco CA PHARMASUG 2006 Bonita Springs FL NESUG 2006 Philadelphia PA WUSS 2006 Irvine CA SESUG 2006 Atlanta GA SCSUG 2006 Irving TX