Upload
carmella-maryann-wilkinson
View
224
Download
0
Embed Size (px)
Citation preview
Using Advanced INPUT Techniques
Peter CosetteDave Hall
Amy Dunn-RuizEric 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
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
Inputting dataWith missing valuesmissing.txt:1 1 23 58 13 2134 55 89
Use missover
Inputting data with missing valuesshort.txt001Amy Dunn-Ruiz 100 90 93002Peter Cossette 95 88003David Hall 85 82 96004Eric Lyon 98100 95
Use Pad
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
Data Example for End=Last & Obsdays.txtSun 0Mon 0Tue 8Wed 2Thu 10Fri 3Sat 1
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
Reading in Multiple FilesThe WildcardEnd=FinishedFilenameDummy Variable
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
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;
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
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
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
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)
Data in Columns12345678901234
HondaCivic1996
112654 3599
Ford Focus2002
83775 7999
BMW X32006
3026017999
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
Mixing Record Types withConditional Input
star89.txt012345678901234567
001875378 2008
002848791352009
003565943212009
004999194842009
005365412 2008
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
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
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.);
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…….
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!