26 readvarlengthfile26.docx

上传人:b****9 文档编号:25221303 上传时间:2023-06-06 格式:DOCX 页数:38 大小:79.43KB
下载 相关 举报
26 readvarlengthfile26.docx_第1页
第1页 / 共38页
26 readvarlengthfile26.docx_第2页
第2页 / 共38页
26 readvarlengthfile26.docx_第3页
第3页 / 共38页
26 readvarlengthfile26.docx_第4页
第4页 / 共38页
26 readvarlengthfile26.docx_第5页
第5页 / 共38页
点击查看更多>>
下载资源
资源描述

26 readvarlengthfile26.docx

《26 readvarlengthfile26.docx》由会员分享,可在线阅读,更多相关《26 readvarlengthfile26.docx(38页珍藏版)》请在冰豆网上搜索。

26 readvarlengthfile26.docx

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 哲学历史

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1