超经典的SAS BASE的笔记2.docx
- 文档编号:9438365
- 上传时间:2023-05-19
- 格式:DOCX
- 页数:23
- 大小:39.94KB
超经典的SAS BASE的笔记2.docx
《超经典的SAS BASE的笔记2.docx》由会员分享,可在线阅读,更多相关《超经典的SAS BASE的笔记2.docx(23页珍藏版)》请在冰点文库上搜索。
超经典的SASBASE的笔记2
Topic:
AccessingDataandCreatingDataStructures
1.ReadingrawdatafilesusingINFILEandINPUTstatement
2.Writing_NULL_DataSet
3.Assigningandchangevariableattributes
4.ImportdatabasetableordatafileintoSASdataset
5.Labelingvariables
6.ReadingexistingSASdataset
7.Restrictingobservationswhilereadingdata
8.CreatingtemporaryandpermanentSASdatasets
9.Exportingdatatodifferentfiles
10.Displayingcontentsofdataset
11.RestrictingobservationsandvariablesinaSASdatasetprocessed
1.ReadingrawdatafilesusingINFILEandINPUTstatement
]
1.1Introduction
1.1.1CommonStepBoundaryKeywords:
DATAPROCCARDSDATALINESQUITRUN
1.1.2DataStepFlow
datasales;
infilerawin;
inputname$1-10division$12years15-16sales19-25;
run;
procprintdata=sales;
run;
Note:
TheuseofRUNaftereachstepishighlyrecommended
A.TheCompilationPhase
WhenyousubmitaDATAstepforexecution,SASchecksthesyntaxoftheSASstatementsandcompilesthem,thatis,automaticallytranslatesthestatementsintomachinecode.Inthisphase,SASidentifiesthetypeandlengthofeachnewvariable,anddetermineswhetheratypeconversionisnecessaryforeachsubsequentreferencetoavariable.Duringthecompilephase,SAScreatesthefollowingthreeitems:
inputbuffer
isalogicalareainmemoryintowhichSASreadseachrecordofrawdatawhenSASexecutesanINPUTstatement.NotethatthisbufferiscreatedonlywhentheDATAstepreadsrawdata.(WhentheDATAstepreadsaSASdataset,SASreadsthedatadirectlyintotheprogramdatavector.)
programdatavector(PDV)
isalogicalareainmemorywhereSASbuildsadataset,oneobservationatatime.Whenaprogramexecutes,SASreadsdatavaluesfromtheinputbufferorcreatesthembyexecutingSASlanguagestatements.Thedatavaluesareassignedtotheappropriatevariablesintheprogramdatavector.Fromhere,SASwritesthevaluestoaSASdatasetasasingleobservation.
Alongwithdatasetvariablesandcomputedvariables,thePDVcontainstwoautomaticvariables,_N_and_ERROR_.The_N_variablecountsthenumberoftimestheDATAstepbeginstoiterate.The_ERROR_variablesignalstheoccurrenceofanerrorcausedbythedataduringexecution.Thevalueof_ERROR_iseither0(indicatingnoerrorsexist),or1(indicatingthatoneormoreerrorshaveoccurred).SASdoesnotwritethesevariablestotheoutputdataset.
descriptorinformation
isinformationthatSAScreatesandmaintainsabouteachSASdataset,includingdatasetattributesandvariableattributes.Itcontains,forexample,thenameofthedatasetanditsmembertype,thedateandtimethatthedatasetwascreated,andthenumber,namesanddatatypes(characterornumeric)ofthevariables.
B.TheExecutionPhase
Bydefault,asimpleDATAstepiteratesonceforeachobservationthatisbeingcreated.TheflowofactionintheExecutionPhaseofasimpleDATAstepisdescribedasfollows:
1.TheDATAstepbeginswithaDATAstatement.EachtimetheDATAstatementexecutes,anewiterationoftheDATAstepbegins,andthe_N_automaticvariableisincrementedby1.
2.SASsetsthenewlycreatedprogramvariablestomissingintheprogramdatavector(PDV).
3.SASreadsadatarecordfromarawdatafileintotheinputbuffer,oritreadsanobservationfromaSASdatasetdirectlyintotheprogramdatavector.YoucanuseanINPUT,MERGE,SET,MODIFY,orUPDATEstatementtoreadarecord.
4.SASexecutesanysubsequentprogrammingstatementsforthecurrentrecord.
5.Attheendofthestatements,anoutput,return,andresetoccurautomatically.SASwritesanobservationtotheSASdataset,thesystemautomaticallyreturnstothetopoftheDATAstep,andthevaluesofvariablescreatedbyINPUTandassignmentstatementsareresettomissingintheprogramdatavector.NotethatvariablesthatyoureadwithaSET,MERGE,MODIFY,orUPDATEstatementarenotresettomissinghere.
6.SAScountsanotheriteration,readsthenextrecordorobservation,andexecutesthesubsequentprogrammingstatementsforthecurrentobservation.
7.TheDATAstepterminateswhenSASencounterstheend-of-fileinaSASdatasetorarawdatafile.
Note:
ItshowsthedefaultprocessingoftheDATAstep.Youcancodedata-readingstatements(suchasINPUTorSET),ordata-writingstatements(suchasOUTPUT),inanyorderinyourprogram.
FlowofActionintheDATAStep
DiagnosingErrorsintheCompilationPhase
NowthatyouknowhowaDATAstepisprocessed,youcanusethatknowledgetocorrecterrors.Therewereerrorsthataredetectedduringthecompilationphase,including
∙misspelledkeywordsanddatasetnames
∙missingsemicolons
∙unbalancedquotationmarks
∙invalidoptions.
Duringthecompilationphase,SAScaninterpretsomesyntaxerrors(suchasthekeywordDATAmisspelledasDAAT).Ifitcannotinterprettheerror,SAS
∙printsthewordERRORfollowedbyanerrormessageinthelog
∙compilesbutdoesnotexecutethestepwheretheerroroccurred,andprintsthefollowingmessagetowarnyou:
NOTE:
TheSASSystemstoppedprocessingthisstepbecauseoferrors.
SomeerrorsareexplainedfullybythemessagethatSASprints;othererrormessagesarenotaseasytointerpret.Forexample,becauseSASstatementsarefree-format,whenyoufailtoendaSASstatementwithasemicolon,SASdoesnotalwaysdetecttheerroratthepointwhereitoccurs.
DiagnosingErrorsintheExecutionPhase
Asyouhaveseen,errorscanoccurinthecompilationphase,resultinginaDATAstepthatiscompiledbutnotexecuted.Errorscanalsooccurduringtheexecutionphase.WhenSASdetectsanerrorintheexecutionphase,thefollowingcanoccur,dependingonthetypeoferror:
∙Anote,warning,orerrormessageisdisplayedinthelog.
∙Thevaluesthatarestoredintheprogramdatavectoraredisplayedinthelog.
∙Theprocessingofthestepeithercontinuesorstops.
1.2BasicFormsofINPUTStatement
ThemostcommonwaytocreatenewdatasetsisbysubmittingaDATAstep.TheINPUTstatementdescribeswhatdatawillbecontainedinyournewdataset.Itisusedtoreaddatafromanexternalsource,orfromlinescontainedinyourSASprogram.
1.2.1ListInput
UsetheListinputmodetoreaddatarecordedwithatleastoneblankspaceseparatingeachdatafield.Missingvaluesarerepresentedasadot(period).Thisisthesimpleformofinput(freeformlistorformat-free).
DATACensus;INPUTState$Pop@@;
CARDS;
NC5.085SC2.590VA1.360MA3.450PA.
;run;
1.2.2ColumnInput
UseColumninputmodetoreadthefollowingtypeofdata.Thevariablesmustbelistedintheorderinwhichtheyappearintheinputdata.
-Characterandnumericdata
-Datavalueswhichareenteredinfixedcolumnpositions
-Charactervalueslongerthaneightcharacters
-Charactervaluesthatcontainembeddedblanks
Example:
INPUTvariable
DATACensus;
INPUTState$1-2Pop3-7;
CARDS;
NC5.082
SC2.590
VA1.360
;
run;
DATACensus2;
INPUTState$1-10Pop11-15;
CARDS;
NewYork5.082
NewJersey2.590
;
run;
Thenumbersaftereachvariablenameintheinputstatementindicatethecolumnsinwhichthisvariablecanbefound.The$indicatesthatthevariableischaracter.
Noticethattheprimarydifferencebetweenthis“column”inputstatementandthe“freeformlist”inputstatementistheinclusionofcolumnrangestellingSASwhereinthedatasettofindtheinformationforeachvariable.
1.2.3FormattedInput
Formattedinputisatechniqueforreadingdatathatallowsyoutospecifythebeginningcolumnofafiledandoptionallyitstypeandformat.Useformattedinputmodetoreadthefollowing:
Form:
INPUTpointercontrolvariable
Example:
datainpt;
input@1custom_id$6.
@7custom_name$6.
@14rental_fee;
datalines;
240442Smith950
240910Yang1120
240808Andrew1050
;
Limitationsandadvantagesofformattedinputinclude:
▪Datamustbeinfixedcolumns
▪Datafieldsmaybereadinanyorder.
▪Blanksneednottoseparatefields.
▪Inputonlythevariablesyouneed--skiptherest.
▪Readindatausinginformatandformatspecifications.
Format
Specifiesvalues...
Example
COMMAw.d
thatcontaincommasanddecimalplaces
comma8.2
DOLLARw.d
thatcontaindollarsignsandcommas
dollar6.2
MMDDYYw.
asdatevaluesoftheform09/12/97(MMDDYY8.)or09/12/1997(MMDDYY10.)
mmddyy10.
w.
roundedtothenearestintegerinwspaces
7.
w.d
roundedtoddecimalplacesinwspaces
8.2
$w.
ascharactervaluesinwspaces
$12.
DATEw.
asdatevaluesoftheform16OCT99(DATE7.)or16OCT1999(DATE9.)
date9.
1.2.4NameInput
Youcanusenamedinputtoreadrecordsinwhichdatavaluesareprecededbythenameofthevariableandanequalsign(=).ThefollowingINPUTstatementreadsthedatalinescontainingequalsigns.
datagames;
inputname=$score1=score2=;
datalines;
name=rileyscore1=1132score2=1187
;
procprintdata=games;
run;
Note:
Allformsofinput,exceptthenamedinput,canbeusedinanycombination
1.2.5MultipleStylesinaSingleINPUTStatement
datamul;
inputidnoname$18.team$24-30startwghtendwght;
cards;
023DavidShawred189165
049AmeliaSerranoyellow189165
;
ThevalueofIDNO,STARTWGHT,andENDWGHTarereadwithlistinput,thevalueofNAMEwithformattedinput,andthevalueofTEAMwithcolumninput.
1.3Pointer Controls
AsSASreadsvaluesfromtheinputdatarecordsintotheinputbuffer,itkeepstrackofitspositionwithapointer.TheINPUTstatementprovidesthreewaystocontrolthemovementofthepointe
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 超经典的SAS BASE的笔记2 经典 SAS BASE 笔记
![提示](https://static.bingdoc.com/images/bang_tan.gif)