26 readvarlengthfile26.docx
- 文档编号:25221303
- 上传时间:2023-06-06
- 格式:DOCX
- 页数:38
- 大小:79.43KB
26 readvarlengthfile26.docx
《26 readvarlengthfile26.docx》由会员分享,可在线阅读,更多相关《26 readvarlengthfile26.docx(38页珍藏版)》请在冰豆网上搜索。
26readvarlengthfile26
Overview
Introduction
Someoperatingsystemshavedifferenttypesofrecordformatsforexternalfiles.Anexternalfilethathasafixed-lengthrecordformathasanend-of-recordmarkerafterapredeterminednumberofcolumns.Atypicalrecordlengthis80columns.
Or,externalfilescanhaveavariable-lengthrecordformat,inwhichanend-of-recordmarkerisplacedafterthelastfield.
Whenrecordscontainfieldsthathavevaryinglengthsoravaryingnumberoffields,avariable-lengthrecordformatusesspacemoreefficientlythanafixed-lengthrecordformat.
SettingUpFilerefsforPracticesinThisLesson
SubmitthisSASprogramtosetupfilerefsforpractices.IfyouhavealreadydonethisinthecurrentSASsession,youcanskipthistask.(Thisprogramisthesameforalllessons.)
TimetoComplete
Thislessoncontains23pagesandtakesapproximately30minutestocomplete.
Objectives
Inthislesson,youlearnto
∙determinethelengthofeachrecord
∙readcharactervariablesthat havevaryinglengths.
Youcanalsoreviewhowto
∙usealine-holdspecifiertoholdthecurrentrecord
∙executeSASstatementsbasedonthevalueofavariable
∙explicitlywriteanobservationtoadataset.
Prerequisites
Beforetakingthislesson,youshouldcompletethefollowinglessons:
∙BasicConcepts
∙UsingtheProgrammingWorkspace
∙ReferencingFilesandSettingOptions
∙EditingandDebuggingSASPrograms
∙CreatingListReports
∙CreatingSASDataSetsfromRawData
∙UnderstandingDATAStepProcessing
∙ReadingRawDatainFixedFields
∙ReadingFree-FormatData
∙CreatingMultipleObservationsfromaSingleRecord.
ReadingVariable-LengthFields
TheLENGTH=Option
EachrecordinthefilethatisreferencedbythefilerefPhonedatcontainsavaluefortheIDnumber,name,andphoneextensionofanemployee. Thefirstandthirdfields(IDandPhoneExt)havethesamelengthineveryrecord.However,thesecondfield(Name)variesinlengthfromrecordtorecord.
RawDataFilePhonedat
1---+----10---+----20
1802JOHNSON2123
1803BARKER2142
1804EDMUNDSON2325
1805RIVERS2543
1806MASON2646
1807JACKSON2049
1808LEVY2856
1809THOMAS2222
1810WARREN2003
1811VANDENBERGH2110
1812YEAGER2145
1813SMITH2025
1814HINES2243
1815TAYLOR2746
AsyouwritetheDATAsteptoreadthesevalues,youmustaccommodatethevaryinglengthofNameineachindividualrecord.
dataperm.phones;
infilephonedat...;
Butfirst,youneedtodeterminethetotallengthofeachrecord.ThisiseasilyaccomplishedbyusingtheLENGTH=option.
Generalform,INFILEstatementwithLENGTH=option:
INFILE file-specificationLENGTH=variable;
wherevariabledefinesanumericvariablewhosevalueissettothelengthofthecurrentrecord.
AlthoughtheLENGTH=optionisspecifiedintheINFILEstatement,thevariableisnotassignedavalueuntilanINPUTstatementisexecutedandSASdeterminesthelengthoftherecordthatisbeingread.
Here,thelengthofeachrecordisassignedtothevariablereclen.Aseachrecordisread,thevalueofreclenchanges.
dataperm.phones;
infilephonedatlength=reclen;
input...;
ReadingVariable-LengthFields(continued)
HoldingtheFirstRecord
Let'sseehowwecanusereclentodeterminethelengthofNameineachrecord.YouknowthatthevaluesforeachemployeeIDnumberarealwayslocatedincolumns1-4ofeveryrecord.SotheINPUTstatementreadsthevaluesforID.
Youmustholdtherecordwiththesingletrailing@line-holdspecifieruntilthevaluesforNameandPhoneExtareread.
RawDataFilePhonedat
1---V----10---+----20
1802JOHNSON2123
1803BARKER2142
1804EDMUNDSON2325
1805RIVERS2543
1806MASON2646
1807JACKSON2049
1808LEVY2856
1809THOMAS2222
dataperm.phones;
infilephonedatlength=reclen;
inputID4.@;
ReadingVariable-LengthFields(continued)
StoringtheCurrentLengthofaVariable-LengthField
Nowlet'sthinkabouthowtogiveinstructionstoSASforreadingthevaluesofName.First,youneedtocreateavariablethatcontainsthelengthofeachrawdatavalueofName.
Let'scallthevariablenamelen.Asyoucansee,thevalueofnamelenvaries.
dataperm.phones;
infilephonedatlength=reclen;
inputID4.@;
namelen=?
?
?
?
?
?
Butremember,eachrecordcontainsonlythreefields,andthelengthofbothIDandPhoneExtis4ineveryrecord.So,thevaluesforIDandPhoneExtoccupyonly8spacesineachrecord.Therefore,thevalueofnamelenisthelengthofeachrecord(reclen)minusthelengthofIDandPhoneExt(8).
dataperm.phones;
infilephonedatlength=reclen;
inputID4.@;
namelen=reclen-8;
ReadingVariable-LengthFields(continued)
ReadingVariable-LengthValues
ThenextstepistoreadthevaluesofNameandPhoneExt.Becausethedataisnotinfixedfieldsandisnotseparatedbydelimiters,columninputandlistinputcannotbeused.The$w.informatisnoteffectivebecausethewvaluespecifiesonlyonefieldlength.So,youneedaspecialinformatthatreadsfieldsthathavevaryinglengths.
The$VARYINGw.informatisaspecialSASinformatthatenablesyoutoreadacharactervaluewhoselengthdiffersfromrecordtorecord.
Generalform,INPUTstatementwiththe$VARYINGw.informat:
INPUTvariable$VARYINGw.length-variable;
where
∙variableisacharactervariable
∙w.specifiesthemaximumlengthofthevariable
∙length-variablespecifiesanumericvariablethatcontainstheactuallengthofthevariableinthecurrentrecord.
RawDataFilePhonedat
1---+----10---+----20
1802JOHNSON2123
1803BARKER2142
1804EDMUNDSON2325
1805RIVERS2543
1806MASON2646
1807JACKSON2049
1808LEVY2856
1809THOMAS2222
It'simportanttounderstandhowthesyntaxofthe$VARYINGw.informatisdifferentfromthatofothercharacterinformats.Thesyntaxiscomposedofthreeparts:
theinformatname,themaximumlength(w.),andthelengthvariable.
INPUTvariable$VARYINGw.length-variable;
ThesecondINPUTstatementusesthe$VARYINGw.informattoreadthevaluesforName,startingincolumn5andcontinuingforthenumberofcolumnsthatarespecifiedbynamelen.ThenthevaluesforPhoneExtareread.
dataperm.phones;
infilephonedatlength=reclen;
inputID4.@;
namelen=reclen-8;
inputName$varying10.namelen
PhoneExt;
ReadingVariable-LengthFields(continued)
NoticethatthewvaluethatisspecifiedintheinformatisthemaximumlengthofNameandnottheactualnumberofcolumnsthatareread.
dataperm.phones;
infilephonedatlength=reclen;
inputID4.@;
namelen=reclen-8;
inputName$varying10.namelen
PhoneExt;
WhentheDATAstepexecutesforthefirsttime,therecordlengthis15,sothevalueofnamelenis15-8,or7.Becausethevalueofnamelenis7,only7columnsareread,eventhoughthewvalueis10.
| 7 |
1---+----10---V----20
1802JOHNSON2123
1803BARKER2142
1804EDMUNDSON2325
1805RIVERS2543
1806MASON2646
1807JACKSON2049
1808LEVY2856
1809THOMAS2222
Butsupposethereisalaterrecordinwhichthevalueofnamelenexceedsthewvaluethatisspecifiedinthe$VARYINGw.informat.Forexample,inthefollowingrecordthevalueofnamelenis11.However,themaximumlengthofthevariableNameis10,soonly10columnsareread,andthelastcharacteristruncated.
Themissingcharacter,H,isthenreadaspartofPhoneExt,whichisanumericvariable.Therefore,thevalueofofPhoneExtisinvalidandissettomissingintheProgramDataVector.
| 11 |
1---+----10---V----20
1811VANDENBERGH2110
1812YEAGER2145
1813SMITH2025
1814HINES2243
Asyoucansee,it'simportanttospecifyawvaluethatislargeenoughtoaccommodatethelongestvalue.
dataperm.phones;
infilephonedatlength=reclen;
inputID4.@;
namelen=reclen-8;
inputName$varying12.namelen
PhoneExt;
1---+----10---+----20
1802JOHNSON2123
1803BARKER2142
1804EDMUNDSON2325
1805RIVERS2543
1806MASON2646
1807JACKSON2049
1808LEVY2856
1809THOMAS2222
1810WARREN2003
1811VANDENBERGH2110
1812YEAGER2145
1813SMITH2025
1814HINES2243
1815TAYLOR2746
ReadingVariable-LengthFields(continued)
ReadingCharacterVariablesThatHaveVaryingLengths
WhentheDATAstephascompletelyexecuted,youcanusePROCPRINTtodisplaythedataset.Noticethatthevariablereclenisnotshown.
procprintdata=perm.phones;
run;
Obs
ID
namelen
Name
PhoneExt
1
1802
7
JOHNSON
2123
2
1803
6
BARKER
2142
3
1804
9
EDMUNDSON
2325
4
1805
6
RIVERS
2543
5
1806
5
MASON
2646
6
1807
7
JACKSON
2049
7
1808
4
LEVY
2856
8
1809
6
THOMAS
2222
9
1810
6
WARREN
2003
10
1811
11
VANDENBERGH
2110
AvariablethatisdefinedbytheLENGTH=optionisnotstoredaspartoftheobservation.Sowhenyoudisplaythedataset,valuesforreclenarenotshown.Instead,theminimumandmaximumvaluesofreclenarenotedintheSASlog.
SASLog
NOTE:
14recordswerereadfromtheinfilePHONEDAT.
Theminimumrecordlengthwas12.
Themaximumrecordlengthwas19.
NOTE:
ThedatasetPERM.PHONEShas14observations
and4variables.
Ifyoudonotwanttoprintthevariablenamelen,youcanusetheDROP=optioninthePRINTstatement.
procprintdata=perm.phones(drop=namelen);
run;
Obs
ID
Name
PhoneExt
1
1802
JOHNSON
2123
2
1803
BARKER
2142
3
1804
EDMUNDSON
2325
4
1805
RIVERS
2543
5
1806
MASON
2646
6
1807
JACKSON
2049
7
1808
LEVY
2856
8
1809
THOMAS
2222
9
1810
WARREN
2003
10
1811
VANDENBERGH
2110
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 26 readvarlengthfile26
![提示](https://static.bdocx.com/images/bang_tan.gif)