23
Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Embed Size (px)

Citation preview

Page 1: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Using Advanced INPUT Techniques

Peter CosetteDave Hall

Amy Dunn-RuizEric Lyon

Page 2: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Advanced Input OutlineHandling Missing Values and Short Data LinesDetecting the End of FilesAdvanced Options for Reading Data FilesReading Data ConditionallyThe Single Trailing @ and the Double Trailing

@@Using Variable and Informant ListsCreating Multiple Obs. from One Line of InputRelative Column Pointers

Page 3: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

LOG: “SAS went to a new line when INPUT statement reached past end of the line”Solutions

Missover sets variable to missing if it has more variables than

data values in one linePad

When doing column input this command pads each line with blanks up to 256 bytes

Truncover Good for reading in variable length records with

missing data

Page 4: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Inputting dataWith missing valuesmissing.txt:1 1 23 58 13 2134 55 89

Use missover

Page 5: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Inputting data with missing valuesshort.txt001Amy Dunn-Ruiz 100 90 93002Peter Cossette 95 88003David Hall 85 82 96004Eric Lyon 98100 95

Use Pad

Page 6: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

End=Last Optionlast is a temporary variable that is false(0)

until last record of external file is read in, then it is true(1)

end is also a set option and used to determine when you are reading the last observation in a SAS data set

Page 7: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Data Example for End=Last & Obsdays.txtSun 0Mon 0Tue 8Wed 2Thu 10Fri 3Sat 1

Page 8: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Obs and First ObsNo matter how many observations are in the

file, if you put obs=3 it will only reads in the first 3

Use FirstObs to read in selected observations in the middle of a data set

firstobs=4 obs=7Useful for large data sets where you need to

test stuff out

Page 9: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Reading in Multiple FilesThe WildcardEnd=FinishedFilenameDummy Variable

Page 10: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

The ? WildcardIf you have multiple files with similar names

like: Person1, Person2, Person3,…. data wild;

infile 'e:\files\info\person?.txt';input Name Style Letter;

run;You can use a * wildcard to allow for more than

one wildcard digit

Page 11: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

End=Finisheddata lotsofiles;

if finished = 0 then infile ‘grover.txt' end=finished;else infile ‘bigbird.txt';else infile ‘snuffy.txt’;input Name Style Place;

run;Use this to read in one file, then when that's

done, it moves on to read in the next file, and so on;

Page 12: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Filenamefilename super (‘grover.txt’ ‘oscar.txt’

‘bert.txt);data lotsofiles;

infile super;input Name Place Style;

run;

We called our file super, you can name it anything you want

This is a little less typing than End=Finished method

Page 13: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Dummy with External Filedata lotsofiles;

infile 'e:\super.txt';input External $ 30.;infile dummy filevar=External end=Last;do until (last);

input Name Place Style;output;

end;run;

E:\super.txt contains a list of all of the filenames we want to read in

Easier to use than End=Finshed or Filename when you have LOTS of files to read in

Page 14: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Dummy with Datalinesdata lotsofiles;input External $ 30.;

infile dummy filevar=External end=Last;do until (last);

input Name Place Style;output;

end;datalines;e:\wilma.txte:\betty.txte:\fred.txte:\barney.txtetc.....;run;Same as previous slide, but with datalines not an external file

Page 15: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Reading Multiple Lines of DataName lines #1, #2, #3, etc and it reads them

in together as one lineOr use a slash to indicate where the lines are

separated (though this is not always super clear, author doesn't like this technique)

Page 16: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Data in Columns12345678901234

HondaCivic1996

112654 3599

Ford Focus2002

83775 7999

BMW X32006

3026017999

Page 17: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Mixing Record Types withConditional InputIf some data you are reading in has more

variables than other dataUsing an @ sign fixes the errors, it is the

absolute column pointer

Page 18: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Mixing Record Types withConditional Input

star89.txt012345678901234567

001875378 2008

002848791352009

003565943212009

004999194842009

005365412 2008

Page 19: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

More Trailing @ Sign For Use as a FilterThe @ tells computer not to move on after

reading in Year, lets it do the if statement rather than go onto next line

Page 20: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

The Trailing @@When there are, for example, only 2 variables

but more than 2 things on the dataline Normally SAS won't read in all the data off

that line, moves to next line after inputting the first 2 values on the line

@@ makes everything nice again, UNLESS THERE'S A MISSING VALUE...THEN IT WON'T WORK

Page 21: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Using Variable & Informat ListsYou can supply a single informat to a list of

variables and save typinginput (FirstName LastName MT1-MT3)(2*$10.

3*$2.);

Page 22: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Using Relative Column Pointers to Read a Complex Data Structure EffectivelyThe + sign is a relative column pointerinput @1 Age1 2. @3 Wt1 3.

@6 Age2 2. @8 Wt2 3.@11 Age3 2. @13 Wt3 3.

etc…….

Page 23: Using Advanced INPUT Techniques Peter Cosette Dave Hall Amy Dunn-Ruiz Eric Lyon

Summary of Key TermsMissover - Used to handle data with missing valuesPad – Used to handle column data with missing valuesEnd = Last - Used to detect the end of a data fileObs - used to read the first n obs. from a data fileFirstobs - Sets the first obs. read from a data fileFilevar – Used to specify filenameSingle @ - Used to “hold the line” for conditional inputDouble @@ - Use with Caution!Informant Lists & Relative Column Pointers – Useful!