sas programming ii manipulating data with the data step

sas programming ii manipulating data with the data step

ID:30479971

大小:2.40 MB

页数:465页

时间:2018-12-30

上传者:U-14522
sas programming ii manipulating data with the data step_第1页
sas programming ii manipulating data with the data step_第2页
sas programming ii manipulating data with the data step_第3页
sas programming ii manipulating data with the data step_第4页
sas programming ii manipulating data with the data step_第5页
资源描述:

《sas programming ii manipulating data with the data step》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库

®SASProgrammingII:ManipulatingDatawiththeDATAStepCourseNotes ®SASProgrammingII:ManipulatingDatawiththeDATAStepCourseNoteswasdevelopedbyJemshaidCheemaandMelindaThielbar.AdditionalcontributionsweremadebyMartyHultgren,KentReeve,WarrenRepole,andRogerStaum.EditingandproductionsupportwasprovidedbytheCurriculumDevelopmentandSupportDepartment.SASandallotherSASInstituteInc.productorservicenamesareregisteredtrademarksortrademarksofSASInstituteInc.intheUSAandothercountries.®indicatesUSAregistration.Otherbrandandproductnamesaretrademarksoftheirrespectivecompanies.®SASProgrammingII:ManipulatingDatawiththeDATAStepCourseNotesCopyright2004bySASInstituteInc.,Cary,NC27513,USA.Allrightsreserved.PrintedintheUnitedStatesofAmerica.Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmitted,inanyformorbyanymeans,electronic,mechanical,photocopying,orotherwise,withoutthepriorwrittenpermissionofthepublisher,SASInstituteInc.Bookcode59798,coursecodePROG2,prepareddate22Apr04. ForYourInformationiiiTableofContentsCourseDescription......................................................................................................................viPrerequisites...............................................................................................................................viiGeneralConventions.................................................................................................................viiiChapter1Introduction..........................................................................................1-11.1Overview..........................................................................................................................1-31.2ReviewofSASBasics.....................................................................................................1-71.3ReviewofDATAStepProcessing..................................................................................1-151.4ReviewofDisplayingSASDataSets............................................................................1-211.5WorkingwithExistingSASDataSets...........................................................................1-281.6PrerequisiteSyntax(Self-Study)....................................................................................1-441.7NavigatingtheSASWindowingEnvironment(Self-Study).........................................1-47Chapter2ControllingInputandOutput..............................................................2-12.1OutputtingMultipleObservations...................................................................................2-32.2WritingtoMultipleSASDataSets................................................................................2-152.3SelectingVariablesandObservations............................................................................2-232.4WritingtoanExternalFile.............................................................................................2-392.5SolutionstoExercises....................................................................................................2-58Chapter3SummarizingData................................................................................3-13.1CreatinganAccumulatingTotalVariable........................................................................3-33.2AccumulatingTotalsforaGroupofData......................................................................3-123.3SolutionstoExercises....................................................................................................3-30 ivForYourInformationChapter4ReadingandWritingDifferentTypesofData.....................................4-14.1ReadingDelimitedRawDataFiles..................................................................................4-34.2ControllingWhenaRecordLoads.................................................................................4-304.3ReadingHierarchicalRawDataFiles............................................................................4-564.4SolutionstoExercises....................................................................................................4-90Chapter5DataTransformations...........................................................................5-15.1Introduction......................................................................................................................5-35.2ManipulatingCharacterValues........................................................................................5-85.3ManipulatingNumericValues.......................................................................................5-395.4ManipulatingNumericValuesBasedonDates..............................................................5-485.5ConvertingVariableType..............................................................................................5-535.6SolutionstoExercises....................................................................................................5-71Chapter6DebuggingTechniques(Self-Study)...................................................6-16.1UsingthePUTStatement.................................................................................................6-36.2UsingtheDEBUGOption.............................................................................................6-14Chapter7ProcessingDataIteratively..................................................................7-17.1DOLoopProcessing........................................................................................................7-37.2SASArrayProcessing....................................................................................................7-277.3UsingSASArrays..........................................................................................................7-387.4SolutionstoExercises....................................................................................................7-53®Chapter8CombiningSASDataSets.................................................................8-18.1Match-MergingTwoorMoreSASDataSets..................................................................8-38.2SimpleJoinsUsingtheSQLProcedure(Self-Study)....................................................8-19 ForYourInformationv8.3SolutionstoExercises....................................................................................................8-31Chapter9LearningMore.......................................................................................9-19.1WhereDoIGoFromHere?.............................................................................................9-39.2SASResources.................................................................................................................9-6AppendixAIndex.....................................................................................................A-1 viForYourInformationCourseDescriptionThisLevelIIIcourseisdesignedforexperiencedSASprogrammerswhowanttobuildonthe®skillsacquiredintheSASProgrammingI:Essentialscourse.ThecoursefocusesonhowtomanageSASdatasetinputandoutput,workwithdifferentdatatypes,andmanipulatedata.Aftercompletingthiscourse,youshouldbeableto•readandwritedifferenttypesofdata•combineSASdatasets•summarizedata•performdatamanipulationandtransformationsusingSASfunctions•processdataiteratively•usearrays•understandDATAstepprocessing.Tolearnmore…Afullcurriculumofgeneralandstatisticalinstructor-basedtrainingisavailableatanyoftheInstitute’strainingfacilities.Instituteinstructorscanalsoprovideon-sitetraining.Forinformationonothercoursesinthecurriculum,contacttheSASEducationDivisionat1-919-531-7321,orsende-mailtotraining@sas.com.YoucanalsofindthisinformationontheWebatsupport.sas.com/training/aswellasintheTrainingCourseCatalog.ForalistofotherSASbooksthatrelatetothetopicscoveredinthisCourseNotes,USAcustomerscancontactourSASPublishingDepartmentat1-800-727-3228orsende-mailtosasbook@sas.com.CustomersoutsidetheUSA,pleasecontactyourlocalSASoffice.Also,seethePublicationsCatalogontheWebatsupport.sas.com/pubsforacompletelistofbooksandaconvenientorderform. ForYourInformationviiPrerequisites®Beforeattendingthiscourse,youshouldhavecompletedtheSASProgrammingI:EssentialscourseorhaveatleastsixmonthsofexperiencewritingSASprograms.Specifically,youshouldbeableto•createandaccessfilesinyouroperatingenvironment•explainthestructureofaSASprogram•explorethestructureandcontentsofaSASdataset•distinguishbetweensyntaxanddataerrors•debugaSASprogram•createaSASdatasetfromafixed-formatexternalfile•subsettherowsandcolumnsofaSASdataset•createderivedvariables•writeconditionallogicstatements•readandwriteSASdatevalues•accessSASdatalibraries•sortaSASdataset•readoneormoreSASdatasetsusingaSETstatement•performasimplemergeusingaMERGEstatement•createdetailandsummaryreportsfromaSASdataset•placetitlesandfootnotesonreports.®YoucangainthisrecommendedknowledgeoftheSASSystemfromtheSASProgrammingI:Essentialscourse. viiiForYourInformationGeneralConventionsThissectionexplainsthevariousconventionsusedinpresentingtext,SASlanguagesyntax,andexamplesinthisbook.TypographicalConventionsYouwillseeseveraltypestylesinthisbook.Thislistexplainsthemeaningofeachstyle:UPPERCASEROMANisusedforSASstatementsandotherSASlanguageelementswhentheyappearinthetext.italicidentifiestermsorconceptsthataredefinedintext.Italicisalsousedforbooktitleswhentheyarereferencedintext,aswellasforvarioussyntaxandmathematicalelements.boldisusedforemphasiswithintext.monospaceisusedforexamplesofSASprogrammingstatementsandforSAScharacterstrings.Monospaceisalsousedtorefertovariableanddatasetnames,fieldnamesinwindows,informationinfields,anduser-suppliedinformation.selectindicatesselectableitemsinwindowsandmenus.Thisbookalsousesiconstorepresentselectableitems.SyntaxConventionsThegeneralformsofSASstatementsandcommandsshowninthisbookincludeonlythatpartofthesyntaxactuallytaughtinthecourse.Forcompletesyntax,seetheappropriateSASreferenceguide.PROCCHARTDATA=SAS-data-set;HBAR|VBARchart-variables;RUN;ThisisanexampleofhowSASsyntaxisshownintext:•PROCandCHARTareinuppercaseboldbecausetheyareSASkeywords.•DATA=isinuppercasetoindicatethatitmustbespelledasshown.•SAS-data-setisinitalicbecauseitrepresentsavaluethatyousupply.Inthiscase,thevaluemustbethenameofaSASdataset.•HBARandVBARareinuppercaseboldbecausetheyareSASkeywords.Theyareseparatedbyaverticalbartoindicatetheyaremutuallyexclusive;youcanchooseoneortheother.•chart-variablesisinitalicbecauseitrepresentsavalueorvaluesthatyousupply.•representsoptionalsyntaxspecifictotheHBARandVBARstatements.Theanglebracketsenclosetheslashaswellasoptionsbecauseifnooptionsarespecifiedyoudonotincludetheslash.•RUNisinuppercaseboldbecauseitisaSASkeyword. Chapter1Introduction1.1Overview..........................................................................................................................1-31.2ReviewofSASBasics....................................................................................................1-71.3ReviewofDATAStepProcessing...............................................................................1-151.4ReviewofDisplayingSASDataSets..........................................................................1-211.5WorkingwithExistingSASDataSets.........................................................................1-281.6PrerequisiteSyntax(Self-Study).................................................................................1-441.7NavigatingtheSASWindowingEnvironment(Self-Study)......................................1-47 1-2Chapter1Introduction 1.1Overview1-31.1OverviewObjectivesExplorethefunctionalityoftheDATAstep.3WhyUsetheDATAStep?TheDATAsteppermitstrueprogrammingfunctionality.Itisflexibleaccessible.TheDATAstepispartofBaseSASsoftware,whichmakesitavailableonalloperatingsystemsandforallSASusers.4 1-4Chapter1IntroductionWhatCantheDATAStepDo?YoucanusetheDATAstepinthefollowingwaystotransformyourinformation:ReadfromarawdatafileintotheSASSystem.SASDataSetRawDataFileDescriptorDATAStep5......WhatCantheDATAStepDo?CreatemultipleSASdatasetsinoneDATAstep.DATAStep6......WhatCantheDATAStepDo?Rotateadataset.IDQtr1Qtr2Qtr3Qtr4IDQtr1Qtr2Qtr3Qtr4E00224123322.E00224123322.E0036735484030E0036735484030IDQtrAmountIDQtrAmountE00224112E00224233E00224322E002244.E00367135E00367248E00367340E003674307...... 1.1Overview1-5WhatCantheDATAStepDo?Combineexistingdatasets.SASDataSet1SASDataSet2DATAStep8......WhatCantheDATAStepDo?Youcanalsoaddoraugmentinformationinavarietyofways.Createaccumulatingtotals.SaleSaleSaleDateAmtSaleDateAmtMth2DteMth2Dte01APR2001498.4901APR2001498.49498.49498.4902APR2001946.5002APR2001946.501444.991444.9903APR2001994.9703APR2001994.972439.962439.9604APR2001564.5904APR2001564.593004.553004.5505APR2001783.0105APR2001783.013787.563787.569......WhatCantheDATAStepDo?Manipulatenumericvalues.BirthDay4253AgeSASFunction3010...... 1-6Chapter1IntroductionWhatCantheDATAStepDo?Manipulatecharactervalues.HappyBirthDayHappyBirthDayHappy30thBirthday!30thHappy29thBirthday!29thSASFunction11......WhatCantheDATAStepDo?Summarizedatasets.SalaryDivSalaryDiv42000HUMRESDivDivSalDivDivSal42000HUMRES34000FINACE34000FINACE27000FLTOPSDATAStepFINACE73000FINACE7300027000FLTOPS20000FINACEFLTOPS46000FLTOPS4600020000FINACE19000FINACEHUMRES42000HUMRES4200019000FINACE19000FLTOPS19000FLTOPS12......Andmuch,muchmore.13 1.2ReviewofSASBasics1-71.2ReviewofSASBasicsObjectivesReviewfundamentalSASconcepts.ReviewcreatingaSASdatasetfromarawdatafile.15IndustryTerminologyComparisonDataProcessingSASSystemSQLfiledatasettablerecordobservationrowfieldvariablecolumn16 1-8Chapter1IntroductionStructureofSASDataSetsSASDataSetGeneralDataSetInformationNameNumberofObs.*LabelNumberofVariablesDate/TimeCreatedDescriptorPortionStorageInformationInformationforEachVariableNameTypeLengthPosition*Label*Format*InformatIDNUMNAMEWAGECATWAGERATE1351Farr,SueS3392.50161S5093.75Data212Moore,RonS.Portion2512...Ruth,GHS1572.50......5151Coxe,SusanS3163.0017AttributesofSASVariablesAllSASvariableshavethreerequiredattributes:nametypelength.18 1.2ReviewofSASBasics1-9VariableNamesTherulesfornamingSASdatasetsandvariablesarethesame.Namesmustbe1to32charactersinlengthmuststartwithaletter(A-Z)oranunderscore(_)cancontinuewithanycombinationofnumbers,letters,andunderscores.19InSASVersion8andhigher,SASvariablenamesaredisplayedinthecasethattheyarecreated.However,asinallversionsofSAS,variablenamesarenotcase-sensitivewithintheprogram.Thisenablesyoutocreatevariablenamesthatareeasiertoreadinreportswithoutworryingaboutcase-sensitivitywithinyourSASprograms.VariableTypeandLengthVariablescanbeCharacterCharacter(1to32K)(1to32K)NumericNumericbydefault,storedinfloatingpointbydefault,storedinfloatingpointrepresentationusing8bytesrepresentationusing8bytes20Itispossibletostorenumericvariablesusingfewerthan8bytes.However,reducingthelengthofnumericvariablesdecreasestheirprecisionandcanyieldunexpectedresults. 1-10Chapter1IntroductionSASDatesSASdatesarespecialnumericvaluesrepresentingthenumberofdaysbetweenJanuary1,1960andaspecifieddate.1jan19591jan19601jan19611jan2000DATE9.Informat-365036614610SASDateSASDateValuesValuesMMDDYY10.Format01010101/0/0/0/01/1/1/1/191919205960610021CreateaSASDataSetfromaRawDataFileE123215OCT199961065ArawdatafilecontainsE234101JUN199791688employeeinformationforE345226OCT199332639thelevel1flightattendants.E678116SEP199228305UsetherawdatafiletoE832126NOV199640440createthework.fltat1E105227FEB199739461SASdataset.E106210MAY198741463E817206JAN200040650E109120AUG19914095022Thisisafixed-columnrawdatafile.Eachdatafieldstartsinthesameposition,respectively,ineveryrecordofthefile.Youcanreadfixed-columnrawdatafileswitheithercolumnorformattedinput. 1.2ReviewofSASBasics1-11DesiredOutputHireObsEmpIDDateSalaryBonus1E123214532610653053.252E234113666916884584.403E345212352326391631.954E678111947283051415.255E832113479404402022.006E105213572394611973.057E10629991414632073.158E817214615406502032.509E109111554409502047.5023Inadditiontothefieldsintherawdatafile,thedesiredoutputhasabonusforeachemployee,whichis5%oftheemployee’ssalary.TheDATAStatementADATAstepalwaysbeginswithaDATAstatement.GeneralformofaDATAstatement:DATADATASAS-data-setSAS-data-set;;TheDATAstatementstartstheDATAstepandnamestheSASdatasetbeingcreated.24TheDATAstatementisawritinginstruction.OptionsintheDATAstatementaffecthowtheoutputdatasetiscreatedbutnothowthedataisread. 1-12Chapter1IntroductionTheINFILEStatementIfyouarereadingdatafromarawdatafile,youneedanINFILEstatement.GeneralformofanINFILEstatement:INFILEINFILE'raw-data-file'raw-data-file'';;TheINFILEstatementpointstotherawdatafilebeingread.OptionsintheINFILEstatementaffecthowSASreadstherawdatafile.25TheINPUTStatementWhenyoureadfromarawdatafile,theINPUTstatementfollowstheINFILEstatement.GeneralformofanINPUTstatement:INPUTINPUTvariable-specificationvariable-specification…;…;TheINPUTstatementdescribestherawdatafieldsandspecifieshowyouwantthemconvertedintoSASvariables.26FormattedInputTheinputstyletellsSASwheretofindthefieldsandhowtoreadthemintoSAS.INPUTINPUT@@nvariable-nameinformat....nvariable-nameinformat....;;@nmovesthepointertothestartingpositionofthefield.variable-namenamestheSASvariablebeingcreated.InformatspecifieshowmanypositionstoreadandhowtoconverttherawdataintoaSASvalue.27 1.2ReviewofSASBasics1-13TheINPUTStatementCommonSASinformats:$w.readsastandardcharacterfield,wherewspecifiesthewidthofthefieldinbytes.w.readsastandardnumericfield,wherewspecifiesthewidthofthefieldinbytesanddspecifiesthenumberofimplieddecimalpositions.DATE9.readsdatesintheform31DEC2012.28Aninformatisareadinginstruction.Theinformatthatisuseddependsontheformofthefieldintherawdatafile.UnlesstheseattributesarespecifiedbeforetheINPUTstatement,SASusestheinformattosetthetypeandlengthofthevariablesyoureadfromtherawdatafile.ForacompletelistofSASinformats,seetheSASdocumentation.TheAssignmentStatementTocreateanewvariableintheDATAstep,useanassignmentstatement:variable-namevariable-name==expressionexpression;;TheassignmentstatementcreatesaSASvariableandspecifieshowtocalculatethatvariable'svalue.29SASusestheexpressiontosetthetypeandlengthforthenewvariableunlessthoseattributesarespecifiedbeforetheassignmentstatement. 1-14Chapter1IntroductionCreateaSASDataSetfromaRawDataFiledatawork.fltat1;infile'raw-data-file';input@1EmpID$5.@7HireDatedate9.@17Salary5.;Bonus=.05*Salary;run;30CreateaSASDataSetfromaRawDataFilePartialLogNOTE:9recordswerereadfromtheinfile'fltat1.dat'.Theminimumrecordlengthwas21.Themaximumrecordlengthwas21.NOTE:ThedatasetWORK.FLTAT1has9observationsand4variables.31c01s2d1.sas 1.3ReviewofDATAStepProcessing1-151.3ReviewofDATAStepProcessingObjectivesReviewthetwophasesofDATAstepprocessing.33CreateaSASDataSetfromRawDatadatawork.fltat1;infile'raw-data-file';input@1EmpID$5.@7HireDatedate9.@17Salary5.;Bonus=.05*Salary;run;34 1-16Chapter1IntroductionProcessingtheDATAStepTheSASSystemprocessestheDATAstepintwophases:compilationexecution.35CompilationDuringcompilation,SAScheckscodeforsyntaxerrorstranslatescodetomachinecodeestablishesanareaofmemorycalledtheinputbufferifreadingrawdataestablishesanareaofmemorycalledtheProgramDataVector(PDV)assignsrequiredattributestovariablescreatesthedescriptorportionofthenewdataset.36RawDataFileCompiledatawork.fltat1;infile'raw-data-file';E123215OCT199961065input@1EmpID$5.E234101JUN199791688@7HireDateDate9.E345226OCT199332639@17Salary5.;E678116SEP199228305Bonus=.05*Salary;E832126NOV199640440E105227FEB199739461run;E106210MAY198741463E817206JAN200040650E109120AUG199140950InputBufferPDVEMPIDHIREDATESALARY$NN58837...... 1.3ReviewofDATAStepProcessing1-17RawDataFiledatawork.fltat1;infile'raw-data-file';E123215OCT199961065input@1EmpID$5.E234101JUN199791688@7HireDateDate9.E345226OCT199332639@17Salary5.;E678116SEP199228305Bonus=.05*Salary;E832126NOV199640440E105227FEB199739461run;E106210MAY198741463E817206JAN200040650E109120AUG199140950InputBufferPDVEMPIDHIREDATESALARYBONUS$NNN588838......TheINPUTstatementcreatesthevariablesEmpID,Salary,andHireDateandassignstypeandlengthforeachvariable.TheassignmentstatementcreatesBonusandsetsitstypeandlength.IfavariableisreferencedmultipletimesinaDATAstep,theattributesaresetatthefirstencounter.ExecutionDuringtheexecutionphase,SASinitializesthePDVtomissingreadsdatavaluesintothePDVcarriesoutassignmentstatementsandconditionalprocessingwritestheobservationinthePDVtotheoutputSASdatasetattheendoftheDATAstep(bydefault)returnstothetopoftheDATAstepinitializesanyvariablesthatarenotreadfromaSASdatasettomissing(bydefault)repeatstheprocess.39 1-18Chapter1IntroductionTheDATAStepIsaLoopInitializeInitializePDV.PDV.YESStopExecuteExecuteEndofDATA“read”statement.“read”statement.file?step.ExecuteExecuteNOprogramstatements.programstatements.OutputobservationtoOutputobservationtoSASdataset.SASdataset.40.........RawDataFileExecutiondatawork.fltat1;infile'raw-data-file';E123215OCT199961065input@1EmpID$5.E234101JUN199791688@7HireDatedate9.E345226OCT199332639E678116SEP199228305@17Salary5.;E832126NOV199640440Bonus=.05*Salary;E105227FEB199739461run;E106210MAY198741463E817206JAN200040650E109120AUG199140950InputBufferPDVEMPIDHIREDATESALARYBONUS...41......BeforethefirstexecutionoftheDATAstep,SASinitializesallvariablestomissing. 1.3ReviewofDATAStepProcessing1-19RawDataFiledatawork.fltat1;infile'raw-data-file';E123215OCT199961065input@1EmpID$5.E234101JUN199791688@7HireDatedate9.E345226OCT199332639E678116SEP199228305@17Salary5.;E832126NOV199640440Bonus=.05*Salary;E105227FEB199739461run;E106210MAY198741463E817206JAN200040650E109120AUG199140950InputBufferE123215OCT199961065PDVEMPIDHIREDATESALARYBONUSE123214532.61065..43......TheINPUTstatement1.loadsarecordintotheinputbuffer2.readsthespecifiedfieldsintothePDV.RawDataFiledatawork.fltat1;infile'raw-data-file';E123215OCT199961065input@1EmpID$5.E234101JUN199791688@7HireDatedate9.E345226OCT199332639E678116SEP199228305@17Salary5.;E832126NOV199640440Bonus=.05*Salary;E105227FEB199739461run;E106210MAY198741463E817206JAN200040650E109120AUG199140950.05*61065InputBufferE123215OCT199961065PDVEMPIDHIREDATESALARYBONUSE123214532.61065.3053.25.44...TheassignmentstatementcalculatesthevalueforBonusandassignsittothevariable. 1-20Chapter1IntroductionRawDataFiledatawork.fltat1;infile'raw-data-file';E123215OCT199961065input@1EmpID$5.E234101JUN199791688@7HireDatedate9.E345226OCT199332639E678116SEP199228305@17Salary5.;E832126NOV199640440Bonus=.05*Salary;E105227FEB199739461run;E106210MAY198741463E817206JAN200040650ImplicitReturnImplicitE109120AUG199140950OutputInputBufferE123215OCT199961065PDVEMPIDHIREDATESALARYBONUSE123214532.61065.3053.25.45Writeoutobservationtowork.fltat1.......ThereisanimplicitoutputatthebottomoftheDATAstep.Bydefault,SASoutputsoneobservationeverytimetheDATAstepexecutes.RawDataFiledatawork.fltat1;infile'raw-data-file';E123215OCT199961065input@1EmpID$5.E234101JUN199791688@7HireDatedate9.E345226OCT199332639E678116SEP199228305@17Salary5.;E832126NOV199640440Bonus=.05*Salary;E105227FEB199739461run;E106210MAY198741463E817206JAN200040650E109120AUG199140950InputBufferReinitializethePDV.ValuesE123215OCT199961065ofvariablesnotreadfromSASaresettomissing.PDVEMPIDHIREDATESALARYBONUS...48......Ateveryexecutionafterthefirst,allvariablesnotreadfromSASaresettomissing.ThisincludesvariablesreadwithanINPUTstatementandvariablescreatedwithanassignmentstatement. 1.4ReviewofDisplayingSASDataSets1-211.4ReviewofDisplayingSASDataSetsObjectivesReviewproceduresthatdisplaySASdatasets.57CreateaSASDataSetfromRawDatadatawork.fltat1;infile'raw-data-file';input@1EmpID$5.@7HireDatedate9.@17Salary5.;Bonus=.05*Salary;run;58 1-22Chapter1IntroductionCreateaSASDataSetfromRawDataPartialLogNOTE:9recordswerereadfromtheinfile'fltat1.dat'.Theminimumrecordlengthwas21.Themaximumrecordlengthwas21.NOTE:ThedatasetWORK.FLTAT1has9observationsand4variables.59ViewingaSASDataSetYoucanusetheCONTENTSproceduretodisplaythedescriptorportionofaSASdatasetPRINTproceduretodisplaythedataofaSASdataset.60GeneralformofaPROCCONTENTSstep:PROCCONTENTSDATA=SAS-data-set;RUN;GeneralformofaPROCPRINTstep:PROCPRINTDATA=SAS-data-set;RUN; 1.4ReviewofDisplayingSASDataSets1-23ViewingtheDescriptorPortionproccontentsdata=work.fltat1;run;PartialOutput---AlphabeticListofVariablesandAttributes---#VariableTypeLenPosƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ4BonusNum8161EmpIDChar5242HireDateNum803SalaryNum8861ViewingtheDataPortionprocprintdata=work.fltat1;run;PartialOutputHireObsEmpIDDateSalaryBonus1E123214532610653053.252E234113666916884584.403E345212352326391631.954E678111947283051415.255E832113479404402022.0062PROCPRINTproducesalistreport,whichisareportwithalineforeveryobservationinthedataset.Bydefault,allvariablesandallobservationsaredisplayed. 1-24Chapter1IntroductionTheNOOBSOptionTheNOOBSoptioninthePROCPRINTstatementsuppressestheobservationnumbersinthelistreport.GeneralformoftheNOOBSoption:PROCPRINTPROCPRINTDATA=DATA=SAS-data-setSAS-data-setNOOBSNOOBS;;RUN;RUN;63ViewingtheDataPortionprocprintdata=work.fltat1noobs;run;PartialOutputHireEmpIDDateSalaryBonusE123214532610653053.25E234113666916884584.40E345212352326391631.95E678111947283051415.2564ThevaluesofHireDatearedisplayedasthenumberofdayssinceJanuary1,1960. 1.4ReviewofDisplayingSASDataSets1-25TheFORMATStatementTheFORMATstatementappliesaSASformattospecifiedvariables.Aformatcontrolshowdatavaluesaredisplayed.GeneralformofaFORMATstatement:FORMATFORMATSAS-variable(s)format-name.…SAS-variable(s)format-name.…;;YoucanformatasmanyvariablesasyouneedusingoneFORMATstatement.65SASFormatsGeneralformofaSASformat:<<$$>FORMAT-NAMEw.>FORMAT-NAMEw.$indicatesacharacterformat.FORMAT-NAMEisthenameoftheformat.wspecifiesthetotalnumberofcharactersavailablefordisplayingthevalue..istherequireddelimiter.dspecifiesthenumberofdecimalplacestobedisplayedforanumericvalue.66CommonSASFormatsExamplesofformatsareCOMMAw.daddscommastonumericvalues.Example:46,543DOLLARw.daddscommasandadollarsigntonumericvalues.Example:$46,543MMDDYY10.writesdatesintheform12/31/2012.67 1-26Chapter1IntroductionApplyingaFormatprocprintdata=work.fltat1noobs;formatHireDatemmddyy10.SalaryBonusdollar7.;run;PartialOutputEmpIDHireDateSalaryBonusE123210/15/1999$61,065$3,053E234106/01/1997$91,688$4,584E345210/26/1993$32,639$1,632E678109/16/1992$28,305$1,415E832111/26/1996$40,440$2,02268Formatsassignedinaprocedurearetemporary;theyonlyremainforthatprocedure.AFORMATstatementinaDATAstepassignstheformatpermanently,andmakesitavailablewheneverthedatasetisused.Forexample,thefollowingcodeassignspermanentformatstothevariablesSalary,Bonus,andHireDate:datawork.fltat1;infile'raw-data-file';input@1EmpID$5.@7HireDatedate9.@17Salary5.;Bonus=Salary*.05;formatHireDatemmddyy10.SalaryBonusdollar7.;run;TheVARStatementTocontrolwhichvariablesaredisplayedandtheorderinwhichtheyaredisplayed,usetheVARstatement.GeneralformofaVARstatement:VARVARSAS-variableSAS-variable……;;69 1.4ReviewofDisplayingSASDataSets1-27TheVARStatementprocprintdata=work.fltat1noobs;formatSalaryBonusdollar7.;varEmpIDBonusSalary;run;PartialOutputEmpIDBonusSalaryE1232$3,053$61,065E2341$4,584$91,688E3452$1,632$32,639E6781$1,415$28,305E8321$2,022$40,440E1052$1,973$39,46170c01s4d1.sas 1-28Chapter1Introduction1.5WorkingwithExistingSASDataSetsObjectivesReviewtheconceptofSASdatalibraries.ReviewtheLIBNAMEstatement.ReviewcreatinganewSASdatasetfromanexistingdataset.Reviewconditionalprocessing.72SASFilesSASdatasetsandotherfilesarestoredinSASdatalibraries.SASUSERWORKPROG273 1.5WorkingwithExistingSASDataSets1-29SASDataLibrariesASASdatalibraryisacollectionofSASfilesthatarerecognizedasaunitbySASonyouroperatingenvironment.WORKWORK-temporarylibrarySASUSERSASUSER-permanentlibraryYoucancreateandaccessyourownPROG2permanentlibraries.PROG2-permanentlibrary74SASDataLibrariesThephysicalstructureofaSASdatalibrarydependsonyouroperatingsystem.Directory-basedoperatingsystems(WindowsorUNIX)anyfolderorsub-directoryz/OS(OS/390)systemsspeciallyformattedsequentialfile.75 1-30Chapter1IntroductionTheLIBNAMEStatementTheLIBNAMEstatementestablishesthelibraryreference(orlibref),whichisanaliasfortheSASdatalibrary.GeneralformoftheLIBNAMEstatement:LIBNAMELIBNAMElibreflibref'SAS-data-library'SAS-data-library'';;Thelibrefmustbeeightcharactersorfewer.76Exceptfortheeight-characterlengthlimit,thelibraryreferencefollowsthenamingconventionsforSASdatasetsandvariables.Specifically,itmust•beginwithaletterorunderscore•includenospecialcharactersotherthantheunderscore.TheLIBNAMEStatement:Examplesz/OS(OS/390)BatchandTSOlibnameprog2'edu.prog2.sasdata'disp=shr;Windows,DOS,andOS/2libnameprog2'c:prog2';UNIXlibnameprog2'/user/prog2';77 1.5WorkingwithExistingSASDataSets1-31Two-LevelSASDataSetNameslibref.SAS-filenamelibref.SAS-filenameSASDataSASLibrefDataLibrary78TheWORKLibraryTheWORKlibraryisthedefaultlibrary.IfyoudonotspecifyalibraryreferenceonaSASdatasetname,SASassumesthelibrefiswork.work.fltat1fltat179AccessingaPermanentSASDataSetTherearetwostepstoaccessingapermanentSASdataset:1.UseaLIBNAMEstatementtosetupalibrefthatpointstothelocationofthedataset.2.Referencethedatasetusingthelibrefasthefirstpartofthedatasetname.IfthelibrefisalreadyassignedintheSASsession,youdonotneedtoassignitagain.80 1-32Chapter1IntroductionViewingaPermanentSASDataSetWindowslibnameprog2'c:workshopwinsasprog2';procprintdata=prog2.testnoobs;run;81ExceptforthenameoftheSASdatalibrary,theSAScodedoesnotchangeacrossoperatingsystems.ViewingaPermanentSASDataSetLNameScoreSMITH0.90JONES0.57MOORE0.85LEE0.98LONG0.67GREEN0.70FOREMAN0.6982 1.5WorkingwithExistingSASDataSets1-33ViewingaPermanentSASDataSetUNIXlibnameprog2'/users/prog2';procprintdata=prog2.testnoobs;run;83ViewingaPermanentSASDataSetLNameScoreSMITH0.90JONES0.57MOORE0.85LEE0.98LONG0.67GREEN0.70FOREMAN0.6984 1-34Chapter1IntroductionViewingaPermanentSASDataSetz/OS(OS/390)libnameprog2'.prog2.sasdata';procprintdata=prog2.testnoobs;run;851Theperiodatthebeginningofthez/OSfilenameconcatenatestheuserIDtothefront.ViewingaPermanentSASDataSetLNameScoreSMITH0.90JONES0.57MOORE0.85LEE0.98LONG0.67GREEN0.70FOREMAN0.69861Anyreferencetoz/OSappliestoOS/390,unlessotherwisenoted. 1.5WorkingwithExistingSASDataSets1-35CreatingaPermanentSASDataSetTherearetwostepswhenyoucreateapermanentSASdataset:1.UseaLIBNAMEstatementtosetupalibrefthatpointstothelocationyouwanttosaveto.2.UsethelibrefasthefirstleveloftheSASdatasetname.IfthelibrefisalreadyassignedintheSASsession,youdonotneedtoassignitagain.87CreatingaPermanentSASDataSetWindowslibnameprog2'c:workshopwinsasprog2';dataprog2.fltat1;infile'fltat1.dat';input@1EmpID$5.@7HireDatedate9.@17Salary5.;Bonus=.05*Salary;run;88CreatingaPermanentSASDataSetUNIXlibnameprog2'/users/prog2';dataprog2.fltat1;infile'fltat1.dat';input@1EmpID$5.@7HireDatedate9.@17Salary5.;Bonus=.05*Salary;run;89 1-36Chapter1IntroductionCreatingaPermanentSASDataSetz/OS(OS/390)libnameprog2'.prog2.sasdata';dataprog2.fltat1;infile'.prog2.rawdata(fltat1)';input@1EmpID$5.@7HireDatedate9.@17Salary5.;Bonus=.05*Salary;run;90CreateaSASDataSetwithSASDataThescoresfromafinalexamLNameScorearestoredintheSASdatasetprog2.test.TheSMITH0.90professormustassigneachJONES0.57studentapassinggradeifMOORE0.85thescoreis0.7oraboveandLEE0.98afailinggradeotherwise.LONG0.67ThevariableScoreshouldGREEN0.70FOREMAN0.69notappearintheoutputdataset.91DesiredOutputThedatasetwork.fnlscoresshouldcontainonlythevariablesLNameandGrade.LNameGradeSMITHPassJONESFailedMOOREPassLEEPassLONGFailedGREENPassFOREMANFailed92 1.5WorkingwithExistingSASDataSets1-37TheSETStatementUseaSETstatementtoreadaSASdataset.GeneralformofaSETstatement:SETSETSAS-data-setSAS-data-set;;TheSETstatementpointstotheSASdataset(s)toberead.OptionsintheSETstatementaffecthowthedataisread.93IF-THENELSEStatementsOnemethodusedtoassignvaluesorexecutestatementsconditionallyisIF-THENELSEstatements.IFIFconditionconditionTHENTHENstatementstatement;;<;>……<;>94 1-38Chapter1IntroductionTheLENGTHStatementWhenyoucreatecharactervariableswithconditionallogicorfunctions,itisusuallyagoodideatoassignthelengthsexplicitlyusingaLENGTHstatement.GeneralformofaLENGTHstatement:LENGTHLENGTHvariable-namevariable-name<$><$>length-specification...length-specification...;;95SASsetsthetypeandlengththefirsttimethattheseattributesarereferencedintheprogram.AfterSASsetsthem,theattributescannotbechangedduringtheDATAstep.WhenyouuseaLENGTHstatement,becertainthatitisthefirststatementtoreferencethevariable.TheDROPStatementTodropvariablesthatarereadorcreatedduringtheDATAstep,useaDROPstatement.GeneralformofaDROPstatement:DROPDROPSAS-variable(s)SAS-variable(s);;VariablesdroppedwithaDROPstatementarereadintothePDVbutarenotoutputtothenewSASdataset.TheyareavailableforprocessingduringtheDATAstep.96AKEEPstatementisalsovalidforselectingvariablestooutputtoaSASdataset:KEEPSAS-variable(s); 1.5WorkingwithExistingSASDataSets1-39CreatingaVariablewithConditionalLogicdatafnlgrades;lengthGrade$6;dropScore;setprog2.test;ifScore>=.7thenGrade='Pass';elseGrade='Failed';run;97Compiledatafnlgrades;lengthGrade$6;LNameScoredropScore;setprog2.test;SMITH0.90ifScore>=.7thenGrade='Pass';JONES0.57elseGrade='Failed';MOORE0.85run;LEE0.98LONG0.67GREEN0.70FOREMAN0.69PDVGRADE$698......TheplacementoftheLENGTHstatementintheDATAstepdeterminesthepositionofthenewvariableinthePDVandthedefaultorderintheoutputdataset.BecausetheLENGTHstatementappearsbeforetheSETstatement,Gradeprecedesthevariablesobtainedfromtheprog2.testdataset.MovingtheLENGTHstatementaftertheSETstatementwouldaddGradetotheendofthePDV. 1-40Chapter1Introductiondatafnlgrades;lengthGrade$6;LNameScoredropScore;setprog2.test;SMITH0.90TrueifScore>=.7thenGrade='Pass';JONES0.57elseGrade='Failed';MOORE0.85run;LEE0.98LONG0.67GREEN0.70FOREMAN0.69PDVDGRADELNAMESCORE$$N688SMITH0.90103......datafnlgrades;lengthGrade$6;LNameScoredropScore;setprog2.test;SMITH0.90ifScore>=.7thenGrade='Pass';JONES0.57elseGrade='Failed';MOORE0.85run;LEE0.98LONG0.67ImplicitReturnImplicitGREEN0.70OutputFOREMAN0.69PDVDGRADELNAMESCORE$$N688PassSMITH0.90Writeoutobservationtofnlgrades.105......datafnlgrades;lengthGrade$6;LNameScoredropScore;setprog2.test;SMITH0.90ifScore>=.7thenGrade='Pass';JONES0.57elseGrade='Failed';MOORE0.85run;LEE0.98LONG0.67GREEN0.70ReinitializePDV.ValuesofFOREMAN0.69PDVvariablesnotreadfromSASaresettomissing.DGRADELNAMESCORE$$N688SMITH0.90108...... 1.5WorkingwithExistingSASDataSets1-41datafnlgrades;lengthGrade$6;LNameScoredropScore;setprog2.test;FalseSMITH0.90ifScore>=.7thenGrade='Pass';JONES0.57elseGrade='Failed';MOORE0.85run;LEE0.98LONG0.67GREEN0.70FOREMAN0.69PDVDGRADELNAMESCORE$$N688JONES0.57111......datafnlgrades;lengthGrade$6;LNameScoredropscore;setprog2.test;SMITH0.90ifscore>=.7thenGrade='Pass';JONES0.57elsegrade='Failed';MOORE0.85run;LEE0.98LONG0.67ImplicitReturnImplicitGREEN0.70OutputFOREMAN0.69PDVDGRADELNAMESCORE$$N688FailedJONES0.57Writeoutobservationtofnlgrades.113......datafnlgrades;lengthGrade$6;LNameScoredropscore;setprog2.test;SMITH0.90ifscore>=.7thenGrade='Pass';JONES0.57elsegrade='Failed';MOORE0.85run;LEE0.98LONG0.67ImplicitGREEN0.70OutputFOREMAN0.69PDVDGRADELNAMESCORE$$N688FailedJONES0.57Writeoutobservationtofnlgrades.114...... 1-42Chapter1Introductiondatafnlgrades;lengthGrade$6;LNameScoredropScore;setprog2.test;SMITH0.90ifScore>=.7thenGrade='Pass';JONES0.57elseGrade='Failed';MOORE0.85run;LEE0.98LONG0.67ContinueprocessingGREEN0.70untilend-of-filemarker.FOREMAN0.69PDVDGRADELNAMESCORE$$N688FailedJONES0.57116......SASdatasetscontainanend-of-filemarkerthatsignalstheendofthedatafile.WhenSASencounterstheend-of-filemarker,SASstopstheDATAstepandgoestothenextstep.CreatingaVariablewithConditionalLogicprocprintdata=fnlgradesnoobs;run;GradeLNamePassSMITHFailedJONESPassMOOREPassLEEFailedLONGPassGREENFailedFOREMAN117 1.5WorkingwithExistingSASDataSets1-43UsingtheVARStatementprocprintdata=fnlgradesnoobs;varLNameGrade;run;LNameGradeSMITHPassJONESFailedMOOREPassLEEPassLONGFailedGREENPassFOREMANFailed118c01s5d2.sas 1-44Chapter1Introduction1.6PrerequisiteSyntax(Self-Study)Thefollowingisasyntaxguidetostatementsandproceduresyoushouldknowbeforeyoustartthisclass.StatementsValidOnlyinaDATAStepTostarttheDATAstepandnamethedatasetbeingcreated:DATASAS-dataset;Tousearawdatafileasinput:INFILE'raw-data-file';andINPUTvariable-specifications;TouseaSASdatasetasinput:SETSAS-data-set;Tocreateanewvariable(assignmentstatement):variable-name=expression;Toperformconditionalprocessing:IFconditionTHENstatement;DATAStepCompile-Time-OnlyStatementsToexplicitlysetthelengthofavariable:LENGTHvariable-name<$>length-specification...;Todropavariableorvariablesonoutput:DROPSASvariable(s)tobedropped;orKEEPSASvariable(s)tobekept; 1.6PrerequisiteSyntax(Self-Study)1-45ProceduresTodisplaythedescriptorportionofaSASdataset:PROCCONTENTSDATA=SAS-data-set;RUN;TocreatealistreportofaSASdataset:PROCPRINTDATA=SAS-data-set;RUN;TocontrolwhichvariablesareshowninthePROCPRINTandtheirorder:VARSAS-variable(s);StatementsValidinaProcedureorDATAStepToapplyaformattoavariableorvariables:FORMATvariable-nameformat.…;Generalformofaformatname:<$>FORMAT-NAMEw.;where$indicatesacharacterformat.FORMAT-NAMEisthenameoftheformat.wspecifiesthetotalcharactersavailablefordisplayingthevalue..istherequireddelimiter.dspecifiesthenumberofdecimalplacestobedisplayedforanumericvalue.CommonNumericFormatsCOMMAw.daddscommastothevalue.DOLLARw.daddsdollarsignsandcommastothevalue.MMDDYY10.displaysSASdatesintheform12/31/2012.DATE9.displaysSASdatesintheform31DEC2012. 1-46Chapter1IntroductionGlobalStatementsToassignalibraryreferencetoaSASdatalibrary:LIBNAMElibref'operating-system-location';ToassignaheadertoSASoutput:TITLEn'header';Youcanspecifyuptotentitles.TITLEisequivalenttoTITLE1.Whenatitleisset,itstaysineffectuntilitischangedorcanceled,oruntiltheSASsessionends. 1.7NavigatingtheSASWindowingEnvironment(Self-Study)1-471.7NavigatingtheSASWindowingEnvironment(Self-Study)TheseinstructionsareintendedforstudentsnavigatingtheSASwindowingenvironmentonSASclassroommachines.Theymaynotbeappropriateforallsites.NavigatingtheSASWindowingEnvironmentonWindowsTheEnhancedEditor(thedefaulteditoronWindows)isonlyavailableontheWindowsoperatingsystem.UnliketheProgramEditor,itdoesnotautomaticallyclearwhencodeissubmitted,andyoucanhavemultipleEnhancedEditorwindowsopensimultaneously.YoucanusetheProgramEditor(thedefaulteditorinSASVersion6andearlier)byselectingViewÖProgramEditor.NavigatingtheWindowsTonavigatetoanywindow,dooneofthefollowing:•Selectthewindowbuttonatthebottomofthescreen(ifthewindowisopen).•SelectthewindownamefromtheViewdrop-downmenu.•TypethenameofthewindowinthecommandbarandpresstheEnterkey.Tocloseanywindow,dooneofthefollowing:•Selectintheupper-rightcornerofthewindow.•Typeendinthecommandbar,andpresstheEnterkey. 1-48Chapter1IntroductionOpeningaSASProgramToopenaSASprogram,theProgramEditorortheEnhancedEditormustbetheactivewindow.1.SelectFileÖOpenorselect.AWindowsdialogboxappears.2.Navigatethroughthefoldersandhighlighttheprogram.3.SelectOK.SubmittingaSASProgramTosubmitaprogram,theProgramEditorortheEnhancedEditormustbetheactivewindow,andthecodetobesubmittedmustbeinthewindow.1.Highlightthecodeyouwanttosubmit.(Thisisnotnecessaryifyousubmittheentirecontentsofthewindow.)2.IssuetheSUBMITcommandbyselecting,pressingtheF3key,orselectingRunÖSubmit.RecallingSubmittedCodeTheProgramEditorisclearedautomaticallyeverytimecodeissubmittedfromit.Torecallsubmittedcode,maketheProgramEditortheactivewindow,anddooneofthefollowing:•SelectRunÖRecall.•Typerecallinthecommandbar,andpresstheEnterkey.•UsetheF4shortcutkey.TheRECALLcommandcanalsobeusedfromtheEnhancedEditortoretrievelostcodethatwassubmitted.SavingaSASProgramTosaveaSASprogram,theProgramEditorortheEnhancedEditormustbetheactivewindow,andthecodeyouwanttosavemustbeinthewindow.1.SelectFileÖSaveAs....AWindowsdialogboxappears.2.Navigatetothefolderinwhichyouwanttosavetheprogram.3.Typeanamefortheprogramintheappropriatebox.4.SelectOK.ClearingWindowsToclearawindow,dooneofthefollowing:•Activatethewindow,typeclearinthecommandbar,andpresstheEnterkey.•ActivatethewindowandselectEditÖClearAll.•TypeclearandthenameofthewindowinthecommandbarandpresstheEnterkey. 1.7NavigatingtheSASWindowingEnvironment(Self-Study)1-49IssuingMultipleCommandsatOnceToissuemorethanonecommandatthesametime,typethecommandsinthecommandbarseparatedbysemicolons.Forexample,toclearboththeLogandOutputwindows,typethefollowinginthecommandbar:clearlog;clearoutputNavigatingtheSASWindowingEnvironmentonUNIXIntheUNIXenvironment,SASwindowsarefloating,notdocked.Thereisafloatingtoolbarwithacommandbarandshortcuticons.Drop-downmenusareatthetopofeachwindow.NavigatingtheWindowsToactivateanywindow,dooneofthefollowing:•Selectthewindowiconatthebottomofthescreen.•SelectthewindownamefromtheViewdrop-downmenu.•Typethewindownameinthecommandbar,andpresstheEnterkey. 1-50Chapter1IntroductionSubmittingaProgramTosubmitaSASprogram,theProgramEditormustbetheactivewindowandcontainthecodeyouwanttosubmit.DoanyofthefollowingtosubmitthecontentsoftheProgramEditor:•Typesubmitinthecommandbar,andpresstheEnterkey.•UsetheF3shortcutkey.•Selectfromthetoolbar.•SelectRunÖSubmit.RecallingSubmittedCodeTheProgramEditorisclearedautomaticallyeverytimecodeissubmittedfromit.Torecallsubmittedcode,maketheProgramEditortheactivewindow,anddooneofthefollowing:•SelectRunÖRecall.•Typerecallinthecommandbar,andpresstheEnterkey.•UsetheF4shortcutkey.SavingaSASProgramTosaveaSASprogram,theProgramEditormustbetheactivewindow,andthecodeyouwanttosavemustbeinthewindow.1.SelectFileÖSaveAs....Adialogboxappears.2.Navigatetothedirectoryinwhichyouwanttosavetheprogram.3.Typeanamefortheprogramintheappropriatebox.4.SelectOK.ClearingWindowsToclearawindow,dooneofthefollowing:•Activatethewindow,typeclearinthecommandbar,andpresstheEnterkey.•ActivatethewindowandselectEditÖClearAll.•Typeclearandthenameofthewindowatthecommandbar,andpresstheEnterkey.IssuingMultipleCommandsatOnceTosubmitmorethanonecommandatthesametime,typethecommands,separatedbysemicolons,inthecommandbarandpresstheEnterkey.Forexample,toclearboththeLogandOutputwindows,typethefollowinginthecommandbar:clearlog;clearoutput 1.7NavigatingtheSASWindowingEnvironment(Self-Study)1-51NavigatingtheSASWindowingEnvironmentonz/OSEachtimeyoulogon,1.opentheOutputwindowbytypingoutputonanycommandlineandpressingtheEnterkey.2.issuethefollowingcommandfromthecommandlineoftheOutputwindow.(Thispreventssuspendedoutput.)autoscroll0NavigatingtheWindows•Eachwindowcontainsacommandline.•YoucanopenanywindowbytypingitsnameonanycommandlineandpressingtheEnterkey.•ThePageUpandPageDownkeysonyourkeyboardmovefromoneopenwindowtoanother.•F7andF8enableyoutoscrollupanddownwithinawindow.•TocloseanywindowandreturntotheProgramEditor,issuetheENDcommandorusetheF3key.IftheProgramEditorisactive,theF3keysubmitsthecodeinthewindow.•Tomaximizeawindow,typeZonthecommandlineandpresstheEnterkey.Torestorethewindowtonormalsize,typeZonthecommandlineofthemaximizedwindowandpresstheEnterkey.IncludingaSASProgramToincludeaSASprograminyoursession,theProgramEditormustbetheactivewindow.1.Typeinclude'name-of-SAS-program'onthecommandlineoftheProgramEditorwindow.2.PresstheEnterkey.SubmittingaProgramTosubmitaSASprogram,theProgramEditormustbetheactivewindowandcontainthecodeyouwanttosubmit.Tosubmitcode,dooneofthefollowing:•TypesubmitinthecommandlineoftheProgramEditor,andpresstheEnterkey.•UsetheF3shortcutkey.RecallingSubmittedCodeTheProgramEditorisclearedautomaticallyeverytimecodeissubmittedfromit.Torecallsubmittedcode,maketheProgramEditortheactivewindowanddooneofthefollowing:•TyperecallinthecommandlineoftheProgramEditor,andpresstheEnterkey.•UsetheF4shortcutkey. 1-52Chapter1IntroductionSavingaSASProgramTosaveaSASprogram,theProgramEditormustbetheactivewindowandcontainthecodeyouwanttosave.1.Typefile'name-of-SAS-program'onthecommandlineoftheProgramEditorwindow.2.PresstheEnterkey.Anoteappearsatthetopofthewindow.ClearingWindowsToclearawindow,dooneofthefollowing:•TypeclearonthecommandlineofthatwindowandpresstheEnterkey.•TypeclearandthenameofthewindowtobeclearedonanycommandlineandpresstheEnterkey.EditingSASProgramCodeintheUNIXandz/OSEnvironmentsProgramEditorLineNumberCommandsMostWindowsusersutilizecopyandpastecommands.However,theProgramEditorinallthreeenvironmentsallowstheuseoflinenumbercommands.Usethesecommandstocopy,paste,ordeleteprogramcode.Iinsertsoneline(after)thecurrentline.Ininsertsnlines(after)thecurrentline.IBinsertsoneline(before)thecurrentline.IBninsertsnlines(before)thecurrentline.Ddeletesthecurrentline.Dndeletesnlines.DDdeletesablockoflines.Typeddonthefirstandlastlinesoftheblock.Rrepeatsthecurrentlineonce.Rnrepeatsthecurrentlinentimes.RRrepeatsablockoflinesonce.Typerronthefirstandlastlinesoftheblock. 1.7NavigatingtheSASWindowingEnvironment(Self-Study)1-53MovingandCopyingCodeTocopyormoveonelineofcode,dothefollowing:1.Typec(tocopy)orm(tomove)thelineyouwanttocopyormove.2.Typea(forafter)orb(forbefore)ontheappropriatelinetoindicatewhereyouwanttocopyormovethespecifiedline.Tocopyormoveablockoflinesofcode,dothefollowing:1.Typeccormmonthefirstlineyouwanttocopyormove.2.Typeccormmonthelastlineyouwanttocopyormove.3.Typea(forafter)orb(forbefore)ontheappropriatelinetoindicatewhereyouwanttocopyormovetheblockoflines.LinenumbercommandsarenotavailableintheWindowsEnhancedEditor. 1-54Chapter1Introduction Chapter2ControllingInputandOutput2.1OutputtingMultipleObservations.................................................................................2-32.2WritingtoMultipleSASDataSets...............................................................................2-152.3SelectingVariablesandObservations.......................................................................2-232.4WritingtoanExternalFile...........................................................................................2-392.5SolutionstoExercises.................................................................................................2-58 2-2Chapter2ControllingInputandOutput 2.1OutputtingMultipleObservations2-32.1OutputtingMultipleObservationsObjectivesExplicitlycontroltheoutputofmultipleobservationstoaSASdataset.3AForecastingApplicationThegrowthrateofeachdivisionofanairlineisforecastinprog2.growth.Ifeachofthefivedivisionsgrowsatitsrespectiverateforthenextthreeyears,whatwillbetheapproximatesizeofeachdivisionattheendofeachofthethreeyears?PartialListingofprog2.growthNumDivisionEmpsIncreaseAPTOPS2050.075FINACE1980.040FLTOPS1870.0804 2-4Chapter2ControllingInputandOutputAForecastingApplicationTheoutputSASdataset,forecast,shouldcontain15observations.PartialListingofforecastNewDivisionIncreaseYearTotalAPTOPS0.0751220.38APTOPS0.0752236.90APTOPS0.0753254.67FINACE0.0401205.92FINACE0.0402214.165YoucanuseaSASformattodisplaythevaluesofNewTotalaswholenumbers.ImplicitOutput(Review)Bydefault,everyDATAstepcontainsanimplicitOUTPUTstatementattheendofeachiteration.ThisimplicitOUTPUTstatementtellstheSASSystemtowriteobservationstothedatasetordatasetsthatarecreated.dataforecast;setprog2.growth;2.Implicit;run;1.Implicitoutput6......Aniterationisoneexecutionofasequenceofcomputeroperationsorinstructionsthatareperformedaspecifiednumberoftimesoruntilaconditionismet. 2.1OutputtingMultipleObservations2-5ImplicitOutput(Review)dataforecast;setprog2.growth;2.Implicit;run;1.ImplicitoutputWhenoneobservationisreadfromprog2.growth,oneobservationiswrittentoforecast.7TheOUTPUTStatementTheexplicitOUTPUTstatementwritesthecurrentcontentsofthePDVtoaSASdataset.PlacinganexplicitOUTPUTstatementinaDATAstepoverridestheimplicitoutput,andSASaddsanobservationtoadatasetonlywhenanexplicitOUTPUTstatementisexecuted.OUTPUTOUTPUT;>;8UsinganexplicitOUTPUTstatementwithoutargumentscausesthecurrentobservationtobewrittentoalldatasetsthatarenamedintheDATAstatement.YoucanusetheexplicitOUTPUTstatementto•createtwoormoreSASobservationsfromeachlineofinputdata•writeobservationstomultipleSASdatasetsinoneDATAstep•writeobservationstoaSASdatasetwithoutanyinputdata.ImplicitreturntothebeginningoftheDATAstepoccursafterthebottomofthestepisreached;notwhenanexplicitOUTPUTstatementisexecuted. 2-6Chapter2ControllingInputandOutputAForecastingApplicationdataforecast;dropNumEmps;setprog2.growth;Year=1;NewTotal=NumEmps*(1+Increase);output;Year=2;NewTotal=NewTotal*(1+Increase);output;Year=3;NewTotal=NewTotal*(1+Increase);output;run;c02s1d1.sas9Inyearstwoandthree,theexistingvalueofNewTotalisusedtocalculatethenewvalueofNewTotal.PartialListingofprog2.growthNumCompiledataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;NewTotal=NewTotal*(1+Increase);output;Year=3;NewTotal=NewTotal*(1+Increase);output;run;PDV10...... 2.1OutputtingMultipleObservations2-7PartialListingofprog2.growthNumdataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;NewTotal=NewTotal*(1+Increase);output;Year=3;NewTotal=NewTotal*(1+Increase);output;run;PDVDIVISIONNUMEMPSINCREASEYEARNEWTOTAL$NNNN6888813......PartialListingofprog2.growthNumdataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;NewTotal=NewTotal*(1+Increase);output;Year=3;NewTotal=NewTotal*(1+Increase);output;run;PDVDIVISIONNUMEMPSINCREASEYEARNEWTOTAL$DNNNN6888814...... 2-8Chapter2ControllingInputandOutputPartialListingofprog2.growthNumExecutedataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;PartialListingofforecastNewTotal=NewTotal*(1+Increase);Newoutput;DivisionYearTotalYear=3;NewTotal=NewTotal*(1+Increase);output;run;PDVDIVISIONNUMEMPSINCREASEYEARNEWTOTAL$DNNNN6888815...Theforecastdatasetcontainsfourvariables:Division,Increase,Year,andNewTotal.TheIncreasevariableisnotdisplayedintherepresentationsofforecast.PartialListingofprog2.growthNumdataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;PartialListingofforecastNewTotal=NewTotal*(1+Increase);Newoutput;DivisionYearTotalYear=3;NewTotal=NewTotal*(1+Increase);output;run;PDVDIVISIONNUMEMPSINCREASEYEARNEWTOTAL$DNNNN68888APTOPS2050.075..17...... 2.1OutputtingMultipleObservations2-9PartialListingofprog2.growthNumdataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;PartialListingofforecastNewTotal=NewTotal*(1+Increase);Newoutput;DivisionYearTotalYear=3;NewTotal=NewTotal*(1+Increase);output;run;PDV205*(1+0.075)DIVISIONNUMEMPSINCREASEYEARNEWTOTAL$DNNNN68888APTOPS2050.0751220.3820......PartialListingofprog2.growthNumdataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;PartialListingofforecastNewTotal=NewTotal*(1+Increase);Newoutput;DivisionYearTotalYear=3;NewTotal=NewTotal*(1+Increase);APTOPS1220.38output;run;PDVDIVISIONNUMEMPSINCREASEYEARNEWTOTAL$DNNNN68888APTOPS2050.0751220.38Writeoutfirstobservationtoforecast.21...PartialListingofprog2.growthNumdataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;PartialListingofforecastNewTotal=NewTotal*(1+Increase);Newoutput;DivisionYearTotalYear=3;NewTotal=NewTotal*(1+Increase);APTOPS1220.38output;run;PDVDIVISIONNUMEMPSINCREASEYEARNEWTOTAL$DNNNN68888APTOPS2050.0752220.3822... 2-10Chapter2ControllingInputandOutputPartialListingofprog2.growthNumdataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;PartialListingofforecastNewTotal=NewTotal*(1+Increase);Newoutput;DivisionYearTotalYear=3;NewTotal=NewTotal*(1+Increase);APTOPS1220.38output;APTOPS2236.90run;PDVDIVISIONNUMEMPSINCREASEYEARNEWTOTAL$DNNNN68888APTOPS2050.0752236.90Writeoutsecondobservationtoforecast.24...PartialListingofprog2.growthNumdataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;PartialListingofforecastNewTotal=NewTotal*(1+Increase);Newoutput;DivisionYearTotalYear=3;NewTotal=NewTotal*(1+Increase);APTOPS1220.38output;APTOPS2236.90run;PDVDIVISIONNUMEMPSINCREASEYEARNEWTOTAL$DNNNN68888APTOPS2050.0753236.9025...PartialListingofprog2.growthNumdataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;PartialListingofforecastNewTotal=NewTotal*(1+Increase);Newoutput;DivisionYearTotalYear=3;NewTotal=NewTotal*(1+Increase);APTOPS1220.38output;APTOPS2236.90run;APTOPS3254.67PDVDIVISIONNUMEMPSINCREASEYEARNEWTOTAL$DNNNN68888APTOPS2050.0753254.67Writeoutthirdobservationtoforecast.27...... 2.1OutputtingMultipleObservations2-11PartialListingofprog2.growthNumdataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;PartialListingofImplicitreturnforecastNewTotal=NewTotal*(1+Increase);Newoutput;DivisionYearTotalYear=3;NewTotal=NewTotal*(1+Increase);APTOPS1220.38output;APTOPS2236.90run;APTOPS3254.67NoimplicitPDVoutputDIVISIONNUMEMPSINCREASEYEARNEWTOTAL$DNNNN68888APTOPS2050.0753254.6729...PartialListingofprog2.growthNumdataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;PartialListingofforecastNewTotal=NewTotal*(1+Increase);Newoutput;DivisionYearTotalYear=3;NewTotal=NewTotal*(1+Increase);APTOPS1220.38output;APTOPS2236.90run;APTOPS3254.67PDVDIVISIONNUMEMPSINCREASEYEARNEWTOTAL$DNNNN68888FINACE1980.040..31......PartialListingofprog2.growthNumdataforecast;DivisionEmpsIncreasedropNumEmps;setprog2.growth;APTOPS2050.075Year=1;FINACE1980.040NewTotal=NumEmps*(1+Increase);FLTOPS1870.080output;Year=2;PartialListingofforecastNewTotal=NewTotal*(1+Increase);Newoutput;DivisionYearTotalYear=3;ContinueexecutingDATANewTotal=NewTotal*(1+Increase);APTOPS1220.38output;stepuntilallobservationsAPTOPS2236.90run;APTOPS3254.67fromprog2.growthPDVareread.DIVISIONNUMEMPSINCREASEYEARNEWTOTAL$NNNND68888FINACE1980.0401.34...... 2-12Chapter2ControllingInputandOutputAForecastingApplicationPartialLogNOTE:Therewere5observationsreadfromthedatasetPROG2.GROWTH.NOTE:ThedatasetWORK.FORECASThas15observationsand4variables.35AForecastingApplicationprocprintdata=forecastnoobs;formatNewTotal6.;run;PartialPROCPRINTOutputNewDivisionIncreaseYearTotalAPTOPS0.0751220APTOPS0.0752237APTOPS0.0753255FINACE0.0401206FINACE0.040221436Applyingthe6.formattoNewTotaldoesnotchangethevaluesstoredintheforecastdataset.ASASfunctioncanbeusedtochangethestoredvalue.SASfunctionsarediscussedinChapter5,“DataTransformations.” 2.1OutputtingMultipleObservations2-13ExercisesTheseexercisesuseSASdatasetsstoredinapermanentSASdatalibrary.FillintheblankwiththelocationofyourSASdatalibrary.SubmittheLIBNAMEstatementtoassignthelibrefPROG2totheSASdatalibrary.libnameprog2'______________________________________';1.OutputtingMultipleObservationsRotating,ortransposing,aSASdatasetcanbeaccomplishedbyusingexplicitOUTPUTstatementsinaDATAstep.Whenadatasetisrotated,thevaluesofanobservationintheinputdatasetbecomevaluesofavariableintheoutputdataset.UseexplicitOUTPUTstatementstorotateprog2.donateintoadatasetcalledrotate.Createfouroutputobservationsinrotatefromeachinputobservationinprog2.donate.Therotatedatasetshouldhavethreevariables:ID,Qtr,andAmount.Printthedatasettoverifyyourresults.PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4E00224123322.E0036735484030E00441.638990E0058716193029E005984861PartialListingofrotateObsIDQtrAmount1E002241122E002242333E002243224E002244.5E003671356E003672487E003673408E003674309E004411.10E0044126311E0044138912E00441490 2-14Chapter2ControllingInputandOutput2.UsingConditionalLogictoOutputMultipleObservations(Optional)Theprog2.ffmethoddatasetcontainsinformationaboutthedifferentwaysthatfrequentflyerspurchasedairlinetickets.AvalueofYintheInternet,Telephone,orTravelAgencyvariablesindicatesthatthefrequentflyerusedthatmethod.prog2.ffmethodTravelIDInternetTelephoneAgencyF31351NYYF161YYNF212NNYF25122YNNUseexplicitOUTPUTstatementstocreateadatasetcalledbuyhistory.Thisdatasetwillcontainoneobservationforeachmethodusedbyeachfrequentflyer.TherewillbetwoobservationsinbuyhistorythatrefertoF31351,butonlyoneobservationthatreferstoF212.Thebuyhistorydatasetshouldhavetwovariables:IDandMethod.Printthedatasettoverifyyourresults.buyhistoryObsIDMethod1F31351Telephone2F31351TravelAgency3F161Internet4F161Telephone5F212TravelAgency6F25122InternetADOstatementcanbeusedwithinIF-THEN/ELSEstatementstodesignateagroupofstatementstobeexecuted,dependingonwhethertheIFconditionistrueorfalse. 2.2WritingtoMultipleSASDataSets2-152.2WritingtoMultipleSASDataSetsObjectivesCreatemultipleSASdatasetsinasingleDATAstep.Useconditionalprocessingtocontrolthedataset(s)towhichanobservationiswritten.39WritingtoMultipleSASDataSetsThedatasetprog2.militarycontainsinformationaboutairfacilitiesmaintainedbytheArmy,Navy,AirForce,andMarines.CreatefourSASdatasets,army,navy,airforce,andmarines.Eachofthefourdatasetsshouldcontaininformationaboutasinglebranchofthearmedforces.40 2-16Chapter2ControllingInputandOutputWritingtoMultipleSASDataSetsprocprintdata=prog2.militarynoobs;PROG2.MILITARY(Partialoutput.)varCodeType;run;PartialPROCPRINTOutputCodeTypeSKFAirForceDPGArmyHIFAirForceNFENavalDAAArmy41TheDATAStatement(Review)TheDATAstatementbeginsaDATAstepandprovidesnamesforanyoutputSASdatasets.YoucancreatemultipleSASdatasetsinasingleDATAstepbylistingthenamesoftheoutputdatasetsseparatedbyatleastonespace.DATADATA<<…><…data-set-name-ndata-set-name-n>;>;42IfyoudonotspecifyaSASdatasetnameorthereservedname_NULL_inaDATAstatement,thenbydefault,SASautomaticallycreatesdatasetswiththenamesdata1,data2,andsoonintheworklibrary. 2.2WritingtoMultipleSASDataSets2-17TheOUTPUTStatement(Review)Bydefault,theexplicitOUTPUTstatementwritesthecurrentobservationtoeverySASdatasetlistedintheDATAstatement.Youcanspecifythename(s)ofadatasetordatasetstowhichSASwritestheobservation.OUTPUTOUTPUT;>;43SAS-data-set-1throughSAS-data-set-nmustalsoappearintheDATAstatement.TospecifymultipledatasetsinasingleOUTPUTstatement,separatethedatasetnameswithaspace:outputdata1data2; 2-18Chapter2ControllingInputandOutputWritingtoMultipleSASDataSetsdataarmynavyairforcemarines;dropType;setprog2.military;ifTypeeq'Army'thenoutputarmy;elseifTypeeq'Naval'thenoutputnavy;elseifTypeeq'AirForce'thenoutputairforce;elseifTypeeq'Marine'thenoutputmarines;run;c02s2d1.sas44AnalternateformofconditionallyexecutingstatementsusesSELECTgroups.SELECT<(select-expression)>;WHEN-1(when-expression-1<…,when-expression-n>)statement;<…WHEN-n(when-expression-1<…,when-expression-n>)statement;>END;TheDATAstepshownabovecouldberewrittentouseSELECTgroupsasfollows:dataarmynavyairforcemarines;dropType;setprog2.military;select(Type);when('Army')outputarmy;when('Naval')outputnavy;when('AirForce')outputairforce;when('Marine')outputmarines;otherwise;end;run;SeeSASdocumentationformoreinformationaboutusingSELECTgroups. 2.2WritingtoMultipleSASDataSets2-19WritingtoMultipleSASDataSetsPartialLogNOTE:Therewere137observationsreadfromthedatasetPROG2.MILITARY.NOTE:ThedatasetWORK.ARMYhas41observationsand5variables.NOTE:ThedatasetWORK.NAVYhas28observationsand5variables.NOTE:ThedatasetWORK.AIRFORCEhas64observationsand5variables.NOTE:ThedatasetWORK.MARINEShas4observationsand5variables.45 2-20Chapter2ControllingInputandOutputExercises3.WritingtoMultipleSASDataSetsThedatasetprog2.elementscontainsinformationabouttheknownelementsintheperiodictable.Eachobservationcontainsanelement'sname,symbol,atomicnumber,andstate.ThevalueofStatereferstowhethertheelementisagas,liquid,solid,orsyntheticatroomtemperature.Asyntheticelementisanelementthatisnotpresentinnature.CreatefourSASdatasets:gas,liquid,solid,andsynthetic.Eachdatasetwillcontaininformationaboutthoseelementsthathavethatstateatroomtemperature.Eachofthesefourdatasetsshouldcontainthreevariables;theyshouldnotcontaintheStatevariable.Charactervaluesarecase-sensitive.Thegasdatasetshouldcontain11observations.Theliquiddatasetshouldcontainthreeobservations.Thesoliddatasetshouldcontain78observations.Thesyntheticdatasetshouldcontain21observations.PartialListingofprog2.elementsAtomicNameSymbolNumStateActiniumAc89SolidAluminumAl13SolidAmericiumAm95SyntheticAntimonySb51SolidArgonAr18GasArsenicAs33SolidAstatineAt85SolidBariumBa56SolidBerkeliumBk97SyntheticBerylliumBe4SolidBismuthBi83SolidBohriumBh107SolidBoronB5SolidBromineBr35Liquid 2.2WritingtoMultipleSASDataSets2-21ListingofliquidAtomicObsNameSymbolNum1BromineBr352FranciumFr873MercuryHg80ThenamesofelementsandtheirsymbolsareapprovedbyIUPAC,theInternationalUnionofPureandAppliedChemistry.IUPAChasnotapprovednamesforelementswithatomicnumbersabove109;therefore,temporaryIUPACnamesareused.In1999,ateamofscientistsannouncedtheobservationofwhatappearedtobeelements116(ununhexium)and118(ununoctium).In2001,theteamretracteditsoriginalpaperafterseveralconfirmationexperimentsfailedtoreproducethedesiredresults.In2004,ateamofscientistsfromtheLawrenceLivermoreNationalLaboratoryandtheJointInstituteofNuclearResearchinRussiaannouncedthediscoveryofthesuperheavyelements113(ununtrium,uut)and115(ununpentium,uup).Element117(ununseptium,uus)isnotyetdiscovered.4.WritingtoMultipleSASDataSets(Optional)Alanthanideisanymemberoftheseriesofelementsofincreasingatomicnumbersbeginningwithlanthanum(57)andendingwithytterbium(70).Anactinideisanymemberoftheseriesofelementsthatbeginswithactinium(89)andendswithlawrencium(102).CreatetwoSASdatasets,lanthanidesandactinides.Eachdatasetwillcontaininformationaboutthoseelementsineachrespectiveseries.Besurethateachdatasetcontainsfourvariables. 2-22Chapter2ControllingInputandOutputPartialListingoflanthanidesAtomicObsNameSymbolNumState1CeriumCe58Solid2DysprosiumDy66Solid3ErbiumEr68Solid4EuropiumEu63Solid5GadoliniumGd64SolidPartialListingofactinidesAtomicObsNameSymbolNumState1ActiniumAc89Solid2AmericiumAm95Synthetic3BerkeliumBk97Synthetic4CaliforniumCf98Synthetic5CuriumCm96SyntheticThelanthanidesandactinidesarealsoknownastherareearthelements. 2.3SelectingVariablesandObservations2-232.3SelectingVariablesandObservationsObjectivesControlwhichvariablesarewrittentoanoutputdatasetduringaDATAstep.ControlwhichvariablesarereadfromaninputdatasetduringaDATAstep.ControlhowmanyobservationsareprocessedfromaninputdatasetduringaDATAorPROCstep.48ControllingVariableOutputBydefault,theSASSystemwritesallvariablesfromeveryinputdatasettoeveryoutputdataset.IntheDATAstep,theDROPandKEEPstatementscanbeusedtocontrolwhichvariablesarewrittentooutputdatasets.49 2-24Chapter2ControllingInputandOutputTheDROPandKEEPStatements(Review)InputSASdatasetRawdatafilePDVDROPandKEEPstatementsOutputSASdataset50......CreatingMultipleSASDataSets(Review)proccontentsdata=prog2.military;run;PartialPROCCONTENTSOutput---AlphabeticListofVariablesandAttributes---#VariableTypeLenPosƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ6AirportChar40373CityChar20122CodeChar395CountryChar3344StateChar2321TypeChar9051CreatingMultipleSASDataSets(Review)dataarmynavyairforcemarines;dropType;setprog2.military;ifTypeeq'Army'thenoutputarmy;elseifTypeeq'Naval'thenoutputnavy;elseifTypeeq'AirForce'thenoutputairforce;elseifTypeeq'Marine'thenoutputmarines;run;c02s3d1.sas52 2.3SelectingVariablesandObservations2-25CreatingMultipleSASDataSets(Review)PartialLogNOTE:Therewere137observationsreadfromthedatasetPROG2.MILITARY.NOTE:ThedatasetWORK.ARMYhas41observationsand5variables.NOTE:ThedatasetWORK.NAVYhas28observationsand5variables.NOTE:ThedatasetWORK.AIRFORCEhas64observationsand5variables.NOTE:ThedatasetWORK.MARINEShas4observationsand5variables.53ControllingVariableOutputTheDROPandKEEPstatementsapplytoalloutputdatasets.However,whenyoucreatemultipleoutputdatasets,youcanusetheDROP=andKEEP=datasetoptionstowritedifferentvariablestodifferentdatasets.54 2-26Chapter2ControllingInputandOutputTheDROP=DataSetOptionTheDROP=datasetoptionexcludesvariablesfromprocessingorfromoutputSASdatasets.WhentheDROP=datasetoptionisassociatedwithanoutputdataset,SASdoesnotwritethespecifiedvariablestotheoutputdataset.However,allvariablesareavailableforprocessing.SAS-data-setSAS-data-set(DROP=(DROP=variable-1variable-2variable-1variable-2……variable-nvariable-n))55variable-1throughvariable-nlistsoneormorevariablenamesseparatedbyaspace.IftheDROP=datasetoptionisassociatedwithaninputdataset,thespecifiedvariablesarenotavailableforprocessing.TheKEEP=DataSetOptionTheKEEP=datasetoptionspecifiesvariablesforprocessingorforwritingtooutputSASdatasets.WhentheKEEP=datasetoptionisassociatedwithanoutputdataset,onlythespecifiedvariablesarewrittentotheoutputdataset.However,allvariablesareavailableforprocessing.SAS-data-setSAS-data-set(KEEP=(KEEP=variable-1variable-2variable-1variable-2……variable-nvariable-n))56variable-1throughvariable-nlistsoneormorevariablenamesseparatedbyaspace.IftheKEEP=datasetoptionisassociatedwithaninputdataset,onlythespecifiedvariablesareavailableforprocessing. 2.3SelectingVariablesandObservations2-27TheDROP=andKEEP=DataSetOptionsInputSASdatasetRawdatafilePDVDROP=andKEEP=DROPandKEEPdatasetoptionsstatementsassociatedwithanoutputdatasetOutputSASdataset57......ControllingVariableOutputdataarmy(drop=CityStateCountryType)navy(drop=Type)airforce(drop=CodeType)marines;setprog2.military;ifTypeeq'Army'thenoutputarmy;elseifTypeeq'Naval'thenoutputnavy;elseifTypeeq'AirForce'thenoutputairforce;elseifTypeeq'Marine'thenoutputmarines;run;c02s3d2.sas58YoucannotspecifytheDROP=datasetoptionintheOUTPUTstatement. 2-28Chapter2ControllingInputandOutputControllingVariableOutputPartialLogNOTE:Therewere137observationsreadfromthedatasetPROG2.MILITARY.NOTE:ThedatasetWORK.ARMYhas41observationsand2variables.NOTE:ThedatasetWORK.NAVYhas28observationsand5variables.NOTE:ThedatasetWORK.AIRFORCEhas64observationsand4variables.NOTE:ThedatasetWORK.MARINEShas4observationsand6variables.59ControllingVariableOutputdataarmy(keep=CodeAirport)navy(keep=CodeAirportCityStateCountry)airforce(keep=AirportCityStateCountry)marines;setprog2.military;ifTypeeq'Army'thenoutputarmy;elseifTypeeq'Naval'thenoutputnavy;elseifTypeeq'AirForce'thenoutputairforce;elseifTypeeq'Marine'thenoutputmarines;run;c02s3d3.sas60YoucannotspecifytheKEEP=datasetoptionintheOUTPUTstatement. 2.3SelectingVariablesandObservations2-29ControllingVariableOutputPartialLogNOTE:Therewere137observationsreadfromthedatasetPROG2.MILITARY.NOTE:ThedatasetWORK.ARMYhas41observationsand2variables.NOTE:ThedatasetWORK.NAVYhas28observationsand5variables.NOTE:ThedatasetWORK.AIRFORCEhas64observationsand4variables.NOTE:ThedatasetWORK.MARINEShas4observationsand6variables.61Inmanycases,youhaveachoicebetweenusingaDROP=datasetoption(orDROPstatement)oraKEEP=datasetoption(orKEEPstatement).Typically,choosethedatasetoptionorstatementthatminimizestheamountoftyping.Forexample,acombinationofDROP=andKEEP=datasetoptionscanreducetheamountoftypingnecessaryinthefollowingexample:dataarmy(keep=CodeAirport)navy(drop=Type)airforce(drop=CodeType)marines;setprog2.military;ifTypeeq'Army'thenoutputarmy;elseifTypeeq'Naval'thenoutputnavy;elseifTypeeq'AirForce'thenoutputairforce;elseifTypeeq'Marine'thenoutputmarines;run; 2-30Chapter2ControllingInputandOutputControllingVariableInputIntheDATAstep,theDROPandKEEPstatementsapplyonlytooutputSASdatasets.However,theDROP=andKEEP=datasetoptionscanapplytobothinputandoutputSASdatasets.62InPROCsteps,youcanusetheDROP=orKEEP=datasetoptions,butnottheDROPorKEEPstatements.ControllingVariableInputInputSASdatasetRawDataFileDROP=andKEEP=datasetoptionsin“read”statementPDVDROP=andKEEP=DROPandKEEPdatasetoptionsstatementsassociatedwithanoutputdatasetOutputSASdataset63......TheINPUTstatementcontrolswhichfieldsfromarawdatafilearereadintothePDV. 2.3SelectingVariablesandObservations2-31ControllingVariableInputSASappliesdatasetoptionstoinputdatasetsbeforeitevaluatesprogrammingstatementsappliesdatasetoptionstooutputdatasets.dataarmy(keep=CodeAirport);setprog2.military(drop=CityStateCountry);ifTypeeq'Army'thenoutput;run;c02s3d4.sas64IfaDROPorKEEPstatementisusedatthesametimeasadatasetoption,thestatementisappliedfirst.prog2.militaryTYPECODECITYSTATECOUNTRYAIRPORTCompiledataarmy(keep=CodeAirport);setprog2.military(drop=CityStateCountry);ifTypeeq'Army'thenoutput;run;PDV65...... 2-32Chapter2ControllingInputandOutputprog2.militaryTYPECODECITYSTATECOUNTRYAIRPORTdataarmy(keep=CodeAirport);setprog2.military(drop=CityStateCountry);ifTypeeq'Army'thenoutput;run;PDVDTYPECODEAIRPORT67......ControllingWhichObservationsAreReadBydefault,SASbeginsprocessingaSASdatasetwiththefirstobservationandcontinuesprocessinguntilthelastobservation.TheFIRSTOBS=andOBS=datasetoptionscanbeusedtocontrolwhichobservationsareprocessed.YoucanuseFIRSTOBS=andOBS=withinputdatasetsonly.YoucannotuseeitherdatasetoptionintheDATAstatement.68 2.3SelectingVariablesandObservations2-33TheOBS=DataSetOptionTheOBS=datasetoptionspecifiesanendingpointforprocessinganinputdataset.SAS-data-setSAS-data-set(OBS=(OBS=nn))Thisoptionspecifiesthenumberofthelastobservationtoprocess,nothowmanyobservationsshouldbeprocessed.69nspecifiesapositiveintegerthatislessthanorequaltothenumberofobservationsinthedataset,orzero.TheOBS=datasetoptionoverridestheOBS=systemoptionfortheindividualdataset.ToguaranteethatSASprocessesallobservationsfromadataset,youcanusethefollowingsyntax:SAS-data-set(OBS=MAX)ControllingWhichObservationsAreReadTheOBS=datasetoptionintheSETstatementstopsreadingafterobservation25intheprog2.militarydataset.dataarmy;setprog2.military(obs=25);ifTypeeq'Army'thenoutput;run;70 2-34Chapter2ControllingInputandOutputControllingWhichObservationsAreReadPartialLog60dataarmy;61setprog2.military(obs=25);62ifTypeeq'Army'thenoutput;63run;NOTE:Therewere25observationsreadfromthedatasetPROG2.MILITARY.NOTE:ThedatasetWORK.ARMYhas10observationsand6variables.71TheFIRSTOBS=DataSetOptionTheFIRSTOBS=datasetoptionspecifiesastartingpointforprocessinganinputdataset.SAS-data-setSAS-data-set(FIRSTOBS=(FIRSTOBS=nn))FIRSTOBS=andOBS=areoftenusedtogethertodefinearangeofobservationstobeprocessed.72nspecifiesapositiveintegerthatislessthanorequaltothenumberofobservationsinthedataset.TheFIRSTOBS=datasetoptionoverridestheFIRSTOBS=systemoptionfortheindividualdataset. 2.3SelectingVariablesandObservations2-35ControllingWhichObservationsAreReadTheFIRSTOBS=andOBS=datasetoptionsintheSETstatementread15observationsfromprog2.military.Processingbeginswithobservation11andendsafterobservation25.dataarmy;setprog2.military(firstobs=11obs=25);ifTypeeq'Army'thenoutput;run;c02s3d5.sas73ControllingWhichObservationsAreReadPartialLog67dataarmy;68setprog2.military(firstobs=11obs=25);69ifTypeeq'Army'thenoutput;70run;NOTE:Therewere15observationsreadfromthedatasetPROG2.MILITARY.NOTE:ThedatasetWORK.ARMYhas5observationsand6variables.74 2-36Chapter2ControllingInputandOutputControllingWhichObservationsAreReadTheFIRSTOBS=andOBS=datasetoptionscanalsobeusedinaPROCstep.ThefollowingPROCPRINTstepbeginsprocessingthearmydatasetatobservation2andstopsprocessingthearmydatasetafterobservation4.procprintdata=army(firstobs=2obs=4);varCodeAirport;run;75TheDROP=andKEEP=datasetoptionscanbeusedtoexcludevariablesfromprocessingduringaPROCstep:procprintdata=army(drop=CityStateCountryType);run;However,DROP=andKEEP=donotaffecttheorderinwhichthevariablesareprocessed.ControllingWhichObservationsAreReadPartialLog75procprintdata=army(firstobs=2obs=4);76varCodeAirport;77run;NOTE:Therewere3observationsreadfromthedatasetWORK.ARMY.76 2.3SelectingVariablesandObservations2-37ControllingWhichObservationsAreReadPROCPRINTOutputObsCodeAirport2LGFLagunaArmyAirField3SYLRobertsArmyAirField4HGTTusiArmyHeliport77 2-38Chapter2ControllingInputandOutputExercises5.ControllingInputandOutputSizeRecallthattheprog2.elementsdatasetcontainsinformationabouttheknownelementsontheperiodictable.Eachobservationcontainsanelement’sname,symbol,atomicnumber,andstate.ThevalueofStatereferstowhethertheelementisagas,liquid,solid,orsyntheticatroomtemperature.PartialListingofprog2.elementsAtomicNameSymbolNumStateActiniumAc89SolidAluminumAl13SolidAmericiumAm95SyntheticAntimonySb51SolidArgonAr18GasCreatetwoSASdatasets:naturalandsynthetic.Thenaturaldatasetwillcontaininformationaboutelementsthataresolids,liquids,orgasesatroomtemperature.Thenaturaldatasetwillcontainthreevariables(Name,AtomicNum,andState)and92observations.Thesyntheticdatasetwillcontaintwovariables(NameandAtomicNum)and21observations.PartialListingofnaturalAtomicObsNameNumState1Actinium89Solid2Aluminum13Solid3Antimony51Solid4Argon18Gas5Arsenic33SolidPartialListingofsyntheticAtomicObsNameNum1Americium952Berkelium973Californium984Curium965Dubnium105 2.4WritingtoanExternalFile2-392.4WritingtoanExternalFileObjectivesWriteobservationsfromaSASdatasettoacomma-delimitedexternalfile.Insertheaderandfooterrecordsintoanexternalfile.80IntroductionTheprog2.maysalesdatasetcontainsinformationabouthouses.Readthisdatasetandwritethedatatoanexternalfile.prog2.maysalesListSellSellDescriptionDateDatePriceColonial1380314001355200Townhouse1389414016241200Townhouse1410814392238100Ranch1458514736219400Victorian148051510635820081 2-40Chapter2ControllingInputandOutputIntroductionraw-data-fileDescription,ListDate,SellDate,SellPriceColonial,16OCT1997,02MAY1998,355200Townhouse,15JAN1998,17MAY1998,241200Townhouse,17AUG1998,28MAY1999,238100Ranch,07DEC1999,06MAY2000,219400Victorian,14JUL2000,11MAY2001,358200Data:PROG2.MAYSALES82TheODSCSVALLStatementODSstatementsareglobalinmostrespects.TheyenableyoutomanageoutputobjectsproducedbyproceduresandtheDATAstep.TheODSCSVALLstatementcreatesacomma-delimitedfilefromoutputobjectswiththesecharacteristics:datavaluesareenclosedindoublequotestitlesandfootnotesarepreserved.ODSCSVALLODSCSVALLFILE=FILE=file-specificationfile-specification;;ODSCSVALLCLOSEODSCSVALLCLOSE;;THECSVALLoptionisnewinSAS®9.83TheODSCSVALLStatementTocreatethedesiredexternalfile,placeaPRINTprocedurestepbetweentheODSstatements.odscsvallfile='raw-data-file';footnote1'data:prog2.maysales';procprintnoobsdata=prog2.maysales;formatlistdateselldatedate9.;run;odscsvallclose;84c02s4d1.sas 2.4WritingtoanExternalFile2-41TheODSCSVALLStatementraw-data-file85InordertoviewtheexternalfilefromaninteractiveSASsession,youcanusetheResultsViewerortheFSLISTprocedure(describedbelow).TheDATAStepYoucanusetheDATAsteptowriteacustomreportdatatoanexternalfiletobereadbyotherprogramminglanguagesorsoftware.86YoucanalsousetheEXPORTproceduretoreaddatafromaSASdatasetandwriteittoanexternaldatasource.Externaldatasourcescanincludedatabasetables,PCfiles,spreadsheets,anddelimitedexternalfiles.PROCEXPORTisavailableinthefollowingoperatingenvironments:OS/2,UNIX,OpenVMS,andWindows. 2-42Chapter2ControllingInputandOutputTheDATAStepREADINGFROMWRITINGTOANEXTERNALFILEANEXTERNALFILETheDATAstatementTheDATAstatementbeginstheDATAstep.beginstheDATAstep.TheINFILEstatementidentifiesTheFILEstatementidentifiesanexternalfiletoreadanexternalfiletowritewithanINPUTstatement.withaPUTstatement.TheINPUTstatementdescribesThePUTstatementdescribesthearrangementofvaluesthearrangementofvaluesintheinputdatarecord.intheoutputdatarecord.87......TheDATAStatementUsually,theDATAstatementspecifiesatleastonedatasetnamethattheSASSystemusestocreateanoutputdataset.Usingthe_NULL_keywordasthedatasetnamecausesSAStoexecutetheDATAstepwithoutwritingobservationstoadataset.DATADATA_NULL_;_NULL_;88 2.4WritingtoanExternalFile2-43TheFILEStatementTheFILEstatementcanbeusedtospecifytheoutputdestinationforsubsequentPUTstatements.GeneralformoftheFILEstatement:FILEFILEfile-specificationfile-specification<;>;YoucanusetheFILEstatementinconditionalprocessing(IF-THEN/ELSEorSELECT)becauseitisexecutable.89file-specificationidentifiesanexternalfilethattheDATAstepusestowriteoutputfromaPUTstatement.file-specificationcanhavetheseforms:'external-file'specifiesthephysicalnameofanexternalfile,whichisenclosedinquotationmarks.Thephysicalnameisthenamebywhichtheoperatingenvironmentrecognizesthefile.filerefspecifiesthefilereferenceforanexternalfile.YoumusthavepreviouslyassociatedafilerefwithanexternalfileinaFILENAMEstatementorfunction,orinanappropriateoperatingenvironmentcommand.LOGisareservedfilereferencethatdirectstheoutputfromsubsequentPUTstatementstothelog.PRINTisareservedfilereferencethatdirectstheoutputfromsubsequentPUTstatementstothesameprintfileastheoutputthatisproducedbySASprocedures.Thedefaultfile-specificationisLOG.YoucanusemultipleFILEstatementstowritetomorethanoneexternalfileinasingleDATAstep.YoucanusePRINTasyourinitialfile-specificationtoverifythecontentsofyouroutputfilebeforecreatinganexternalfile. 2-44Chapter2ControllingInputandOutputTheFILENAMEstatementassociatesaSASfilereferencewithanexternalfileoranoutputdevice.FILENAMEfileref'external-file';filerefspecifiesanySASname.device-typespecifiesthetypeofdeviceortheaccessmethodthatisusedifthefilerefpointstoaninputoroutputdeviceorlocationthatisnotaphysicalfile.'external-file'specifiesaphysicalnameofanexternalfile.Thephysicalnameisthenamethatisrecognizedbytheoperatingenvironment.host-optionsspecifydetails,suchasfileattributesandprocessingattributes,thatarespecifictoyouroperatingenvironment.ThePUTStatementThePUTstatementcanwritelinestotheexternalfilethatisspecifiedinthemostrecentlyexecutedFILEstatement.GeneralformofthePUTstatement:PUTPUTvariable-1variable-2…variable-nvariable-1variable-2…variable-n;;Withsimplelistoutput,youlistthenamesofthevariableswhosevaluesyouwantwritten.ThePUTstatementwritesavariablevalue,insertsasingleblank,andthenwritesthenextvalue.90variable-1throughvariable-narethevariableswhosevaluesarewritten.Inadditiontovariablevalues,youcanalsouseaquotedcharacterstringtospecifyastringoftexttowrite.Whenaquotedcharacterstringiswritten,SASdoesnotautomaticallyinsertablankspace.Theoutputpointerstopsatthecolumnthatimmediatelyfollowsthelastcharacterinthestring.Thevaluesofcharactervariablesareleft-alignedinthefield;leadingandtrailingblanksareremoved.AnullPUTstatementcanbeusedtooutputablankline:put; 2.4WritingtoanExternalFile2-45ModifiedListOutputModifiedlistoutputincreasestheversatilityofthePUTstatementbecauseyoucanspecifyaSASformattocontrolhowthevariablevaluesarewritten.Tousemodifiedlistoutput,usethecolon(:)formatmodifierinthePUTstatementbetweenthevariablenameandtheformat.PUTPUTvariable-1variable-1::format-1.format-1.variable-2variable-2::format-2.format-2.……variable-nvariable-n::format-n.format-n.;;91format-1.throughformat-n.specifyformatstousewhenthedatavaluesarewritten.YoucanspecifyeitherSASformatsoruser-definedformats.ThecolonformatmodifierenablesyoutospecifyaformatthatthePUTstatementusestowritethevariablevalue.Allleadingandtrailingblanksaredeleted,andeachvalueisfollowedbyasingleblank.SeeSASdocumentationforacompletelistofSASformatsandtheirusage. 2-46Chapter2ControllingInputandOutputWritingtoanExternalFiledata_null_;setprog2.maysales;file'raw-data-file';putDescriptionListDate:date9.SellDate:date9.SellPrice;run;Whyisthe$omittedafterDescriptioninthePUTstatement?c02s4d2.sas92AFILENAMEstatementcanbeusedtoassociatethefilerefEXTFILEwiththerawdatafile:filenameextfile'raw-data-file';TheFILEstatementcanbesubsequentlyrevised:fileextfile;WritingtoanExternalFilePartialLogNOTE:5recordswerewrittentothefile'raw-data-file'.Theminimumrecordlengthwas32.Themaximumrecordlengthwas36.NOTE:Therewere5observationsreadfromthedatasetPROG2.MAYSALES.CanyouusePROCPRINTtoviewtherawdatafile?93 2.4WritingtoanExternalFile2-47TheFSLISTProcedureTheFSLISTprocedureenablesyoutobrowseexternalfileswithinaninteractiveSASsession.YoucannotusetheFSLISTproceduretobrowseSASdatasets.PROCFSLISTPROCFSLISTFILEREF=FILEREF=file-specificationfile-specification<;>;RUNRUN;;RemembertoclosetheFSLISTwindowwhenyoufinishbrowsingyourexternalfile.94file-specificationspecifiestheexternalfiletobrowse.file-specificationmustbespecified,anditcanbeoneofthefollowing:'external-file'isthecompleteoperatingenvironmentfilespecificationfortheexternalfile.Youmustenclosethenameinquotationmarks.filerefspecifiesthefilerefofanexternalfile.YoumusthavepreviouslyassociatedthefilerefwithanexternalfileinaFILENAMEstatementorfunction,orinanappropriateoperatingenvironmentcommand.AliasesforFILEREF=includeFILE=,DDNAME=,andDD=.Youcanuseanytexteditoravailableforyouroperatingenvironmenttoviewtheexternalfile.Forinstance,WindowsuserscanuseNotepadorMicrosoftWord,UNIXuserscanuseemacsorvi,andz/OSuserscanuseISPF.ReadingfromanExternalFileprocfslistfileref='raw-data-file';run;PROCFSLISTOutputColonial16OCT199702MAY1998355200Townhouse15JAN199817MAY1998241200Townhouse17AUG199828MAY1999238100Ranch07DEC199906MAY2000219400Victorian14JUL200011MAY2001358200Howcanyouaddasinglerowofcolumnheadersbeforetherowsofdata?95 2-48Chapter2ControllingInputandOutputThe_N_AutomaticVariable(Review)The_N_automaticvariableiscreatedbyeveryDATAstep.EachtimethattheDATAsteploopspasttheDATAstatement,_N_isincrementedby1.Therefore,thevalueof_N_representsthenumberoftimesthattheDATAstepiterated._N_isaddedtotheProgramDataVector,butitisnotoutput.96WritingtoanExternalFiledata_null_;setprog2.maysales;file'raw-data-file';if_N_=1thenput'Description''ListDate''SellDate''SellPrice';putDescriptionListDate:date9.SellDate:date9.SellPrice;run;WhyisthesecondPUTstatementnotcontainedinanELSEstatement?c02s4d3.sas97TheIF-THENstatementshownabovecouldalsobewrittenasfollows:if_N_=1thenput'DescriptionListDateSellDateSellPrice';Exercisecautionwhenindentingorbreakinglineswithinaquotedstring.ThefollowingPUTstatementproducesunexpectedresults:if_N_=1thenput'DescriptionListDateSellDateSellPrice';Becauseoftheindentionwithinthequotedstring,thefollowingresultsareproduced:DescriptionListDateSellDateSellPrice 2.4WritingtoanExternalFile2-49WritingtoanExternalFileprocfslistfileref='raw-data-file';run;PROCFSLISTOutputDescriptionListDateSellDateSellPriceColonial16OCT199702MAY1998355200Townhouse15JAN199817MAY1998241200Townhouse17AUG199828MAY1999238100Ranch07DEC199906MAY2000219400Victorian14JUL200011MAY2001358200Howcanyouaddafooterrecordaftertherowsofdata?98TheEND=OptionintheSETStatementTheEND=optionintheSETstatementcreatesandnamesatemporaryvariablethatactsasanend-of-fileindicator.SETSETSAS-data-setSAS-data-setEND=END=variablevariable<;>;Thistemporaryvariableisinitializedto0.WhentheSETstatementreadsthelastobservationofthedatasetlisted,thevalueofthevariableissetto1.Thevariableisnotaddedtoanynewdataset.99END=isanoptionintheSETstatement.Itisnotadatasetoption;itisnotenclosedinparentheses. 2-50Chapter2ControllingInputandOutputWritingtoanExternalFiledata_null_;setprog2.maysalesend=IsLast;file'raw-data-file';if_N_=1thenput'Description''ListDate''SellDate''SellPrice';putDescriptionListDate:date9.SellDate:date9.SellPrice;ifIsLast=1thenput'Data:PROG2.MAYSALES';run;c02s4d4.sas100ifIsLast=1thenput'Data:PROG2.MAYSALES';couldbereplacedwithifIsLastthenput'Data:PROG2.MAYSALES';PartialListingofprog2.maysalesExecuteListSelldata_null_;DescriptionDateDatesetprog2.maysalesend=IsLast;file'raw-data-file';Colonial1380314001if_N_=1thenTownhouse1389414016put'Description''ListDate'Townhouse1410814392'SellDate''SellPrice';Ranch1458514736putDescriptionVictorian1480515106ListDate:date9.SellDate:date9.PartialListingofraw-data-fileSellPrice;ifIsLast=1thenput'Data:PROG2.MAYSALES';run;PDVDLISTSELLSELLDISLASTDESCRIPTIONDATEDATEPRICE_N_101...Theprog2.maysalesdatasetcontainsfourvariables:Description,ListDate,SellDate,andSellPrice.TheSellPricevariableisnotdisplayedintherepresentationsofprog2.maysales. 2.4WritingtoanExternalFile2-51PartialListingofprog2.maysalesListSelldata_null_;DescriptionDateDatesetprog2.maysalesend=IsLast;file'raw-data-file';Colonial1380314001if_N_=1thenTownhouse1389414016put'Description''ListDate'Townhouse1410814392'SellDate''SellPrice';Ranch1458514736putDescriptionVictorian1480515106ListDate:date9.SellDate:date9.PartialListingofraw-data-fileSellPrice;ifIsLast=1thenput'Data:PROG2.MAYSALES';run;PDVDLISTSELLSELLDISLASTDESCRIPTIONDATEDATEPRICE_N_0Colonial13803140013552001103......PartialListingofprog2.maysalesListSelldata_null_;DescriptionDateDateTruesetprog2.maysalesend=IsLast;file'raw-data-file';Colonial1380314001if_N_=1thenTownhouse1389414016put'Description''ListDate'Townhouse1410814392'SellDate''SellPrice';Ranch1458514736putDescriptionVictorian1480515106ListDate:date9.SellDate:date9.PartialListingofraw-data-fileSellPrice;DescriptionListDateifIsLast=1thenput'Data:PROG2.MAYSALES';run;PDVDLISTSELLSELLDISLASTDESCRIPTIONDATEDATEPRICE_N_0Colonial13803140013552001105......raw-data-filecontainsfourfieldsandsevenrecords.Notallfieldsandrecordsaredisplayedintherepresentationsofraw-data-file. 2-52Chapter2ControllingInputandOutputPartialListingofprog2.maysalesListSelldata_null_;DescriptionDateDatesetprog2.maysalesend=IsLast;file'raw-data-file';Colonial1380314001if_N_=1thenTownhouse1389414016put'Description''ListDate'Townhouse1410814392'SellDate''SellPrice';Ranch1458514736putDescriptionVictorian1480515106ListDate:date9.SellDate:date9.PartialListingofraw-data-fileSellPrice;DescriptionListDateifIsLast=1thenColonial16OCT1997put'Data:PROG2.MAYSALES';run;PDVDLISTSELLSELLDISLASTDESCRIPTIONDATEDATEPRICE_N_0Colonial13803140013552001106......PartialListingofprog2.maysalesListSelldata_null_;DescriptionDateDatesetprog2.maysalesend=IsLast;file'raw-data-file';Colonial1380314001if_N_=1thenTownhouse1389414016put'Description''ListDate'Townhouse1410814392'SellDate''SellPrice';Ranch1458514736putDescriptionVictorian1480515106ListDate:date9.FalseSellDate:date9.PartialListingofraw-data-fileSellPrice;DescriptionListDateifIsLast=1thenColonial16OCT1997put'Data:PROG2.MAYSALES';run;PDVDLISTSELLSELLDISLASTDESCRIPTIONDATEDATEPRICE_N_0Colonial13803140013552001107......PartialListingofprog2.maysalesListSelldata_null_;DescriptionDateDatesetprog2.maysalesend=IsLast;file'raw-data-file';Colonial1380314001if_N_=1thenTownhouse1389414016put'Description''ListDate'Townhouse1410814392'SellDate''SellPrice';Ranch1458514736putDescriptionVictorian1480515106ListDate:date9.FalseSellDate:date9.PartialListingofraw-data-fileSellPrice;DescriptionListDateifIsLast=1thenColonial16OCT1997put'Data:PROG2.MAYSALES';Townhouse15JAN1998run;PDVDLISTSELLSELLDISLASTDESCRIPTIONDATEDATEPRICE_N_0Townhouse13894140162412002108...... 2.4WritingtoanExternalFile2-53PartialListingofprog2.maysalesListSelldata_null_;DescriptionDateDatesetprog2.maysalesend=IsLast;file'raw-data-file';Colonial1380314001if_N_=1thenTownhouse1389414016put'Description''ListDate'Townhouse1410814392'SellDate''SellPrice';Ranch1458514736putDescriptionVictorian1480515106ListDate:date9.TrueSellDate:date9.PartialListingofraw-data-fileSellPrice;DescriptionListDateifIsLast=1thenColonial16OCT1997put'Data:PROG2.MAYSALES';Townhouse15JAN1998run;Victorian14JUL2000PDVData:PROG2.MAYSALESDLISTSELLSELLDISLASTDESCRIPTIONDATEDATEPRICE_N_1Victorian14805151063582005109......WritingtoanExternalFileprocfslistfileref='raw-data-file';run;PROCFSLISTOutputDescriptionListDateSellDateSellPriceColonial16OCT199702MAY1998355200Townhouse15JAN199817MAY1998241200Townhouse17AUG199828MAY1999238100Ranch07DEC199906MAY2000219400Victorian14JUL200011MAY2001358200Data:PROG2.MAYSALES110 2-54Chapter2ControllingInputandOutputSpecifyinganAlternateDelimiterUsetheDLM=optionintheFILEstatementtocreateafilewithanalternatedelimiter(otherthanablank).FILEFILEfile-specificationfile-specificationDLM='DLM='quoted-stringquoted-string''<;>;Youcanalsospecifyacharactervariablewhosevaluecontainsyourdelimiter,insteadofaquotedstring.111'quoted-string'specifiesanalternatedelimiter(otherthanthedefault,ablank)tobeusedforsimpleormodifiedlistoutput.Althoughacharacterstringorcharactervariableisaccepted,onlythefirstcharacterofthestringorvariableisusedastheoutputdelimiter.TospecifyatabcharacteronaPCoronUNIX,usedlm='09'x.Tospecifyatabcharacteronz/OS,usedlm='05'x.DLM=isanaliasforDELIMITER=.WritingtoanExternalFiledata_null_;setprog2.maysalesend=IsLast;file'raw-data-file'dlm=',';if_N_=1thenput'Description,ListDate,''SellDate,SellPrice';putDescriptionListDate:date9.SellDate:date9.SellPrice;ifIsLast=1thenput'Data:PROG2.MAYSALES';run;c02s4d5.sas112TheIF-THENstatementshownabovecanalsobewrittenasfollows:if_N_=1thenput'Description,ListDate,SellDate,SellPrice'; 2.4WritingtoanExternalFile2-55WritingtoanExternalFileprocfslistfileref='raw-data-file';run;PROCFSLISTOutputDescription,ListDate,SellDate,SellPriceColonial,16OCT1997,02MAY1998,355200Townhouse,15JAN1998,17MAY1998,241200Townhouse,17AUG1998,28MAY1999,238100Ranch,07DEC1999,06MAY2000,219400Victorian,14JUL2000,11MAY2001,358200Data:PROG2.MAYSALES113 2-56Chapter2ControllingInputandOutputExercises6.WritingtoanExternalFileThedatasetprog2.visitscontainsinformationaboutpatientswhovisitedaphysician'soffice.Createacomma-delimitedexternalfilecontainingtheinformationfromprog2.visits.Thenameofyourexternalfiledependsonyouroperatingenvironment.ThevaluesofDateshouldbeoutputusingtheMMDDYY10.format.Thefirstrecordintheexternalfileshouldcontaincolumnheaders.Thelastrecordintheexternalfileshouldcontainafooter.UsetheFSLISTproceduretoviewyourexternalfile.RemembertoclosetheFSLISTwindowwhenyoufinishbrowsingyourexternalfile.IfyouusetheWindowsoperatingenvironmentandassignanextensionofCSVtoyourexternalfile,donotattempttoviewitinExcelbydouble-clickingonthefileintheWindowsExplorer.BecausethefirstfieldonthefirstrecordisID,Excelmayinterpretthisasaspecialfiletypeandbeunabletoopenit.Listingofprog2.visitsIDDateFee243-88-436422JUL2001864.15193-27-981522JUL2001621.50278-80-579323JUL20011228.75926-36-394824JUL2001897.25618-96-176424JUL2001897.25679-72-175925JUL2001952.50618-96-176426JUL2001731.50679-72-175926JUL20011781.25236-76-157429JUL2001897.25345-10-391229JUL20011228.75679-72-175930JUL20011339.25278-80-579330JUL2001676.25ThevaluesofDatearedisplayedwithapermanentlyassignedDATE9.format.ThevaluesofDateshouldnotbeoutputusingthisformat. 2.4WritingtoanExternalFile2-57DesiredOutput(ExternalFile)ID,Date,Fee243-88-4364,07/22/2001,864.15193-27-9815,07/22/2001,621.5278-80-5793,07/23/2001,1228.75926-36-3948,07/24/2001,897.25618-96-1764,07/24/2001,897.25679-72-1759,07/25/2001,952.5618-96-1764,07/26/2001,731.5679-72-1759,07/26/2001,1781.25236-76-1574,07/29/2001,897.25345-10-3912,07/29/2001,1228.75679-72-1759,07/30/2001,1339.25278-80-5793,07/30/2001,676.25Data:PROG2.VISITS 2-58Chapter2ControllingInputandOutput2.5SolutionstoExercises1.OutputtingMultipleObservationsdatarotate;dropQtr1Qtr2Qtr3Qtr4;setprog2.donate;Qtr=1;Amount=Qtr1;output;Qtr=2;Amount=Qtr2;output;Qtr=3;Amount=Qtr3;output;Qtr=4;Amount=Qtr4;output;run;procprintdata=rotate;run;2.UsingConditionalLogictoOutputMultipleObservations(Optional)databuyhistory(keep=IDMethod);lengthMethod$13;setprog2.ffmethod;ifInterneteq'Y'thendo;Method='Internet';output;end;ifTelephoneeq'Y'thendo;Method='Telephone';output;end;ifTravelAgencyeq'Y'thendo;Method='TravelAgency';output;end;run;procprintdata=buyhistory;varIDMethod;run; 2.5SolutionstoExercises2-593.WritingtoMultipleSASDataSetsdatagasliquidsolidsynthetic;dropState;setprog2.elements;ifStateeq'Gas'thenoutputgas;elseifStateeq'Liquid'thenoutputliquid;elseifStateeq'Solid'thenoutputsolid;elseifStateeq'Synthetic'thenoutputsynthetic;run;procprintdata=liquid;run;4.WritingtoMultipleSASDataSets(Optional)datalanthanidesactinides;setprog2.elements;ifAtomicNumge57andAtomicNumle70thenoutputlanthanides;elseifAtomicNumge89andAtomicNumle102thenoutputactinides;run;procprintdata=lanthanides;run;procprintdata=actinides;run; 2-60Chapter2ControllingInputandOutput5.ControllingInputandOutputSizedatanatural(keep=NameAtomicNumState)synthetic(keep=NameAtomicNum);setprog2.elements;ifStateeq'Synthetic'thenoutputsynthetic;elseoutputnatural;run;procprintdata=natural;run;procprintdata=synthetic;run;AlternateSolution:datanatural(keep=NameAtomicNumState)synthetic(keep=NameAtomicNum);setprog2.elements;ifStatein('Solid','Liquid','Gas')thenoutputnatural;elseoutputsynthetic;run;procprintdata=natural;run;procprintdata=synthetic;run; 2.5SolutionstoExercises2-616.WritingtoanExternalFiledata_null_;/*TheEND=optionintheSETstatementisusedtodeterminewhenSASreadsthelastobservationfromPROG2.VISITS.*/setprog2.visitsend=IsLast;/*TheDLM=optionintheFILEstatementseparatesthedatavalueswithcommas.*/file'visits.dat'dlm=',';/*The_N_automaticvariableisusedtowritecolumnheadersatthetopoftherawdatafile.*/if_N_eq1thenput'ID,Date,Fee';putIDDate:mmddyy10.Fee;/*ThevalueofISLAST,createdusingtheEND=optionintheSETstatement,isusedtocreateafooteratthebottomoftherawdatafile.*/ifIsLast=1thenput'Data:PROG2.VISITS';run;/*TheFILEstatementisapplicabletotheWindowsandUNIXoperatingenvironments.z/OSusersshoulduse:file'.prog2.rawdata(visits)';*/procfslistfileref='visits.dat';run;/*ThePROCFSLISTstatementisapplicabletotheWindowsandUNIXoperatingenvironments.z/OSusersshoulduse:procfslistfileref='.prog2.rawdata(visits)';run*/ 2-62Chapter2ControllingInputandOutput®AlternateSolution(SAS9):/*Thefile'visits.dat'isapplicabletotheWindowsandUNIXoperatingenvironments.z/OSusersshoulduse'.prog2.rawdata(visits)'inboththeODSandPROCFSLISTstatements.*/odscsvallfile='visits.dat';title1;footnote1'Data:PROG2.VISITS';procprintnoobsdata=prog2.visits;formatDatemmddyy10.;run;odscsvallclose;procfslistfile='visits.dat';run; Chapter3SummarizingData3.1CreatinganAccumulatingTotalVariable.....................................................................3-33.2AccumulatingTotalsforaGroupofData...................................................................3-123.3SolutionstoExercises.................................................................................................3-30 3-2Chapter3SummarizingData 3.1CreatinganAccumulatingTotalVariable3-33.1CreatinganAccumulatingTotalVariableObjectivesUnderstandhowtheSASSysteminitializesthevalueofavariableinthePDV.PreventreinitializationofavariableinthePDV.Createanaccumulatingvariable.3CreatinganAccumulatingVariableSaleDateSaleAmtTheSASdatasetprog2.daysales01APR2001498.49containsdailysalesdata02APR2001946.50foraretailstore.Thereis03APR2001994.97oneobservationforeach04APR2001564.59dayinAprilshowingthe05APR2001783.01date(SaleDate)and06APR2001228.82thetotalreceiptsforthat07APR2001930.57day(SaleAmt).08APR2001211.4709APR2001156.2310APR2001117.6911APR2001374.7312APR2001252.734 3-4Chapter3SummarizingDataCreatinganAccumulatingVariableThestoremanageralsowantstoseearunningtotalofsalesforthemonthasofeachday.PartialOutputSaleSaleDateAmtMth2Dte01APR2001498.49498.4902APR2001946.501444.9903APR2001994.972439.9604APR2001564.593004.5505APR2001783.013787.565TheinputSASdatasetmustbesortedbyDateforthefollowingmethodtowork.CreatingMth2DteBydefault,variablescreatedwithanassignmentstatementareinitializedtomissingatthetopoftheDATAstep.Mth2Dte=Mth2Dte+SaleAmt;Anaccumulatingvariablemustretainitsvaluefromoneobservationtothenext.6 3.1CreatinganAccumulatingTotalVariable3-5TheRETAINStatementGeneralformoftheRETAINstatement:RETAINRETAINvariable-namevariable-name<…;>…;TheRETAINstatementpreventsSASfromre-initializingthevaluesofnewvariablesatthetopoftheDATAstep.PreviousvaluesofretainedvariablesareavailableforprocessingacrossiterationsoftheDATAstep.7TheRETAINStatementTheRETAINstatementretainsthevalueofthevariableinthePDVacrossiterationsoftheDATAstepinitializestheretainedvariabletomissingbeforethefirstexecutionoftheDATAstepifaninitialvalueisnotspecifiedisacompile-time-onlystatement.8TheRETAINstatementhasnoeffectonvariablesthatarereadwithSET,MERGE,orUPDATEstatements;valuesreadfromSASdatasetsareautomaticallyretained.AvariablereferencedintheRETAINstatementappearsintheoutputSASdatasetonlyifitisgivenaninitialvalueorreferencedelsewhereintheDATAstep. 3-6Chapter3SummarizingDataRetainMth2DteandSetanInitialValueretainMth2Dte0;Ifyoudonotsupplyaninitialvalue,allthevaluesofMth2Dtewillbemissing.9CreatinganAccumulatingVariabledatamnthtot;setprog2.daysales;retainMth2Dte0;Mth2Dte=Mth2Dte+SaleAmt;run;10Compiledatamnthtot;setprog2.daysales;SaleDateSaleAmtretainMth2Dte0;Mth2Dte=Mth2Dte+SaleAmt;15066498.49run;15067946.5015068994.9715069564.5915070783.01RSALEDATESALEAMTMTH2DTE11...... 3.1CreatinganAccumulatingTotalVariable3-7datamnthtot;setprog2.daysales;SaleDateSaleAmtretainMth2Dte0;Mth2Dte=Mth2Dte+SaleAmt;15066498.49run;15067946.5015068994.9715069564.5915070783.010+498.49RSALEDATESALEAMTMTH2DTE15066.498.49.498.49014......datamnthtot;setprog2.daysales;SaleDateSaleAmtretainMth2Dte0;Mth2Dte=Mth2Dte+SaleAmt;15066498.49run;15067946.50Implicit15068994.97ImplicitReturnOutput15069564.5915070783.01RSALEDATESALEAMTMTH2DTE15066498.49498.49Writeoutobservationtomnthtot.15......datamnthtot;setprog2.daysales;SaleDateSaleAmtSaleDateSaleAmtretainMth2Dte0;Mth2Dte=Mth2Dte+SaleAmt;15066498.4915066498.49run;15067946.5015067946.5015068994.9715068994.9715069564.5915069564.5915070783.0115070783.01498.49+946.50RSALEDATESALEAMTMTH2DTE1506615067498.49946.501444.99498.4920...... 3-8Chapter3SummarizingDatadatamnthtot;setprog2.daysales;SaleDateSaleAmtretainMth2Dte0;Mth2Dte=Mth2Dte+SaleAmt;15066498.49run;15067946.50Implicit15068994.97ImplicitReturnOutput15069564.5915070783.01RSALEDATESALEAMTMTH2DTE15067946.501444.9921Writeoutobservationtomnthtot.......CreatinganAccumulatingVariableprocprintdata=mnthtotnoobs;formatSaleDatedate9.;run;PartialPROCPRINTOutputSaleSaleDateAmtMth2Dte01APR2001498.49498.4902APR2001946.501444.9903APR2001994.972439.9604APR2001564.593004.5505APR2001783.013787.5631AccumulatingTotals:MissingValuesdatamnthtot;setprog2.daysales;retainMth2Dte0;Mth2dte=Mth2Dte+SaleAmt;run;WhathappensiftherearemissingvaluesforSaleAmt?32 3.1CreatinganAccumulatingTotalVariable3-9UndesirableOutputSaleSaleDateAmtMth2Dte01APR2001498.49498.4902APR2001..03APR2001994.97.04APR2001564.59.05APR2001783.01.SubsequentvaluesofMissingvalueMth2Dtearemissing.33......Theresultofanymathematicaloperationonamissingvalueismissing.Withtheabovecode,onemissingvalueforSaleAmtcausesallsubsequentvaluesofMth2Dtetobemissing.YoucansolvethisproblembyusingtheSUMfunctionintheassignmentstatement:Mth2Dte=sum(Mth2Dte,SaleAmt);SeeChapter5,“DataTransformations,”fordetails.However,thesumstatementisamoreefficientsolutionbecauseitdoesnotrequireSAStoinvoketheSUMfunction.TheSumStatementWhenyoucreateanaccumulatingvariable,analternativetotheRETAINstatementisthesumstatement.Generalformofthesumstatement:variablevariable++expressionexpression;;34Liketheassignmentstatement,thesumstatementdoesnotbeginwithakeyword. 3-10Chapter3SummarizingDataTheSumStatementThesumstatementcreatesthevariableontheleftsideoftheplussignifitdoesnotalreadyexistinitializesthevariabletozerobeforethefirstiterationoftheDATAstepautomaticallyretainsthevariableaddsthevalueoftheexpressiontothevariableatexecutionignoresmissingvalues.35AccumulatingTotals:MissingValuesdatamnthtot2;setprog2.daysales2;Mth2Dte+SaleAmt;run;36AccumulatingTotals:MissingValuesprocprintdata=mnthtot2noobs;formatSaleDatedate9.;run;PartialPROCPRINTOutputSaleDateSaleAmtMth2Dte01APR2001498.49498.4902APR2001.498.4903APR2001994.971493.4604APR2001564.592058.0505APR2001783.012841.0637c03s1d1.sas 3.1CreatinganAccumulatingTotalVariable3-11Exercises1.CreatinganAccumulatingTotalVariableThedatasetprog2.statescontainsthestatename(State),thedatethestateenteredtheUnitedStates(EnterDate),andthesizeofthestateinsquaremiles(Size)forall50U.S.states.ThedatasetissortedbyEnterDate.PartialListingofprog2.statesStateEnterDateSizeDelaware07DEC17871955Pennsylvania12DEC178744820NewJersey18DEC17877418Georgia02JAN178857918Connecticut09JAN17884845Massachusetts06FEB17887838Maryland28APR17889775SouthCarolina23MAY178830111ThevariableEnterDatehasthepermanentformatDATE9.CreatetheSASdatasetwork.usareathatcontainsthenewvariableTotArea,whichisarunningtotalofthesizeoftheUnitedStatesaseachstatewasadded,andthenewvariableNumStates,whichshowshowmanystateswereintheUnitedStatesatthatpoint.PartialListingofwork.usareaNumObsStateEnterDateSizeTotAreaStates1Delaware07DEC17871955195512Pennsylvania12DEC1787448204677523NewJersey18DEC178774185419334Georgia02JAN17885791811211145Connecticut09JAN1788484511695656Massachusetts06FEB1788783812479467Maryland28APR1788977513456978SouthCarolina23MAY1788301111646808 3-12Chapter3SummarizingData3.2AccumulatingTotalsforaGroupofDataObjectivesDefineFirst.andLast.processing.Calculateanaccumulatingtotalforgroupsofdata.UseasubsettingIFstatementtooutputselectedobservations.40AccumulatingTotalsforGroupsEmpIDSalaryDivTheSASdatasetprog2.empsalsE0000442000HUMREScontainseachemployee’sE0000934000FINACEidentificationnumberE0001127000FLTOPS(EmpID),salaryE0003620000FINACE(Salary),anddivisionE0003719000FINACE(Div).ThereisoneE0004819000FLTOPSE0007727000APTOPSobservationforeachE0009720000APTOPSemployee.E0010731000FINACEE0012320000APTOPSE0015527000APTOPSE0017144000SALES41 3.2AccumulatingTotalsforaGroupofData3-13DesiredOutputHumanresourceswantsanewdatasetthatshowsthetotalsalarypaidforeachdivision.DivDivSalAPTOPS410000FINACE163000FLTOPS318000HUMRES181000SALES37300042GroupingtheDataAYoumustgroupthedataintheSASdatasetbeforeByoucanperformprocessing.EDC43ReviewoftheSORTProcedureYoucanrearrangetheobservationsintogroupsusingtheSORTprocedure.GeneralformofaPROCSORTstep:PROCSORTPROCSORTDATA=DATA=input-SAS-data-setinput-SAS-data-setoutput-SAS-data-set>;;BYBY<>BY-variable...BY-variable...;;RUNRUN;;44 3-14Chapter3SummarizingDataTheSORTProcedureTheSORTprocedurerearrangestheobservationsinaDATAsetcansortonmultiplevariablescreatesaSASdatasetthatisasortedcopyoftheinputSASdatasetreplacestheinputdatasetbydefault.45SortingbyDivprocsortdata=prog2.empsalsout=salsort;byDiv;run;46ProcessingDatainGroupsDivSalaryDivSalAPTOPS20000APTOPS100000170000APTOPS50000FINACE25000FINACE20000FINACE2300095000FINACE27000SALES10000SALES120002200047...... 3.2AccumulatingTotalsforaGroupofData3-15BY-GroupProcessingGeneralformofaBYstatementusedwiththeSETstatement:DATADATAoutput-SAS-data-setoutput-SAS-data-set;;SETSETinput-SAS-data-setinput-SAS-data-set;;BYBYBY-variable…BY-variable…;;RUNRUN;;TheBYstatementintheDATAstepenablesyoutoprocessyourdataingroups.48WhenaBYstatementisusedwithaSETstatement,thedatamust•besortedorgroupedinorderbytheBYvariable(s),or•haveanindexbasedontheBYvariable(s),or•resideinaDBMStable.BY-GroupProcessingdatadivsals(keep=DivDivSal);setsalsort;byDiv;additionalSASstatementsrun;49 3-16Chapter3SummarizingDataBY-GroupProcessingABYstatementinaDATAstepcreatestemporaryvariablesforeachvariablelistedintheBYstatement.GeneralformofthenamesofBYvariablesinaDATAstep:FirstFirst..BY-variableBY-variableLastLast..BY-variableBY-variable50First.andLast.ValuesTheFirst.variablehasavalueof1forthefirstobservationinaBYgroup;otherwise,itequals0.TheLast.variablehasavalueof1forthelastobservationinaBYgroup;otherwise,itequals0.Usethesetemporaryvariablestoconditionallyprocesssorted,grouped,orindexeddata.51First./Last.ExampleLookahead.DivSalaryFirst.DivAPTOPS200001APTOPS100000APTOPS50000Last.DivFINACE250000FINACE20000FINACE23000FINACE27000SALES10000SALES1200052...... 3.2AccumulatingTotalsforaGroupofData3-17First./Last.ExampleLookahead.DivSalaryFirst.DivAPTOPS200000APTOPS100000APTOPS50000Last.DivFINACE250001FINACE20000FINACE23000FINACE27000SALES10000SALES1200054......WhatMustHappenWhen?Thereisathree-stepprocessforaccumulatingtotals.1.SettheaccumulatingvariabletozeroatthestartofeachBYgroup.2.Incrementtheaccumulatingvariablewithasumstatement(automaticallyretains).3.OutputonlythelastobservationofeachBYgroup.56AccumulatingTotalsforGroups1.SettheaccumulatingvariabletozeroatthestartofeachBYgroup.datadivsals(keep=DivDivSal);setsalsort;byDiv;ifFirst.DivthenDivSal=0;additionalSASstatementsrun;57 3-18Chapter3SummarizingDataAccumulatingTotalsforGroups2.Incrementtheaccumulatingvariablewithasumstatement(automaticallyretains).datadivsals(keep=DivDivSal);setsalsort;byDiv;ifFirst.DivthenDivSal=0;DivSal+Salary;additionalSASstatementsrun;58First./Last.ExampleDivSalaryDivSalAPTOPS2000020000APTOPS100000120000APTOPS50000170000FINACE2500025000FINACE2000045000FINACE2300068000FINACE2700091000SALES1000010000SALES120002200059SubsettingIFStatementThesubsettingIFdefinesaconditionthattheobservationmustmeettobefurtherprocessedbytheDATAstep.GeneralformofthesubsettingIFstatement:IFIFexpressionexpression;;Iftheexpressionistrue,theDATAstepcontinuesprocessingthecurrentobservation.Iftheexpressionisfalse,SASreturnstothetopoftheDATAstep.60 3.2AccumulatingTotalsforaGroupofData3-19AccumulatingTotalsforGroups3.OutputonlythelastobservationofeachBYgroup.datadivsals(keep=DivDivSal);setsalsort;byDiv;ifFirst.DivthenDivSal=0;DivSal+Salary;ifLast.Div;run;61ThestatementifLast.BY-variable;meansifLast.BY-variableistrue.Anumericvalueisconsideredtrueifitisnotequaltozeroandnotmissing.SubsettingIFStatementInitializePDV.InitializePDV.ExecuteprogramExecuteprogramstatements.statements.NOIstheifcondition;conditiontrue?ExecuteadditionalYESExecuteadditionalprogramstatements.programstatements.OutputobservationOutputobservationtoSASdataset.toSASdataset.62...... 3-20Chapter3SummarizingDataAccumulatingTotalsforGroupsPartialLogNOTE:Therewere39observationsreadfromthedatasetWORK.SALSORT.NOTE:ThedatasetWORK.DIVSALShas5observationsand2variables.63AccumulatingTotalsforGroupsprocprintdata=divsalsnoobs;run;PROCPRINTOutputDivDivSalAPTOPS410000FINACE163000FLTOPS318000HUMRES181000SALES37300064c03s2d1.sasInputDataEmpIDSalaryRegionDivTheSASdatasetprog2.regsalsE0000442000EHUMREScontainseachE0000934000WFINACEE0001127000WFLTOPSemployee’sIDnumberE0003620000WFINACE(EmpID),salaryE0003719000EFINACE(Salary),regionE0007727000CAPTOPS(Region),anddivisionE0009720000EAPTOPS(Div).ThereisoneE0010731000EFINACEobservationforeachE0012320000NCAPTOPSE0015527000WAPTOPSemployee.E0017144000WSALESE0018837000WHUMRESE0019643000CAPTOPSE0021031000EAPTOPSE00222250000NCSALESE0023641000WAPTOPS65 3.2AccumulatingTotalsforaGroupofData3-21DesiredOutputHumanresourceswantsanewdatasetthatshowsthetotalsalarypaidandthetotalnumberofemployeesforeachdivisionineachregion.PartialOutputNumRegionDivDivSalEmpsCAPTOPS700002EAPTOPS830003EFINACE1090004EFLTOPS1220003EHUMRES1780005NCAPTOPS370002NCFLTOPS28000166SortingbyRegionandDivThedatamustbesortedbyRegionandDiv.Regionistheprimarysortvariable.Divisthesecondarysortvariable.procsortdata=prog2.regsalsout=regsort;byRegionDiv;run;67SortingbyRegionandDivprocprintdata=regsortnoobs;varRegionDivSalary;run;PartialPROCPRINTOutputRegionDivSalaryCAPTOPS27000CAPTOPS43000EAPTOPS20000EAPTOPS31000EAPTOPS32000EFINACE19000EFINACE3100068 3-22Chapter3SummarizingDataMultipleBYVariablesdataregdivsals;setregsort;byRegionDiv;additionalSASstatementsrun;69MultipleBYVariables:ExampleLookahead.RegionDivFirst.RegionCAPTOPS1CAPTOPSCAPTOPSFirst.DivEAPTOPS1EFINACEEFINACELast.RegionNCFINACE0NCSALESNCSALESNCSALESLast.DivNCSALES070......MultipleBYVariables:ExampleLookahead.RegionDivFirst.RegionCAPTOPS0CAPTOPSCAPTOPSFirst.DivEAPTOPS0EFINACEEFINACELast.RegionNCFINACE0NCSALESNCSALESNCSALESLast.DivNCSALES071...... 3.2AccumulatingTotalsforaGroupofData3-23MultipleBYVariables:ExampleLookahead.RegionDivFirst.RegionCAPTOPS0CAPTOPSCAPTOPSFirst.DivEAPTOPS0EFINACEEFINACELast.RegionNCFINACE1NCSALESNCSALESNCSALESLast.DivNCSALES172......MultipleBYVariables:ExampleLookahead.RegionDivFirst.RegionCAPTOPS1CAPTOPSCAPTOPSFirst.DivEAPTOPS1EFINACEEFINACELast.RegionNCFINACE0NCSALESNCSALESNCSALESLast.DivNCSALES173......MultipleBYVariablesWhenyouusemorethanonevariableintheBYstatement,achangeintheprimaryvariableforcesLast.BY-variable=1forthesecondaryvariable.First.Last.First.RegionDivRegionRegionDivLast.DivCAPTOPS1010CAPTOPS0101EAPTOPS1010EAPTOPS0000EAPTOPS0001EFINACE001074 3-24Chapter3SummarizingDataMultipleBYVariablesdataregdivsals(keep=RegionDivDivSalNumEmps);setregsort;byRegionDiv;ifFirst.Divthendo;DivSal=0;NumEmps=0;end;DivSal+Salary;NumEmps+1;ifLast.Div;run;75MultipleBYVariablesPartialLogNOTE:Therewere39observationsreadfromthedatasetWORK.REGSORT.NOTE:ThedatasetWORK.REGDIVSALShas14observationsand4variables.76MultipleBYVariablesprocprintdata=regdivsalsnoobs;run;PartialPROCPRINTOutputNumRegionDivDivSalEmpsCAPTOPS700002EAPTOPS830003EFINACE1090004EFLTOPS1220003c03s2d2.sas77 3.2AccumulatingTotalsforaGroupofData3-25Exercises2.AccumulatingTotalsforaGroupofDataThedatasetprog2.flymileshasoneobservationforeachtripthatafrequentflyermadewithanairline.Itshowsthefrequentflyernumber(ID)andthenumberofmilesearnedforthattrip(Miles).PartialListingofprog2.flymilesIDMilesF212763F161272F31351800F25122733F25122859F31351437F313511553F31351312F1612245ThedatasetisnotsortedbyID.Createadatasetnamedwork.freqmilesthathasoneobservationforeachfrequentflyeraswellasanewvariablenamedTotMiles,whichshowsthetotalnumberoffrequentflyermilesthepersonearned.Listingofwork.freqmilesTotObsIDMiles1F16158132F21264543F25122102084F313515090 3-26Chapter3SummarizingData3.AccumulatingTotalsforGroupsofDataUsingMorethanOneBYVariableThedatasetprog2.flydayshasoneobservationforeachtripthatafrequentflyermadewithanairline.Itcontainsthefrequentflyernumber(ID),thenumberofmilesearnedforthattrip(Miles),andavariablethatindicateswhetherthemileswereearnedonaweekdayflight(Code='MF')oraweekendflight(Code='SS').PartialListingofprog2.flydaysIDCodeMilesF212SS763F161MF272F31351SS800F25122SS733F25122MF859F31351SS437F31351SS1553F31351MF312F161SS2245CreateaSASdatasetnamedwork.daymilesthatshowshowmanytotalmileseachfrequentflyerearnedforeachtypeofflight.Listingofwork.daymilesTotObsIDCodeMiles1F161MF26332F161SS31803F212MF9764F212SS54785F25122MF70076F25122SS32017F31351MF21008F31351SS2990 3.2AccumulatingTotalsforaGroupofData3-274.DetectingDuplicateObservationsUsingBY-GroupProcessing(Optional)Thedatasetprog2.dupsalshasthevariablesEmpIDandSalary.PartialListingofprog2.dupsalsEmpIDSalaryE0029037000E0037925000E0003719000E0003727526E0023641000E0023659978E0037236000E0037241011E0042131000E0042417000Thedatasetshouldcontainonlyoneobservationperemployee(thatis,allemployeeIDnumbersshouldbeunique).However,aSASprogrammerdiscoveredsomeduplicateobservations.WriteaDATAstepthatsendsduplicateobservationstoadatasetnamedwork.baddataandnon-duplicateobservationstoadatasetnamedwork.gooddata.Listingofwork.gooddataNon-DuplicateEmpIDsObsEmpIDSalary1E00048190002E00077270003E00107310004E00123200005E00155270006E00188370007E00196430008E00210310009E002593200010E002722200011E002903700012E003792500013E003882500014E004213100015E004241700016E0042727000 3-28Chapter3SummarizingDataPartialListingofwork.baddataDuplicateEmpIDsObsEmpIDSalary1E00004420002E00004629023E00009340004E00009497615E00011270006E00011381937E00036200008E00036270579E000371900010E000372752611E0009720000Hint:Tocreatetwodatasets,listbothintheDATAstatement.Tocontroltowhichdatasetanobservationiswritten,usetheOUTPUTstatement.(RefertoChapter2,“ControllingInputandOutput.”) 3.2AccumulatingTotalsforaGroupofData3-295.RotatingaDataSet(Optional)Abookstoresellsthreetypesofproducts:books,cards,andperiodicals.TheSASdatasetprog2.salesbydayhasanobservationforeachproducteachdaythatthestoreisopen(threeobservationsforeachday).ThevariableSalesshowsthetotalsalesforthatproductoneachday.Partiallistingofprog2.salesbydayDateMerchTypeSales01APR2001Books1602.2701APR2001Cards669.4901APR2001Periodicals1651.4902APR2001Books2818.3302APR2001Cards217.1902APR2001Periodicals87.6203APR2001Books751.6703APR2001Cards125.7803APR2001Periodicals72.20ThevariableDateisaSASdate,butithasapermanentDATE9.formatapplied.ThedatasetisalreadysortedbyDate.Rotatethedatasetsothatithasonlyoneobservationperday,andavariableforeachtypeofmerchandise.Thevalueforeachmerchandisetypeshouldbethesalesforthatproductonthatday.PartialOutputObsDateBooksCardsPeriodicals101APR20011602.27669.491651.49202APR20012818.33217.1987.62303APR2001751.67125.7872.20404APR2001890.872370.92587.84505APR20011926.04165.25265.96606APR2001141.111739.463725.15707APR20011406.71117.76706.78808APR2001153.1878.77171.64 3-30Chapter3SummarizingData3.3SolutionstoExercises1.CreatinganAccumulatingTotalVariabledatausarea;setprog2.states;TotArea+Size;NumStates+1;/*SumstatementscreateTotAreaandNumStates,retain,setinitialvaluesto0,andignoremissingvaluesofsize*/run;procprintdata=usarea;run;2.AccumulatingTotalsforaGroupofData/*DatamustbesortedorindexedforBY-groupprocessing*/procsortdata=prog2.flymilesout=milesort;byID;run;datafreqmiles(drop=miles);setmilesort;byID;/*BYstatementcreateFirst.IDandLast.ID*/ifFirst.IDthenTotMiles=0;/*SetTotMilesto0whenIDchanges*/TotMiles+Miles;/*SumstatementcreatesTotMiles,retainsit,setsinitialvalueto0,andignoresmissingvaluesofmiles*/ifLast.ID;/*OutputonlythelastofeachBYgroup*/run;/*Createalistreportofthedatasettoverifytheoutput*/procprintdata=freqmiles;run; 3.3SolutionstoExercises3-313.AccumulatingTotalsforaGroupofDataUsingMorethanOneBYVariable/*DatamustbesortedorindexedforBY-groupprocessing*/procsortdata=prog2.flydaysout=daysort;byIDCode;run;datadaymiles(drop=Miles);setdaysort;byIDCode;/*BYstatementcreatesFirst.ID,Last.IDFirst.Code,andLast.Code*/ifFirst.CodethenTotMiles=0;/*SetTotMilesto0whensubgroupchanges*/TotMiles+Miles;/*SumstatementcreatesTotMiles,retainsit,setsinitialvalueto0,andignoresmissingvaluesofmiles*/ifLast.Codethenoutput;/*OutputonlythelastofeachBYgroup*/run;/*Createalistreportofthedatasettoverifytheoutput*/procprintdata=daymiles;run; 3-32Chapter3SummarizingData4.DetectingDuplicateObservationsUsingBY-GroupProcessing(Optional)/*DatamustbesortedorindexedforBY-groupprocessing*/procsortdata=prog2.dupsalsout=dupsort;byEmpID;run;datagooddatabaddata;/*BothnewdatasetsmustbelistedontheDATAstatement*/setdupsort;byEmpID;/*BYstatementcreatesFirst.EmpIDandLast.EmpID*/ifFirst.EmpIDandLast.EmpID/*firstandlastofthisIDmeansit’sunique*/thenoutputgooddata;elseoutputbaddata;run;/*Createlistreportstoverifyresults*/procprintdata=gooddata;title'Non-DuplicateEmpIDs';run;procprintdata=baddata;title'DuplicateEmpIDs';run;5.RotatingaDataSet(Optional)datawidebooks(drop=MerchTypeSales);setprog2.salesbyday;bydate;retainBooksCardsPeriodicals;ifMerchType='Books'thenBooks=Sales;elseifMerchType='Cards'thenCards=Sales;elseifMerchType='Periodicals'thenPeriodicals=Sales;iflast.datethenoutput;run;procprintdata=widebooks;title'RotatingaDataSet';run; Chapter4ReadingandWritingDifferentTypesofData4.1ReadingDelimitedRawDataFiles................................................................................4-34.2ControllingWhenaRecordLoads..............................................................................4-304.3ReadingHierarchicalRawDataFiles..........................................................................4-564.4SolutionstoExercises.................................................................................................4-90 4-2Chapter4ReadingandWritingDifferentTypesofData 4.1ReadingDelimitedRawDataFiles4-34.1ReadingDelimitedRawDataFilesObjectivesReadaspace-delimitedrawdatafile.Readacomma-delimitedrawdatafile.Readarawdatafilewithmissingdataattheendofarow.Readarawdatafilewithmissingdatarepresentedbyconsecutivedelimiters.3ListInputwiththeDefaultDelimiter500014feb19891325305000211nov19891525405000322oct199190530500044feb19931725505000524jun19931705105000620dec1994180520Thedataisnotinfixedcolumns.Thefieldsareseparatedbyspaces.Thereisonenonstandardfield.4 4-4Chapter4ReadingandWritingDifferentTypesofDataListInputRawdatawithfieldsthatarenotinfixedcolumnsiscalledfreeformat.Uselistinputtoreadfree-formatdata.ThelistinputstylesignalstotheSASSystemthatfieldsareseparatedbydelimiters.SASthenreadsfromnon-delimitertodelimiterinsteadoffromaspecificlocationontherawdatarecord.5DelimitersCommondelimitersareblankscommastabcharactersAspace(blank)isthedefaultdelimiter.6 4.1ReadingDelimitedRawDataFiles4-5ListInputGeneralformoftheINPUTstatementforlistinput:INPUTINPUTvar-1var-1$$var-2var-2......var-nvar-n;;Youmustspecifythevariablesintheorderthattheyappearintherawdatafile.Forstandarddata,specifya$afterthevariablenameifitischaracter.Nosymbolafterthevariablenameindicatesanumericvariable.7The$isnotrequiredifthevariablewaspreviouslydefinedascharacter.InputDataThesecondfieldisadate.HowdoesSASstoredates?500014feb19891325305000211nov19891525405000322oct199190530500044feb19931725505000524jun19931705105000620dec19941805208 4-6Chapter4ReadingandWritingDifferentTypesofDataStandardDataThetermstandarddatareferstocharacterandnumericdatathatSASrecognizesautomatically.Someexamplesofstandardnumericdatainclude35469.933E5(exponentialnotation)-46859.Standardcharacterdataisanycharacteryoucantypeonyourkeyboard.Standardcharactervaluesarealwaysleft-justifiedbySAS.9Thefollowingaretheonlyacceptablecharactersinastandardnumericfield:0123456789.EeDd-+E,e,D,anddrepresentexponentialnotationinastandardnumericfield.Forexample,3E5isanalternativewayofwriting300000.NonstandardDataThetermnonstandarddatareferstocharacterandnumericdatathatSASdoesnotrecognizeautomatically.Examplesofnonstandardnumericdatainclude12/12/201229FEB20004,242$89,000.10Examplesofnon-standardcharacterdataincludepreservingleadingblanksincharactervalues,hexadecimalcharacters,andvaluessurroundedbydoublequotes. 4.1ReadingDelimitedRawDataFiles4-7InformatsToreadinnonstandarddata,youmustapplyaninformat.Generalformofaninformat:<<$$>>INFORMAT-NAMEINFORMAT-NAME.<.

>InformatsareinstructionsthatspecifyhowSASreadsrawdata.11$indicatesacharacterinformat.INFORMAT-NAMEisthenameoftheinformat.wisanoptionalfieldwidth.Ifnowidthisspecified,SASusesthedefaultwidthforthatinformat..istherequireddelimiter.disanoptionaldecimalspecificationfornumericinformats. 4-8Chapter4ReadingandWritingDifferentTypesofDataInformatsExamplesofinformatsareCOMMAw.readsnumericdata($4,242)andstripsoutselectednonnumericcharacters,suchasdollarsignsandcommas.MMDDYYw.readsdatesintheform12/31/2012.DATEw.readsdatesintheform29Feb2000.12Withdateinformats,SASusesthespecifiedwidthtodeterminehowfartoread.Withthelistinputstyle,thelengthoftheinformatisnotimportantbecausethedelimiterdetermineshowfarSASreads.Withdateformats,thespecifiedwidthdetermines•whetherSASdisplaysatwo-orfour-digityear•whetherSASdisplaysdividersiftheyarevalidforthatformat.SpecifyinganInformatTospecifyaninformat,usethecolon(:)formatmodifierintheINPUTstatementbetweenthevariablenameandtheinformat.GeneralformofaformatmodifierinanINPUTstatement:INPUTINPUTvariablevariable::informatinformat;;13 4.1ReadingDelimitedRawDataFiles4-9WithouttheColonThecolonsignalsthatSASshouldreadfromdelimitertodelimiter.Ifthecolonisomitted,SASreadsthelengthoftheinformat,whichmaycauseittoreadpasttheendofthefield.Noerrormessageisprinted.Youmightseeinvaliddatamessagesorunexpecteddatavalues.14Example:Supposethatyouhavethefollowingdatarecord:Cheema,3May1975,FandtheprogrammerforgotthecolonintheINPUTstatement.datanew;infile'birthdays.dat'dlm=',';inputName$Birthdaydate9.Gender$;run;ForBirthday,SASreadsexactlyninecharacters,startingatthefirstpositionoftheDatefield.Thisresultsinthefollowingvalue:3May1975,Commasarenotvalidinadatefield.WhenSASattemptstoconvertthisvaluetoadate,itprintsaninvaliddatamessagetothelogandsetsthevalueofBirthdaytomissing.ReadingaDelimitedRawDataFiledataairplanes;infile'raw-data-file';inputID$InService:date9.PassCapCargoCap;run;HowdoesSASdeterminethelengthsofthesevariables?15 4-10Chapter4ReadingandWritingDifferentTypesofDataLengthsofVariablesWhenyouuselistinput,thedefaultlengthforcharacterandnumericvariablesiseightbytes.YoucansetthelengthofcharactervariableswithaLENGTHstatementorwithaninformat.GeneralformofaLENGTHstatement:LENGTHLENGTHvariable-namevariable-name<$><$>length-specification...length-specification...;;16Youdonotneedtosetthelengthsofnumericvariableswhenreadingwithlistinputbecausethedefaultwidthofeightbytesissufficient.Youcannotstoreanumericinmorethaneightbytes.Storinganumericinlessthaneightbytesreducesitsprecisionandcancauseunexpectedresults.SettingtheLengthofaVariabledataairplanes;lengthID$5;infile'raw-data-file';inputID$InService:date9.PassCapCargoCap;run;17Aninformatcanalsosetthelengthofacharactervariable.Thefollowingcodeproducesthesameresultasthecodeusedintheexample:dataairplanes;infile'raw-data-file';inputID:$5.InService:date9.PassCapCargoCap;run;Ifyouusethismethodtosetthelengthsofcharactervariables,becertaintousethecolonmodifier. 4.1ReadingDelimitedRawDataFiles4-11Compiledataairplanes;RawDataFilelengthID$5;500014feb1989132530infile'raw-data-file';5000211nov1989152540inputID$5000322oct199190530InService:date9.500044feb19931725505000524jun1993170510PassCapCargoCap;5000620dec1994180520run;InputBufferPDVID$518......Compiledataairplanes;RawDataFilelengthID$5;500014feb1989132530infile'raw-data-file';5000211nov1989152540inputID$5000322oct199190530InService:date9.500044feb19931725505000524jun1993170510PassCapCargoCap;5000620dec1994180520run;InputBufferPDVIDINSERVICEPASSCAPCARGOCAP$NNN588819......TheLENGTHstatementcreatestheIDvariable.TheINPUTstatementcreatestheothervariables. 4-12Chapter4ReadingandWritingDifferentTypesofDatadataairplanes;RawDataFilelengthID$5;500014feb1989132530infile'raw-data-file';5000211nov1989152540inputID$5000322oct199190530InService:date9.500044feb19931725505000524jun1993170510PassCapCargoCap;5000620dec1994180520run;InputBuffer500014feb1989132530PDVIDINSERVICEPASSCAPCARGOCAP$NNN58885000110627.132.530.22...SASreadsthefieldsfromnon-delimitertodelimiterintheorderthattheyappearontheINPUTstatement.dataairplanes;RawDataFilelengthID$5;500014feb1989132530infile'raw-data-file';5000211nov1989152540inputID$5000322oct199190530InService:date9.500044feb19931725505000524jun1993170510PassCapCargoCap;5000620dec1994180520run;ImplicitreturnImplicitInputBufferoutput500014feb1989132530PDVIDINSERVICEPASSCAPCARGOCAP$NNN58885000110627.132.530.23Writeoutobservationtoairplanes....... 4.1ReadingDelimitedRawDataFiles4-13ReadingaRawDataFilewithListInputprocprintdata=airplanesnoobs;run;PROCPRINTOutputInPassCargoIDServiceCapCap5000110627132530500021090715254050003116179053050004120881725505000512228170510500061277218052033InServiceappearsasaSASdate,thatis,thenumberofdayssinceJanuary1,1960.Tochangethedate’sappearance,applyaSASdateformatwithaFORMATstatementinthePRINTprocedure.YoucanalsouseaFORMATstatementintheDATAsteptopermanentlyassociateaformatwithavariable.Non-DefaultDelimiterThefieldsareseparatedbycommas.50001,4feb1989,132,53050002,11nov1989,152,54050003,22oct1991,90,53050004,4feb1993,172,55050005,24jun1993,170,51050006,20dec1994,180,52034 4-14Chapter4ReadingandWritingDifferentTypesofDataUsingtheDLM=OptionTheDLM=optionsetsacharacterorcharactersthatSASrecognizesasadelimiterintherawdatafile.GeneralformoftheINFILEstatementwiththeDLM=option:INFILEINFILE'raw-data-file'raw-data-file'DLM=''DLM='delimiter(s)delimiter(s)';';Anycharacteryoucantypeonyourkeyboardcanbeadelimiter.Youcanalsousehexadecimalcharacters.35IfyouspecifymorethanonedelimiterintheDLM=option,anyofthosecharactersisrecognizedasadelimiter.Forexample,DLM=',!'indicatesthateitheracommaoranexclamationpointactsasadelimiter.Bydefault,twoormoreconsecutivedelimitersaretreatedasone;therefore,acommaandanexclamationpointtogetherarealsotreatedasadelimiter.Oneexampleofahexadecimalcharacterisatabcharacter.TospecifyatabcharacteronaPCoronUNIX,typedlm='09'x.Tospecifyatabcharacteronz/OS,typedlm='05'x.YoucanfindthehexadecimalrepresentationofaprintablecharacterusingtheHEXw.formatinSAS.Fornon-printablecharacterslikeatabcharacter,youshouldconsultaprogrammingreferenceforyouroperatingsystem.SpecifyingaDelimiterdataairplanes2;lengthID$5;infile'raw-data-file'dlm=',';inputID$InService:date9.PassCapCargoCap;run;36 4.1ReadingDelimitedRawDataFiles4-15MissingDataattheEndofaRow50001,4feb1989,13250002,11nov1989,152,54050003,22oct1991,90,53050004,4feb1993,17250005,24jun1993,170,51050006,20dec1994,180,52037MissingDataattheEndofaRowBydefault,whenthereismissingdataattheendofarow,1.SASloadsthenextrecordtofinishtheobservation2.anoteiswrittentothelog3.SASloadsanewrecordatthetopoftheDATAstepandcontinuesprocessing.38 4-16Chapter4ReadingandWritingDifferentTypesofDatadataairplanes3;RawDataFilelengthID$5;50001,4feb1989,132infile'raw-data-file'50002,11nov1989,152,540dlm=',';50003,22oct1991,90,530inputID$50004,4feb1993,172InService:date9.50005,24jun1993,170,510PassCapCargoCap;50006,20dec1994,180,520run;InputBuffer50001,4feb1989,132NodataPDVIDINSERVICEPASSCAPCARGOCAP$NNN58885000110627.132..41......Bydefault,whenSASreachestheendofarecordwithoutfindingdataforallvariablesintheINPUTstatement,itskipstothenextlinetofinishtheobservation.Thiscanyieldunexpectedresults.dataairplanes3;RawDataFilelengthID$5;50001,4feb1989,132infile'raw-data-file'50002,11nov1989,152,540dlm=',';50003,22oct1991,90,530inputID$50004,4feb1993,172InService:date9.50005,24jun1993,170,510PassCapCargoCap;50006,20dec1994,180,520run;InputBuffer50002,11nov1989,152,540SASloadsPDVnextrecord.IDINSERVICEPASSCAPCARGOCAP$NNN58885000110627.132.50002.42...... 4.1ReadingDelimitedRawDataFiles4-17dataairplanes3;RawDataFilelengthID$5;50001,4feb1989,132infile'raw-data-file'50002,11nov1989,152,540dlm=',';50003,22oct1991,90,530inputID$50004,4feb1993,172InService:date9.50005,24jun1993,170,510PassCapCargoCap;50006,20dec1994,180,520run;ImplicitInputBufferoutput50002,11nov1989,152,540PDVIDINSERVICEPASSCAPCARGOCAP$NNN58885000110627.132.50002.43Writeoutobservationtoairplanes3.......PartialLogNOTE:6recordswerereadfromtheinfile'aircraft3.dat'.Theminimumrecordlengthwas19.Themaximumrecordlengthwas26.NOTE:SASwenttoanewlinewhenINPUTstatementreachedpasttheendofaline.NOTE:ThedatasetWORK.AIRPLANES3has4observationsand4variables.47ThenumberofrecordsreaddoesnotmatchthenumberofobservationsintheSASdataset. 4-18Chapter4ReadingandWritingDifferentTypesofDataMissingDataattheEndoftheRowprocprintdata=airplanes3noobs;run;PROCPRINTOutputInPassCargoIDServiceCapCap500011062713250002500031161790530500041208817250005500061277218052048TheMISSOVEROptionTheMISSOVERoptionpreventsSASfromloadinganewrecordwhentheendofthecurrentrecordisreached.GeneralformoftheINFILEstatementwiththeMISSOVERoption:INFILEINFILE'raw-data-file''raw-data-file'MISSOVER;MISSOVER;IfSASreachestheendoftherowwithoutfindingvaluesforallfields,variableswithoutvaluesaresettomissing.49UsingtheMISSOVEROptiondataairplanes3;lengthID$5;infile'raw-data-file'dlm=','missover;inputID$InService:date9.PassCapCargoCap;run;50 4.1ReadingDelimitedRawDataFiles4-19UsingtheMISSOVEROptionPartialSASLogNOTE:6recordswerereadfromtheinfile'aircraft3.dat'.Theminimumrecordlengthwas19.Themaximumrecordlengthwas26.NOTE:ThedatasetWORK.AIRPLANES3has6observationsand4variables.51UsingtheMISSOVEROptionprocprintdata=airplanes3noobs;run;PROCPRINTOutputInPassCargoIDServiceCapCap5000110627132.50002109071525405000311617905305000412088172.5000512228170510500061277218052052 4-20Chapter4ReadingandWritingDifferentTypesofDataTheMISSOVERoptionisalsovalidinformattedandcolumninputandcanbeusedwhenyouwanttoensurethatincompletefieldsaresettomissing.Supposethereisarawdatafilewiththefollowingvalues:122333Iftheshorterrecordsarenotpaddedwithblanks,readingthefilewiththefollowingcodeproducesallmissingvalues:datanums;infile'file-name'missover;inputnum4.;run;Noticetheinformat.ThisspecifiesthatSASistolookforexactlyfourbytesofdata.Inthiscase,theMISSOVERoptionindicatesthevariableistobesettomissingifthefieldisthreebytesorless.TheTRUNCOVERoptionenablesSAStoreadvariable-lengthrecordswithoutsettingincompletefieldstomissing.Ifthesamerawdatafileisreadwiththecodedatanums;infile'file-name'truncover;inputnum4.;run;theresultingvaluesare1,22,333.Whenusedwithlistinputandwithoutinformats,theMISSOVERandTRUNCOVERoptionsproducethesameresults.AnotherINFILEstatementoptionthatdealswithvariablelengthrecordsisthePADoption.ThePADoptioninstructsSAStomakeallrecordsthesamelengthbyaddingspacestotheendofshorterrecords.AllrecordsarepaddedtoeitherthedefaultrecordlengthortherecordlengthspecifiedbytheLRECL=option.ItisoftenusedintheWindowsoperatingenvironmentwithcolumnorformattedinputtopreventcarriagereturnsfromaffectinghowrawdataisread.ThePADoptionisnotappropriateforreadingdelimitedfileswithlistinputbecauseitcancauseunexpectedresults.Thisisespeciallytrueifthedataisdelimitedwithspacesorifthereispotentiallymorethanonemissingfieldattheendofsomerows. 4.1ReadingDelimitedRawDataFiles4-21MissingValueswithoutPlaceholdersThereismissingdatarepresentedbytwoconsecutivedelimiters.50001,4feb1989,,53050002,11nov1989,132,54050003,22oct1991,90,53050004,4feb1993,172,55050005,24jun1993,,51050006,20dec1994,180,52053MissingValueswithoutPlaceholdersBydefault,SAStreatstwoconsecutivedelimitersasone.Missingdatashouldberepresentedbyaplaceholder.50001,4feb1989,.,53054Aplaceholdercanbeaperiodifthedataisnumeric,oraspaceifthedataischaracterandthefileisnotspace-delimited. 4-22Chapter4ReadingandWritingDifferentTypesofDataMissingValueswithoutPlaceholdersdataairplanes4;lengthID$5;infile'raw-data-file'dlm=',';inputID$InService:date9.PassCapCargoCap;run;55dataairplanes4;RawDataFilelengthID$5;50001,4feb1989,,530infile'raw-data-file'50002,11nov1989,132,540dlm=',';50003,22oct1991,90,530inputID$50004,4feb1993,172,550InService:date9.50005,24jun1993,,510PassCapCargoCap;50006,20dec1994,180,520run;InputBuffer50001,4feb1989,,530NodataPDVIDINSERVICEPASSCAPCARGOCAP$NNN58885000110627.530..58......dataairplanes4;RawDataFilelengthID$5;50001,4feb1989,,530infile'raw-data-file'50002,11nov1989,132,540dlm=',';50003,22oct1991,90,530inputID$50004,4feb1993,172,550InService:date9.50005,24jun1993,,510PassCapCargoCap;50006,20dec1994,180,520run;InputBuffer50002,11nov1989,132,540SASloadsPDVnextrecord.IDINSERVICEPASSCAPCARGOCAP$NNN58885000110627.530..60...... 4.1ReadingDelimitedRawDataFiles4-23dataairplanes4;RawDataFilelengthID$5;50001,4feb1989,,530infile'raw-data-file'50002,11nov1989,132,540dlm=',';50003,22oct1991,90,530inputID$50004,4feb1993,172,550InService:date9.50005,24jun1993,,510PassCapCargoCap;50006,20dec1994,180,520run;ImplicitreturnImplicitInputBufferoutput50002,11nov1989,132,540PDVIDINSERVICEPASSCAPCARGOCAP$NNN58885000110627.530.50002.62Writeoutobservationtoairplanes4.......MissingValueswithoutPlaceholdersPartialLogNOTE:6recordswerereadfromtheinfile'aircraft4.dat'.Theminimumrecordlengthwas21.Themaximumrecordlengthwas26.NOTE:SASwenttoanewlinewhenINPUTstatementreachedpasttheendofaline.NOTE:ThedatasetWORK.AIRPLANES4has4observationsand4variables.66MissingValueswithoutPlaceholdersprocprintdata=airplanes4noobs;run;PROCPRINTOutputInPassCargoIDServiceCapCap500011062753050002500031161790530500041208817255050005122285105000667 4-24Chapter4ReadingandWritingDifferentTypesofDataMissingValueswithoutPlaceholdersIfyourdatadoesnothaveplaceholders,usetheDSDoption.50001,4feb1989,,53068TheDSDOptionGeneralformoftheDSDoptionintheINFILEstatement:INFILEINFILE'file-name'file-name'DSD;'DSD;69 4.1ReadingDelimitedRawDataFiles4-25TheDSDOptionTheDSDoptionsetsthedefaultdelimitertoacommatreatsconsecutivedelimitersasmissingvaluesenablesSAStoreadvalueswithembeddeddelimitersifthevalueissurroundedbydoublequotes.70Forexample,thefollowingrecordiscomma-delimited,butthesalaryvaluehasanembeddedcomma.Zoellner,Jane,"$55,000"TheDSDoptionsignalsSAStoignoredelimitersthataresurroundedbydoublequotes.UsingtheDSDOptiondataairplanes4;lengthID$5;infile'raw-data-file'dsd;inputID$InService:date9.PassCapCargoCap;run;71 4-26Chapter4ReadingandWritingDifferentTypesofDataMissingValuesWithoutPlaceholdersPartialLogNOTE:6recordswerereadfromtheinfile'aircraft4.dat'.Theminimumrecordlengthwas22.Themaximumrecordlengthwas25.NOTE:ThedatasetWORK.AIRPLANES4has6observationsand4variables.72UsingtheDSDOptionprocprintdata=airplanes4noobs;run;PROCPRINTOutputInPassCargoIDServiceCapCap5000110627.530500021090713254050003116179053050004120881725505000512228.510500061277218052073INFILEStatementOptionsProblemOptionNon-blankdelimitersDLM='delimiter(s)'MissingdataatendofrowMISSOVERMissingdatarepresentedbyDSDconsecutivedelimitersand/orEmbeddeddelimiterswherevaluesaresurroundedbydoublequotesTheseoptionscanbeusedseparatelyortogetherintheINFILEstatement.74 4.1ReadingDelimitedRawDataFiles4-27Exercises1.ReadingNonstandardDataTheSTATESrawdatafilecontainsinformationonstatesize,population,anddateofstatehood.Theorderandlayoutofthefieldsareasfollows:OrderFieldNotes1StateNameLongestvalueis16characters2StatePopulationWritteninCOMMA9.3StateSizeSquaremiles(numericfield)4DateofStatehoodWritteninDATE9.SampleRecordsAlabama!4,447,100!50750!14DEC1819Alaska!626,932!570374!03JAN1959Arizona!5,130,632!113642!14FEB1912Arkansas!2,673,400!52075!15JUN1836California!33,871,648!155973!09SEP1850Colorado!4,301,261!103729!01AUG1876Connecticut!3,405,565!4845!09JAN1788UsetheSTATESrawdatafiletocreatethework.statesdatasetlistedbelow.PartialListingofwork.statesEnterObsStatePopulationSizeDate1Alabama444710050750-511522Alaska626932570374-3633Arizona5130632113642-174884Arkansas267340052075-451245California33871648155973-399256Colorado4301261103729-304677Connecticut34055654845-628138Delaware7836001955-628469Florida1598237853997-4194110Georgia818645357918-62820ThevariableEnterDateisaSASdate,anditisdisplayedasthenumberofdayssinceJanuary1,1960bydefault.Toviewthevaluesascalendardates,applyaSASdateformat(SeeSection1.4,“ReviewofDisplayingSASDataSets.”)YoucanapplytheformatwithaFORMATstatementineithertheDATAsteporthePROCPRINTstep. 4-28Chapter4ReadingandWritingDifferentTypesofData2.UsingINFILEStatementOptionstoChangeDefaultsTheAROMASrawdatafilecontainsinformationondifferentconditionsandpossiblearomatherapycures.Foreachrecord,theconditionislistedfirstandfollowedbyasmanyasthreepossiblecures.OrderFieldNotes1ConditionLongestvalueis11characters.2PossibleCureLongestvalueis11characters.3PossibleCureLongestvalueis11characters.4PossibleCureLongestvalueis11characters.SampleRecordsANGER"YlangYlang"ANXIETYBergamotPetitgrainBOREDOMLemongrassDEPRESSIONBasilBergamotImmortelleDULLNESSGrapefruitLemongrassLimeGRIEFMelissaHEADACHEChamomileLavenderFATIGUEBasilPeppermintRosemaryINSOMNIAChamomileLavenderMarjoramThefieldsareseparatedbyspaces,andonefieldhasembeddeddelimiterswithquotesaroundthevalue.Alltherecordsdonothavevaluesforallfields.UsetheAROMASrawdatafiletocreatethework.aromasdatasetlistedbelow.AromatherapyDataSetObsConditionCure1Cure2Cure31ANGERYlangYlang2ANXIETYBergamotPetitgrain3BOREDOMLemongrass4DEPRESSIONBasilBergamotImmortelle5DULLNESSGrapefruitLemongrassLime6GRIEFMelissa7HEADACHEChamomileLavender8FATIGUEBasilPeppermintRosemary9INSOMNIAChamomileLavenderMarjoram10MIGRAINELavender11STRESSBenzoinBergamotChamomile12VERTIGOLavenderPeppermint13SHOCKPeppermintPetitgrainThisdatasetisnotintendedasmedicaladviceorasaguidetoaromatherapy. 4.1ReadingDelimitedRawDataFiles4-293.ReadingaFixed-ColumnRawDataFilewithVariableLengthRecords(Optional)TheAROMASFrawdatafileisafixed-columnversionoftheAROMASrawdatafile.Ithasthefollowinglayout:FieldStartingPositionFieldLengthCondition110Cure11111Cure22211Cure33311SamplerecordsANGERYlangYlangANXIETYBergamotPetitgrainBOREDOMLemongrassDEPRESSIONBasilBergamotImmortelleDULLNESSGrapefruitLemongrassLimeGRIEFMelissaHEADACHEChamomileLavenderFATIGUEBasilPeppermintRosemaryINSOMNIAChamomileLavenderMarjoramMIGRAINELavenderSTRESSBenzoinBergamotChamomileVERTIGOLavenderPeppermintSHOCKPeppermintPetitgrainReadtheAROMASFrawdatafile,usingcolumnorformattedinput,andcreatethework.aromasfSASdataset.Verifythedatacarefully.YoushouldhavethesameoutputasinExercise2.Forhelponreadingrawdatawithformattedinput,reviewChapter1,Section2.Forhelponoptionsforfixed-columnrawdatafiles,readthenotesaboutMISSOVER,TRUNCOVER,andPAD.ThisexerciseisonlyappropriateforWindowsandUNIXusers. 4-30Chapter4ReadingandWritingDifferentTypesofData4.2ControllingWhenaRecordLoadsObjectivesReadarawdatafilewithmultiplerecordsperobservation.Readarawdatafilewithmixedrecordtypes.Subsetfromarawdatafile.Readarawdatafilewithmultipleobservationsperrecord.77MultipleRecordsPerObservationFarr,SueArawdatafilehasthreeAnaheim,CArecordsperemployee.869-7008Record1containsthefirstAnderson,KayB.andlastnames,record2Chicago,ILcontainsthecityandstate483-3321ofresidence,andrecord3Tennenbaum,MaryAnncontainstheemployee’sJefferson,MOphonenumber.589-903078 4.2ControllingWhenaRecordLoads4-31DesiredOutputTheSASdatasetshouldhaveoneobservationperemployee.LNameFNameCityStatePhoneFarrSueAnaheimCA869-7008AndersonKayB.ChicagoIL483-3321TennenbaumMaryAnnJeffersonMO589-903079TheINPUTStatementTheSASSystemloadsanewrecordintotheinputbufferwhenitencountersanINPUTstatement.YoucanhavemultipleINPUTstatementsinoneDATAstep.DATADATASAS-data-setSAS-data-set;;INPUTINPUTvar-1var-2var-3var-1var-2var-3;;INPUTINPUTvar-4var-5var-4var-5;;additionalSASstatementsadditionalSASstatementsEachINPUTstatementendswithasemicolon.80MultipleINPUTStatementsdataaddress;lengthLNameFName$20City$25State$2Phone$8;infile'raw-data-file'dlm=',';LoadRecordinputLName$FName$;LoadRecordinputCity$State$;LoadRecordinputPhone$;run;81...... 4-32Chapter4ReadingandWritingDifferentTypesofDataLinePointerControlsYoucanalsouselinepointercontrolstocontrolwhenSASloadsanewrecord.DATADATASAS-data-setSAS-data-set;;INPUTINPUTvar-1var-2var-3var-1var-2var-3//var-4var-5var-4var-5;;additionalSASstatementsadditionalSASstatementsSASloadsthenextrecordwhenitencountersaforwardslash.82ReadingMultipleRecordsPerObservationdataaddress;lengthLNameFName$20City$25State$2Phone$8;infile'raw-data-file'dlm=',';LoadRecordinputLName$FName$/LoadRecordCity$State$/LoadRecordPhone$;run;83......Theforwardslashisknownasarelativelinepointercontrolbecauseitmovesthepointerrelativetothelineonwhichitcurrentlyappears.Thereisalsoanabsolutelinepointercontrolthatmovesthepointertoaspecificline.#nmovesthepointertolinen. 4.2ControllingWhenaRecordLoads4-33Example:dataexample;infile'raw-data-file';input#1LName$FName$#2City$State$#3Phone$;run;ThiscodereadsLNameandFNamefromrecord1,CityandStatefromrecord2,andPhonefromrecord3onthefirstloopthroughtheDATAstep.Then,itreadsLNameandFNamefromrecord4,CityandStatefromrecord5,andPhonefromrecord6onthesecondloopthroughtheDATAstep,andsoonuntilSASreachestheendoftherawdatafile.TheabsolutelinepointerisusedtocontrolthedefaultorderofthevariablesintheSASdataset.ReadingMultipleRecordsPerObservationPartialLogNOTE:9recordswerereadfromtheinfile'addresses.dat'.Theminimumrecordlengthwas8.Themaximumrecordlengthwas20.NOTE:ThedatasetWORK.ADDRESShas3observationsand5variables.84ReadingMultipleRecordsPerObservationprocprintdata=addressnoobs;run;PROCPRINTOutputLNameFNameCityStatePhoneFarrSueAnaheimCA869-7008AndersonKayB.ChicagoIL483-3321TennenbaumMaryAnnJeffersonMO589-903085c04s2d1.sas 4-34Chapter4ReadingandWritingDifferentTypesofDataMixedRecordTypesNotallrecordshavethesameformat.101USA1-20-19993295.503034EUR30JAN19991876,30101USA1-30-19992938.00128USA2-5-19992908.741345EUR6FEB19993145,60109USA3-17-19992789.1086......TheEuropeansalesfiguresarewrittenwithacommainplaceofthedecimalpoint.TheCOMMAXw.dinformatreadsvaluesofthistype.DesiredOutputSalesSaleIDLocationDateAmount101USA142643295.503034EUR142741876.30101USA142742938.00128USA142802908.741345EUR142813145.60109USA143202789.1087 4.2ControllingWhenaRecordLoads4-35TheINPUTStatementMultipleINPUTstatementsareneeded.inputSalesID$Location$;ifLocation='USA'theninputSaleDate:mmddyy10.Amount;elseiflocation='EUR'theninputSaleDate:date9.Amount:commax8.;88datasales;RawDataFilelengthSalesID$4Location$3;101USA1-20-19993295.503034EUR30JAN19991876,30infile'raw-data-file';101USA1-30-19992938.00inputSalesID$Location$;128USA2-5-19992908.74ifLocation='USA'then1345EUR6FEB19993145,60inputSaleDate:mmddyy10.109USA3-17-19992789.10Amount;elseifLocation='EUR'theninputSaleDate:date9.Amount:commax8.;run;InputBuffer101USA1-20-19993295.50PDVSALESIDLOCATIONSALEDATEAMOUNT..91......datasales;RawDataFilelengthSalesID$4Location$3;101USA1-20-19993295.503034EUR30JAN19991876,30infile'raw-data-file';101USA1-30-19992938.00inputSalesID$Location$;128USA2-5-19992908.74TrueifLocation='USA'then1345EUR6FEB19993145,60inputSaleDate:mmddyy10.109USA3-17-19992789.10Amount;elseifLocation='EUR'theninputSaleDate:date9.Amount:commax8.;run;InputBuffer101USA1-20-19993295.50PDVSALESIDLOCATIONSALEDATEAMOUNT101USA..92...... 4-36Chapter4ReadingandWritingDifferentTypesofDatadatasales;RawDataFilelengthSalesID$4Location$3;101USA1-20-19993295.503034EUR30JAN19991876,30infile'raw-data-file';101USA1-30-19992938.00inputSalesID$Location$;128USA2-5-19992908.74ifLocation='USA'then1345EUR6FEB19993145,60inputSaleDate:mmddyy10.109USA3-17-19992789.10Amount;elseifLocation='EUR'theninputSaleDate:date9.Amount:commax8.;run;InputBuffer3034EUR30JAN19991876,30PDVSALESIDLOCATIONSALEDATEAMOUNT101USA..95......SASloadsanewrecordintotheinputbuffereachtimethatanINPUTstatementisencountered.TheINPUTStatementNOTE:6recordswerereadfromtheinfile'sales.dat'.Theminimumrecordlengthwas24.Themaximumrecordlengthwas26.NOTE:ThedatasetWORK.SALEShas3observationsand4variables.102... 4.2ControllingWhenaRecordLoads4-37UndesirableOutputSalesSaleIDLocationDateAmount101USA..101USA..1345EUR..103......TheSingleTrailing@Thesingletrailing@optionholdsarawdatarecordintheinputbufferuntilSASexecutesanINPUTstatementwithnotrailing@,orreachesthebottomoftheDATAstep.GeneralformofanINPUTstatementwiththesingletrailing@:INPUTINPUTvar1var2var3…@var1var2var3…@;;104ProcessingtheTrailing@HoldrecordfornextINPUTstatement.Loadnextrecord.inputSalesID$Location$@;iflocation='USA'theninputSaleDate:mmddyy10.Amount;elseifLocation='EUR'theninputSaleDate:date9.Amount:commax8.;105...... 4-38Chapter4ReadingandWritingDifferentTypesofDatadatasales;RawDataFilelengthSalesID$4101USA1-20-19993295.50Location$3;3034EUR30JAN19991876,30infile'raw-data-file';101USA1-30-19992938.00inputSalesID$Location$@;128USA2-5-19992908.74ifLocation='USA'then1345EUR6FEB19993145,60inputSaleDate:mmddyy10.109USA3-17-19992789.10Amount;elseifLocation='EUR'theninputSaleDate:date9.Amount:commax8.;run;InputBuffer101USA1-20-19993295.50PDVSALESIDLOCATIONSALEDATEAMOUNT..109......datasales;RawDataFilelengthSalesID$4101USA1-20-19993295.50Location$3;3034EUR30JAN19991876,30infile'raw-data-file';101USA1-30-19992938.00inputSalesID$Location$@;True128USA2-5-19992908.74ifLocation='USA'then1345EUR6FEB19993145,60inputSaleDate:mmddyy10.109USA3-17-19992789.10Amount;elseifLocation='EUR'theninputSaleDate:date9.Holdrecord.Amount:commax8.;run;InputBuffer101USA1-20-19993295.50PDVSALESIDLOCATIONSALEDATEAMOUNT101USA..110......datasales;RawDataFilelengthSalesID$4101USA1-20-19993295.50Location$3;3034EUR30JAN19991876,30infile'raw-data-file';101USA1-30-19992938.00inputSalesID$Location$@;128USA2-5-19992908.74ifLocation='USA'then1345EUR6FEB19993145,60inputSaleDate:mmddyy10.109USA3-17-19992789.10Amount;elseifLocation='EUR'theninputSaleDate:date9.Amount:commax8.;run;InputBuffer101USA1-20-19993295.50PDVSALESIDLOCATIONSALEDATEAMOUNT101USA14264.3295.50.111... 4.2ControllingWhenaRecordLoads4-39datasales;RawDataFilelengthSalesID$4101USA1-20-19993295.50Location$3;3034EUR30JAN19991876,30infile'raw-data-file';101USA1-30-19992938.00inputSalesID$Location$@;128USA2-5-19992908.74ifLocation='USA'then1345EUR6FEB19993145,60inputSaleDate:mmddyy10.109USA3-17-19992789.10Amount;elseifLocation='EUR'thenImplicitreturninputSaleDate:date9.Amount:commax8.;run;InputBufferImplicitoutput101USA1-20-19993295.50PDVSALESIDLOCATIONSALEDATEAMOUNT101USA14264.3295.50.112Writeoutobservationtosales.......MixedRecordTypesPartialLogNOTE:6recordswerereadfromtheinfile'sales.dat'.Theminimumrecordlengthwas24.Themaximumrecordlengthwas26.NOTE:ThedatasetWORK.SALEShas6observationsand4variables.116MixedRecordTypesprocprintdata=salesnoobs;run;PROCPRINTOutputSalesSaleIDLocationDateAmount101USA142643295.503034EUR142741876.30101USA142742938.00128USA142802908.741345EUR142813145.60109USA143202789.10117c04s2d2.sas 4-40Chapter4ReadingandWritingDifferentTypesofDataSubsettingfromaRawDataFileThisscenariousestherawdatafilefromthepreviousexample.101USA1-20-19993295.503034EUR30JAN19991876,30101USA1-30-19992938.00128USA2-5-19992908.741345EUR6FEB19993145,60109USA3-17-19992789.10118DesiredOutputThesalesmanagerwantstoseesalesfortheEuropeanbranchonly.SalesSaleIDLocationDateAmount3034EUR142741876.301345EUR142813145.60119TheSubsettingIFStatementdataeurope;lengthSalesID$4Location$3;infile'raw-data-file';inputSalesID$Location$@;ifLocation='USA'theninputSaleDate:mmddyy10.Amount;elseifLocation='EUR'theninputSaleDate:date9.Amount:commax8.;ifLocation='EUR';run;120 4.2ControllingWhenaRecordLoads4-41TheSubsettingIFStatementThesubsettingIFshouldappearasearlyintheprogramaspossiblebutafterthevariablesusedintheconditionarecalculated.121TheSubsettingIFStatementdataeurope;lengthSalesID$4Location$3;infile'raw-data-file';inputSalesID$Location$@;ifLocation='EUR';inputSaleDate:date9.Amount:commax8.;run;BecausetheprogramreadsonlyEuropeansales,theINPUTstatementforUSAsalesisnotneeded.122Inmanycases,thereisasignificantefficiencysavingswhenyoureadonlypartoftherecord,checkthesubsettingcondition,andthenreadtherestoftherecordiftheconditionismet,asopposedtoreadingtheentirerecordandthencheckingthesubsettingcriteria. 4-42Chapter4ReadingandWritingDifferentTypesofDatadataeurope;RawDataFilelengthSalesID$4101USA1-20-19993295.50Location$3;3034EUR30JAN19991876,30infile'raw-data-file';101USA1-30-19992938.00inputSalesid$Location$@;128USA2-5-19992908.74ifLocation='EUR';1345EUR6FEB19993145,60inputSaleDate:date9.109USA3-17-19992789.10Amount:commax8.;run;InputBuffer101USA1-20-19993295.50PDVSALESIDLOCATIONSALEDATEAMOUNT..125......dataeurope;RawDataFilelengthSalesID$4101USA1-20-19993295.50Location$3;3034EUR30JAN19991876,30infile'raw-data-file';101USA1-30-19992938.00inputSalesid$Location$@;128USA2-5-19992908.74FalseifLocation='EUR';1345EUR6FEB19993145,60inputSaleDate:date9.109USA3-17-19992789.10Amount:commax8.;run;Holdrecord.InputBuffer101USA1-20-19993295.50PDVSALESIDLOCATIONSALEDATEAMOUNT101USA..126... 4.2ControllingWhenaRecordLoads4-43dataeurope;RawDataFilelengthSalesID$4101USA1-20-19993295.50Location$3;3034EUR30JAN19991876,30infile'raw-data-file';101USA1-30-19992938.00inputSalesid$Location$@;128USA2-5-19992908.74ifLocation='EUR';1345EUR6FEB19993145,60inputSaleDate:date9.109USA3-17-19992789.10Amount:commax8.;run;NoimplicitImplicitreturnoutputInputBuffer101USA1-20-19993295.50PDVSALESIDLOCATIONSALEDATEAMOUNT101USA..127......IfanobservationdoesnotmeetthesubsettingIF,•controlreturnstothetopoftheDATAstep•thePDVisreset•anewrecordisread.TheobservationneverreachesthebottomoftheDATAstepandisthereforeneveroutput.dataeurope;RawDataFilelengthSalesID$4101USA1-20-19993295.50Location$3;3034EUR30JAN19991876,30infile'raw-data-file';101USA1-30-19992938.00inputSalesid$Location$@;128USA2-5-19992908.74TrueifLocation='EUR';1345EUR6FEB19993145,60inputSaleDate:date9.109USA3-17-19992789.10Amount:commax8.;run;Holdrecord.InputBuffer3034EUR30JAN19991876,30PDVSALESIDLOCATIONSALEDATEAMOUNT3034EUR..131...... 4-44Chapter4ReadingandWritingDifferentTypesofDatadataeurope;RawDataFilelengthSalesID$4101USA1-20-19993295.50Location$3;3034EUR30JAN19991876,30infile'raw-data-file';101USA1-30-19992938.00inputSalesid$Location$@;128USA2-5-19992908.74ifLocation='EUR';1345EUR6FEB19993145,60inputSaleDate:date9.109USA3-17-19992789.10Amount:commax8.;run;InputBuffer3034EUR30JAN19991876,30PDVSALESIDLOCATIONSALEDATEAMOUNT3034EUR14274.1876.30.132...dataeurope;RawDataFilelengthSalesID$4101USA1-20-19993295.50Location$3;3034EUR30JAN19991876,30infile'raw-data-file';101USA1-30-19992938.00inputSalesid$Location$@;128USA2-5-19992908.74ifLocation='EUR';1345EUR6FEB19993145,60inputSaleDate:date9.109USA3-17-19992789.10Amount:commax8.;run;ImplicitreturnImplicitoutputInputBuffer3034EUR30JAN19991876,30PDVSALESIDLOCATIONSALEDATEAMOUNT3034EUR14274.1876.30.Writeoutobservationtoeurope.133......IfthesubsettingIFconditionistrue,SAScontinuesprocessingthecurrentobservationuntilitreachesthebottomoftheDATAstepandtheimplicitoutput. 4.2ControllingWhenaRecordLoads4-45TheSubsettingIFStatementprocprintdata=europenoobs;run;SalesSaleIDLocationDateAmount3034EUR142741876.301345EUR142813145.60137MultipleObservationsPerRecordArawdatafilecontainseachemployee’sidentificationnumberandthisyear’scontributiontohisorherretirementplan.Eachrecordcontainsinformationformultipleemployees.E009731400E098722003E731502400E456714500E348051980138DesiredOutputTheoutputSASdatasetshouldhaveoneobservationperemployee.EmpIDContribE009731400E098722003E731502400E456714500E348051980139 4-46Chapter4ReadingandWritingDifferentTypesofDataProcessing:WhatIsRequired?E009731400E098722003E731502400ReadforReadforReadforObs.1Obs.2Obs.3ProcessProcessProcessOtherOtherOtherStatementsStatementsStatementsOutputOutputOutput140......TheDoubleTrailing@Thedoubletrailing@holdstherawdatarecordacrossiterationsoftheDATAstepuntilthelinepointermovespasttheendoftheline.INPUTINPUTvar1var2var3…@@var1var2var3…@@;;141Thedoubletrailing@shouldonlybeusedwithlistinput.Ifusedwithcolumnorformattedinput,aninfiniteloopcanresult. 4.2ControllingWhenaRecordLoads4-47TheDoubleTrailing@datawork.retire;lengthEmpID$6;infile'raw-data-file';inputEmpID$Contrib@@;run;Holduntilendofrecord.142......CreatingMultipleObservationsPerRecordPartialLogNOTE:2recordswerereadfromtheinfile'retire.dat'.Theminimumrecordlengthwas35.Themaximumrecordlengthwas36.NOTE:SASwenttoanewlinewhenINPUTstatementreachedpasttheendofaline.NOTE:ThedatasetWORK.RETIREhas5observationsand2variables.The"SASwenttoanewline"messageisexpectedbecausethe@@optionindicatesthatSASshouldreaduntiltheendofeachrecord.143CreatingMultipleObservationsPerRecordprocprintdata=retirenoobs;run;PROCPRINTOutputEmpIDContribE009731400E098722003E731502400E456714500E348051980144c04s2d4.sas 4-48Chapter4ReadingandWritingDifferentTypesofDataTrailing@VersusDoubleTrailing@OptionEffectTrailing@Holdsrawdatarecorduntil1)anINPUTstatementwithnoINPUTvar-1...@;trailing@2)thebottomoftheDATAstep.Doubletrailing@HoldsrawdatarecordsintheinputbufferuntilSASreadspasttheendoftheline.INPUTvar-1...@@;145Thesingletrailing@andthedoubletrailing@aremutuallyexclusive;theycannotandshouldnotbeusedtogether.IftheybothappearinthesameINPUTstatement,thelastoptionspecifiedisused.TheMISSOVERoptionisalsoinvalidwiththedoubletrailing@@. 4.2ControllingWhenaRecordLoads4-49Exercises4.ReadingMultipleRecordsperObservationMedicaldataisstoredintherawdatafileBLOODTYP.Thefirstrecordcontainsthepatient’sidentificationnumberandthepatient’sfirstandlastnames.Thesecondrecordcontainsacodespecifyingthemedicalplan,thepatient’sbloodtype,acodeindicatingwhetherthepatienthasanyallergies,andthenumberofdependantscoveredbythefamily’shealthplan.FirstRecordOrderFieldNotes1IDNumber5-charactercode2LastNameLongestvalueis9characters.3FirstNameLongestvalueis11characters.SecondRecordOrderFieldNotes1PlanType1-charactercode2BloodTypeLongestvalueis3characters.3AllergyCode1-charactercode:Y=Yes,N=No4NumberofDependantsNumericfieldSampleRecordsE1009MORGANGEORGEFO+Y1E1017WELCHDARIUSFAB+N2E1036MOORELESLIESAB+Y1E1037EDWARDSJENNIFERFB-Y1E1038WASHBURNGAYLENB+Y1 4-50Chapter4ReadingandWritingDifferentTypesofDataCreateaSASdatasetnamedwork.medicalthatcontainsthepatient’sidentificationnumber,firstname,lastname,andbloodtype.PartialListingofwork.medicalObsIDLNameFNameBlood1E1009MORGANGEORGEO+2E1017WELCHDARIUSAB+3E1036MOORELESLIEAB+4E1037EDWARDSJENNIFERB-5E1038WASHBURNGAYLEB+6E1050TUTTLETHOMASA+7E1065CHAPMANNEILO+Youdonothavetoreadallofthefields.5.ReadingMixedRecordTypesMedicaldataisstoredintherawdatafileALLERGY.Thefirstsixfieldsarealwaysasfollows:FirstPartofRecordOrderFieldNotes1IDNumber5-charactercode2LastNameLongestvalueis9characters3FirstNameLongestvalueis11characters4PlanType1-charactercode5BloodTypeLongestvalueis3characters6AllergyCode1-charactercode:Y=Yes,N=NoIfthepatienthasanallergy(AllergyCode=Y),thentherestoftherecordisasfollows:7AllergyType2-charactercodeindicatingtypeofallergy8NumberofDependantsNumericfieldIfthepatientdoesnothaveanallergy(AllergyCode=N),thentherestoftherecordisasfollows:7NumberofDependantsNumericfield 4.2ControllingWhenaRecordLoads4-51SampleRecordsE1009MORGANGEORGEFO+YDY1E1017WELCHDARIUSFAB+N2E1036MOORELESLIESAB+YSM1E1037EDWARDSJENNIFERFB-YHF1E1038WASHBURNGAYLENB+YPA1E1050TUTTLETHOMASSA+N2E1065CHAPMANNEILFO+N2UseconditionalinputtocreatetheSASdatasetnamedwork.allergies.PartialListingofwork.allergiesAlgyObsIDLNameFNamePlanBloodAllergyTypeDependants1E1009MORGANGEORGEFO+YDY12E1017WELCHDARIUSFAB+N23E1036MOORELESLIESAB+YSM14E1037EDWARDSJENNIFERFB-YHF15E1038WASHBURNGAYLENB+YPA16E1050TUTTLETHOMASSA+N27E1065CHAPMANNEILFO+N28E1076VENTERRANDALLNA+N19E1094STARRALTONNB+YSF16.SubsettingfromaRawDataFile(Optional)ModifytheDATAstepyouwroteinthepreviousproblemtocreateaSASdatasetnamedwork.allergies2thatcontainsonlypatientswithallergies.PartialListingofwork.allergies2AlgyObsIDLNameFNamePlanBloodAllergyTypeDependants1E1009MORGANGEORGEFO+YDY12E1036MOORELESLIESAB+YSM13E1037EDWARDSJENNIFERFB-YHF14E1038WASHBURNGAYLENB+YPA1 4-52Chapter4ReadingandWritingDifferentTypesofData7.ReadingRawDatawithMultipleObservationsperRecordTherawdatafileTRANSACTcontainsdailybanktransactionsforagivenaccount.Foreachtransaction,thefollowinginformationisstored:OrderFieldNotes1DateofTransactionWritteninDATE9.2TypeofTransactionC=deposit(credit),D=withdrawal(debit)3AmountofTransactionWritteninCOMMA9.SampleRecords03JAN2001C9,25304JAN2001D12,13506JAN2001C10,56210JAN2001D35,95015JAN2001C95121JAN2001C1,22625JAN2001C8628JAN2001C27,50031JAN2001D75,900CreateaSASdatasetnamedwork.transactionsthatcontainsalltransactions.Listingofwork.transactionsObsDateTypeAmount114978C9253214979D12135314981C10562414985D35950514990C951614996C1226715000C86815003C27500915006D75900 4.2ControllingWhenaRecordLoads4-538.CreatingMultipleSASDataSetsfromaSingleRawDataFile(Optional)ModifytheDATAstepyouwroteinExercise7tocreatetwoSASdatasets.Namethefirstdatasetwork.credits;itshouldcontainallthedepositinformation.Nametheseconddatasetwork.debits;itshouldcontainallthewithdrawalinformation.Hint:CreatebothdatasetsinoneDATAstepbylistingthembothintheDATAstatementandusingconditionallogicwithanOUTPUTstatement(showninSection2.2,“WritingtoMultipleDataSets”).Listingofwork.creditsObsDateTypeAmount114978C9253214981C10562314990C951414996C1226515000C86615003C27500Listingofwork.debitsObsDateTypeAmount114979D12135214985D35950315006D759009.SubsettingFromaFixed-ColumnRawDataFile(Optional)TheREADEMPSprogramreadsafixed-columnrawdatafileandoutputsonlythesalesclerks.datasalclrks;infile'raw-data-file';input@1Division$20.@21HireDatemmddyy10.@31Salarydollar10.2@41LastName$15.@56FirstName$15.@71Country$15.@86Location$10.@96IdNumber$6.@112JobCode$6.;ifjobcode='SALCLK';run;procprintdata=salclrksnoobs;title'EmployeeInformationforSalesclerks';run; 4-54Chapter4ReadingandWritingDifferentTypesofDataa.IncludetheREADEMPSprograminyourProgramEditor.Submittheprogramandverifytheoutput.PartialOutputEmployeeInformationforSalesclerksHireDivisionDateSalaryLastNameFirstNameSALES810729000DANZINMATHIASSALES1249225000HALLDREMAA.SALES920541000BOOZERKRAIGE.SALES829027000LIEBLEIIIJANSALES1265817000TOUGERARTHURSALES783838000COLEJONIL.SALES1006431000FINNBETTYL.SALES862044000KATZPATRICIAB.SALES909738000POTTSPAULSALES1249244000BENTZMARIEIdJobCountryLocationNumberCodeBELGIUMBRUSSELSE0019SALCLKUSACARYE0044SALCLKUSACARYE0058SALCLKUSAORLANDOE0093SALCLKUSACARYE0104SALCLKUSACARYE0113SALCLKUSABEDMINSTERE0149SALCLKUSAKANSASCITE0171SALCLKUSACARYE0199SALCLKFRANCEPARISE0229SALCLKb.Modifytheprogramsothatitproducestheoutputdatasetasefficientlyaspossible.Fixedcolumnfieldscanbereadinanyorder.10.UsingtheAbsoluteLinePointerControl(Optional)TherawdatafileEMPTWOhastheemployee’ssalaryinformationanddateofhireinthefirstline,andtheemployee’sidentificationnumber,firstname,andlastnameonthesecondline.Record1OrderFieldNotes1DivisionLongestvalueis20characters2HireDateWritteninMMDDYY10.3SalaryStandardnumericfield 4.2ControllingWhenaRecordLoads4-55Record2OrderFieldNotes1IDNumber6bytefield2LastNameLongestvalueis15characters.3FirstNameLongestvalueis15characters.SampleRecordsFLIGHTOPTS,03/11/1992,25000E0001,MILLS,DOROTHYEFINANCE,12/19/1983,27000E0002,BOWER,EILEENA.HR&FACIL,03/12/1985,120000E0003,READING,TONYR.HR&FACIL,10/16/1989,42000ReadtheEMPTWOrawdatafiletocreatetheSASdatasetwork.empinfo.Usetheabsolutelinepointertocontrolthedefaultorderofthefields,sothatthevariablesintheSASdatasetareinthefollowingorder:1)IdentificationNumber2)LastName3)FirstName4)Division5)HireDate6)SalaryListingofwork.empinfoIdHireObsNumberLastNameFirstNameDivisionDateSalary1E0001MILLSDOROTHYEFLIGHTOPTS11758250002E0002BOWEREILEENA.FINANCE8753270003E0003READINGTONYR.HR&FACIL92021200004E0004JUDDCAROLA.HR&FACIL10881420005E0005WONSIDHANNAAIRPORTOPTS8023190006E0006ANDERSONCHRISTOPHERSALES11439310007E0007MASSENGILLANNETTEM.FLIGHTOPTS8440290008E0008BADINEDAVIDCORPORATE11733850009E0009DEMENTCHARLESFINANCE98873400010E0010FOSKEYJERED.AIRPORTOPTS1128429000Formoreinformationontheabsolutelinepointercontrol,seethenoteontherelativelinepointercontrol.Forhelponcontrollingvariablelengthwhilepreservingtheirorder,seethenotesonusinginformatsontheINPUTstatementinChapter4,Section1. 4-56Chapter4ReadingandWritingDifferentTypesofData4.3ReadingHierarchicalRawDataFilesObjectivesReadahierarchicalfileandcreateoneobservationperdetailrecord.Readahierarchicalfileandcreateoneobservationperheaderrecord.148ProcessingHierarchicalFilesManyfilesarehierarchicalinstructure,consistingofaheaderrecordoneormorerelateddetailrecords.HeaderTypically,eachrecordcontainsafieldDetailthatidentifieswhetheritisaheaderDetailrecordoradetailrecord.HeaderHeaderDetailHeaderDetailDetail149 4.3ReadingHierarchicalRawDataFiles4-57ProcessingHierarchicalFilesYoucanreadahierarchicalfileintoaSASdatasetbycreatingoneobservationperdetailrecordandstoringtheheaderinformationaspartofeachobservation.HierarchicalFileSASDataSetHeader1HeaderDetailDetail1VariablesVariablesDetail2Detail3Header1Detail1Header2Header1Detail2Detail1Header1Detail3Header3Header2Detail1Detail1Header3Detail1Detail2Header3Detail2150ProcessingHierarchicalFilesYoucanalsocreateoneobservationperheaderrecordandstoretheinformationfromdetailrecordsinsummaryvariables.HierarchicalFileSASDataSetHeader1HeaderSummaryDetail1VariablesVariablesDetail2Detail3Header1Summary1Header2Header2Summary2Detail1Header3Summary3Header3Detail1Detail2151CreatingOneObservationPerDetailTherawdatafileE:Adams:SusanDEPENDANTShasaD:Michael:CD:Lindsay:CheaderrecordcontainingE:Porter:DavidthenameoftheemployeeD:Susan:SandadetailrecordforE:Lewis:DorianD.eachdependantonD:Richard:Ctheemployee’shealthE:Dansky:Ianinsurance.E:Nicholls:JamesD:Roberta:CE:Slaydon:MarlaD:John:S152 4-58Chapter4ReadingandWritingDifferentTypesofDataDesiredOutputPersonnelwantsalistofallthedependantsandthenameoftheassociatedemployee.EmpLNameEmpFNameDepNameRelationAdamsSusanMichaelCAdamsSusanLindsayCPorterDavidSusanSLewisDorianD.RichardCNichollsJamesRobertaCSlaydonMarlaJohnS153BecausePersonnelisinterestedonlyinthedependants,IanDansky,whohasnodependants,willnotappearintheoutputdataset.AHierarchicalFileNotalltherecordsareE:Adams:Susanthesame.D:Michael:CD:Lindsay:CThefieldsareseparatedE:Porter:Davidbycolons.D:Susan:SThereisafieldindicatingE:Lewis:DorianD.whethertherecordisaD:Richard:Cheaderoradetailrecord.E:Dansky:IanE:Nicholls:JamesD:Roberta:CE:Slaydon:MarlaD:John:S154 4.3ReadingHierarchicalRawDataFiles4-59HowtoReadtheRawDatainputType$@;ifType='E'theninputEmpLName$EmpFName$;elseinputDepName$Relation$;155HowtoOutputOnlytheDependantsinputType$@;ifType='E'theninputEmpLName$EmpFName$;elsedo;inputDepName$Relation$;output;end;156datadependants(drop=Type);E:Adams:SusanlengthType$1EmpLNameEmpFNameD:Michael:CDepName$20Relation$1;D:Lindsay:Cinfile'raw-data-file'dlm=':';E:Porter:DavidD:Susan:SinputType$@;E:Lewis:DorianD.ifType='E'thenD:Richard:CinputEmpLName$EmpFName$;E:Dansky:Ianelsedo;E:Nicholls:JamesD:Roberta:CinputDepName$Relation$;E:Slaydon:Marlaoutput;D:John:Send;Holdrecord.run;InputBufferE:Adams:SusanDTYPEEMPLNAMEEMPFNAMEDEPNAMERELATIONE160...... 4-60Chapter4ReadingandWritingDifferentTypesofDatadatadependants(drop=Type);E:Adams:SusanlengthType$1EmpLNameEmpFNameD:Michael:CDepName$20Relation$1;D:Lindsay:Cinfile'raw-data-file'dlm=':'E:Porter:DavidD:Susan:SinputType$@;E:Lewis:DorianD.ifType='E'thenD:Richard:CinputEmpLName$EmpFName$;E:Dansky:Ianelsedo;E:Nicholls:JamesD:Roberta:CinputDepName$Relation$;E:Slaydon:Marlaoutput;D:John:Send;run;InputBufferImplicitreturnNoimplicitE:Adams:SusanoutputDTYPEEMPLNAMEEMPFNAMEDEPNAMERELATIONEAdamsSusan162......datadependants(drop=Type);E:Adams:SusanlengthType$1EmpLNameEmpFNameD:Michael:CDepName$20Relation$1;D:Lindsay:Cinfile'raw-data-file'dlm=':';E:Porter:DavidD:Susan:SinputType$@;E:Lewis:DorianD.ifType='E'thenD:Richard:CinputEmpLName$EmpFName$;E:Dansky:Ianelsedo;E:Nicholls:JamesD:Roberta:CinputDepName$Relation$;E:Slaydon:Marlaoutput;D:John:Send;run;InputBufferE:Adams:SusanReinitializePDV.DTYPEEMPLNAMEEMPFNAMEDEPNAMERELATION166......EmpLNameandEmpFNamearereinitializedatthetopoftheDATAstep.Inthiscase,thatisnotdesirable. 4.3ReadingHierarchicalRawDataFiles4-61datadependants(drop=Type);E:Adams:SusanlengthType$1EmpLNameEmpFNameD:Michael:CDepName$20Relation$1;D:Lindsay:Cinfile'raw-data-file'dlm=':';E:Porter:DavidD:Susan:SinputType$@;E:Lewis:DorianD.ifType='E'thenD:Richard:CinputEmpLName$EmpFName$;E:Dansky:Ianelsedo;E:Nicholls:JamesD:Roberta:CinputDepName$Relation$;E:Slaydon:Marlaoutput;D:John:Send;Holdrecord.run;InputBufferD:Michael:CDTYPEEMPLNAMEEMPFNAMEDEPNAMERELATIOND169......datadependants(drop=Type);E:Adams:SusanlengthType$1EmpLNameEmpFNameD:Michael:CDepName$20Relation$1;D:Lindsay:Cinfile'raw-data-file'dlm=':';E:Porter:DavidD:Susan:SinputType$@;E:Lewis:DorianD.ifType='E'thenD:Richard:CinputEmpLName$EmpFName$;E:Dansky:Ianelsedo;E:Nicholls:JamesD:Roberta:CinputDepName$Relation$;E:Slaydon:Marlaoutput;D:John:Send;run;ExplicitInputBufferoutputD:Michael:CDRTYPEEMPLNAMEEMPFNAMEDEPNAMERELATIONDMichaelC172Writeoutobservationtodependants....... 4-62Chapter4ReadingandWritingDifferentTypesofDataUndesirableOutputEmpEmpLNameFNameDepNameRelationMichaelCLindsayCSusanSRichardCRobertaCJohnS174BecauseSASonlyoutputswhenitreadsadetailrecord,thevaluesofEmpFNameandEmpLNamearemissing.TheRETAINStatement(Review)GeneralformoftheRETAINstatement:RETAINRETAINvariable-namevariable-name<;>;TheRETAINstatementpreventsSASfromreinitializingthevaluesofnewvariablesatthetopoftheDATAstep.Thismeansthatvaluesfrompreviousrecordsareavailableforprocessing.175Bydefault,variablesreferencedintheRETAINstatementaresettomissingbeforethefirstiterationoftheDATAstep.Tochangethis,youcanspecifyaninitialvalueafterthevariable’sname.Formoreinformation,seeChapter3,“SummarizingData,”orSASLanguageReference:Dictionary.VariablesreferencedwiththeRETAINstatementareintheoutputdatasetonlyiftheyarereferencedelsewhereintheDATAsteporassignedinitialvalues. 4.3ReadingHierarchicalRawDataFiles4-63HoldEmpLNameandEmpFNamedatadependants(drop=Type);lengthType$1EmpLNameEmpFNameDepName$20Relation$1;retainEmpLNameEmpFName;infile'raw-data-file'dlm=':';inputType$@;ifType='E'theninputEmpLName$EmpFName$;elsedo;inputDepName$Relation$;output;end;run;176Compiledatadependants(drop=Type);lengthType$1EmpLNameEmpFNameE:Adams:SusanD:Michael:CDepName$20Relation$1;D:Lindsay:CretainEmpLNameEmpFName;E:Porter:Davidinfile'raw-data-file'dlm=':';D:Susan:SinputType$@;E:Lewis:DorianD.D:Richard:CifType='E'thenE:Dansky:IaninputEmpLName$EmpFName$;E:Nicholls:Jameselsedo;D:Roberta:CinputDepName$Relation$;E:Slaydon:MarlaD:John:Soutput;end;run;InputBufferDRRRTYPEEMPLNAMEEMPFNAMEDEPNAMERELATION177...... 4-64Chapter4ReadingandWritingDifferentTypesofDatadatadependants(drop=Type);lengthType$1EmpLNameEmpFNameE:Adams:SusanD:Michael:CDepName$20Relation$1;D:Lindsay:CretainEmpLNameEmpFName;E:Porter:Davidinfile'raw-data-file'dlm=':';D:Susan:SinputType$@;E:Lewis:DorianD.D:Richard:CifType='E'thenE:Dansky:IaninputEmpLName$EmpFName$;E:Nicholls:Jameselsedo;D:Roberta:CinputDepName$Relation$;E:Slaydon:MarlaD:John:Soutput;Holdrecord.end;run;InputBufferE:Adams:SusanDRRRTYPEEMPLNAMEEMPFNAMEDEPNAMERELATIONE180......Aswiththeconditionalinputexampleintheprevioussection,thetrailing@holdstherecordwhileSASchecksthecondition.datadependants(drop=Type);lengthType$1EmpLNameEmpFNameE:Adams:SusanD:Michael:CDepName$20Relation$1;D:Lindsay:CretainEmpLNameEmpFName;E:Porter:Davidinfile'raw-data-file'dlm=':';D:Susan:SinputType$@;E:Lewis:DorianD.D:Richard:CifType='E'thenE:Dansky:IaninputEmpLName$EmpFName$;E:Nicholls:Jameselsedo;D:Roberta:CinputDepName$Relation$;E:Slaydon:MarlaD:John:Soutput;end;run;InputBufferImplicitreturnNoimplicitE:Adams:SusanoutputDRRRTYPEEMPLNAMEEMPFNAMEDEPNAMERELATIONEAdamsSusan182......BecauseoftheexplicitoutputintheDOgroup,SASoutputsanobservationonlywhenitencountersadetailrecord. 4.3ReadingHierarchicalRawDataFiles4-65datadependants(drop=Type);lengthType$1EmpLNameEmpFNameE:Adams:SusanD:Michael:CDepName$20Relation$1;D:Lindsay:CretainEmpLNameEmpFName;E:Porter:Davidinfile'raw-data-file'dlm=':';D:Susan:SinputType$@;E:Lewis:DorianD.D:Richard:CifType='E'thenE:Dansky:IaninputEmpLName$EmpFName$;E:Nicholls:Jameselsedo;D:Roberta:CinputDepName$Relation$;E:Slaydon:MarlaD:John:Soutput;end;run;InputBufferE:Adams:SusanReinitializePDV.DRRRTYPEEMPLNAMEEMPFNAMEDEPNAMERELATIONAdamsSusan186......BecauseoftheRETAINstatement,EmpFNameandEmpLNamearenotreinitialized.datadependants(drop=Type);lengthType$1EmpLNameEmpFNameE:Adams:SusanD:Michael:CDepName$20Relation$1;D:Lindsay:CretainEmpLNameEmpFName;E:Porter:Davidinfile'raw-data-file'dlm=':';D:Susan:SinputType$@;E:Lewis:DorianD.D:Richard:CifType='E'thenE:Dansky:IaninputEmpLName$EmpFName$;E:Nicholls:Jameselsedo;D:Roberta:CinputDepName$Relation$;E:Slaydon:Marlaoutput;D:John:SHoldrecord.end;run;InputBufferD:Michael:CDRRTYPEEMPLNAMEEMPFNAMEDEPNAMERELATIONDAdamsSusan189......Becausethisisadetailrecord,SASexecutestheDOgroup. 4-66Chapter4ReadingandWritingDifferentTypesofDatadatadependants(drop=Type);lengthType$1EmpLNameEmpFNameE:Adams:SusanD:Michael:CDepName$20Relation$1;D:Lindsay:CretainEmpLNameEmpFName;E:Porter:Davidinfile'raw-data-file'dlm=':';D:Susan:SinputType$@;E:Lewis:DorianD.D:Richard:CifType='E'thenE:Dansky:IaninputEmpLName$EmpFName$;E:Nicholls:Jameselsedo;D:Roberta:CinputDepName$Relation$;E:Slaydon:MarlaD:John:Soutput;end;run;ExplicitInputBufferImplicitreturnoutputD:Michael:CDRRRTYPEEMPLNAMEEMPFNAMEDEPNAMERELATIONDAdamsSusanMichaelC191Writeoutobservationtodependants.......CreatingOneObservationPerDetailprocprintdata=work.dependantsnoobs;run;PROCPRINTOutputEmpLNameEmpFNameDepNameRelationAdamsSusanMichaelCAdamsSusanLindsayCPorterDavidSusanSLewisDorianD.RichardCNichollsJamesRobertaCSlaydonMarlaJohnS195c04s3d1.sasCreateOneObservationPerHeaderRecordE:E01442EmployeeinsuranceisD:Michael:Cfreefortheemployees.D:Lindsay:CEachemployeepays$50E:E00705permonthforaspouse’sD:Susan:Sinsurance.E:E01577Eachemployeepays$25D:Richard:Cpermonthforachild’sE:E00997insurance.E:E00955D:Roberta:CE:E00224D:John:S196 4.3ReadingHierarchicalRawDataFiles4-67DesiredOutputPersonnelwantsalistofallemployeesandtheirmonthlypayrolldeductionsforinsurance.IDDeductE0144250E0070550E0157725E009970E0095525E0022450197CalculatingtheValueofDeductE:E01442ThevaluesofDeductD:Michael:CchangeaccordingtotheD:Lindsay:CtypeofrecordreadE:E00705D:Susan:SvalueofRelationE:E01577whenType='D'.D:Richard:CE:E00997E:E00955D:Roberta:CE:E00224D:John:S198RetainingIDValuesofIDandDeductmustbeheldacrossiterationsoftheDATAstep.retainID;IDmustberetainedwithaRETAINstatement.Deductiscreatedwithasumstatement,whichisautomaticallyretained.199 4-68Chapter4ReadingandWritingDifferentTypesofDataWhentoOutput?E:E01442D:Michael:CD:Lindsay:CE:E00705EndObservation1D:Susan:SE:E01577EndObservation2D:Richard:CE:E00997EndObservation3E:E00955EndObservation4D:Roberta:CE:E00224EndObservation5D:John:SEndObservation6200......WhenSASLoadsaTypeERecord1.OutputwhatiscurrentlyinthePDV(unlessthisisthefirsttimethroughtheDATAstep).2.Readthenextemployee’sidentificationnumber.3.ResetDeductto0.ifType='E'thendo;if_N_>1thenoutput;inputID$;Deduct=0;end;201WhenSASLoadsaTypeDRecord1.Readthedependant’snameandrelationship.2.Checktherelationship.3.IncrementDeductappropriately.elsedo;inputDepName$Relation$;ifRelation='C'thenDeduct+25;elseDeduct+50;end;WhyisDepNameread?202 4.3ReadingHierarchicalRawDataFiles4-69datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;retainID;infile'raw-data-file'dlm=':';inputType$@;ifType='E'thendo;if_N_>1thenoutput;inputID$;Deduct=0;end;elsedo;inputDepName$Relation$;ifRelation='C'thenDeduct+25;elseDeduct+50;end;run;203WhatAbouttheLastRecord?E:E01442D:Michael:CD:Lindsay:CE:E00705D:Susan:SE:E01577D:Richard:CE:E00997E:E00955D:Roberta:CE:E00224D:John:SNoimplicitoutput204......InthecurrentDATAstep,SASonlyproducesanobservationwhenitreadsarecordwithType='E'.Thereisnoemployeerecordafterthelastrecordtosignalanoutput. 4-70Chapter4ReadingandWritingDifferentTypesofDataTheEND=OptionGeneralformoftheEND=option:INFILEINFILE'file-name'file-name'END='END=variable-namevariable-name;;wherevariable-nameisanyvalidSASvariablename.TheEND=optioncreatesavariablethathasthevalue1ifitisthelastrecordoftheinputfile0otherwise.VariablescreatedwithEND=areautomaticallydropped.205datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;retainID;infile'raw-data-file'dlm=':'end=LastRec;inputType$@;ifType='E'thendo;if_N_>1thenoutput;inputID$;Deduct=0;end;elsedo;inputDepName$Relation$;ifRelation='C'thenDeduct+25;elseDeduct+50;end;ifLastRecthenoutput;206run;SASoutputsonlywhenitencounters•aheaderrecordthatisnotthefirstintherawdatafile•thelastrecordintherawdatafile.BecausethereisanexplicitoutputintheDATAstep,thestatementthatoutputsthelastrecordmustbeanexplicitoutput,notasubsettingIF. 4.3ReadingHierarchicalRawDataFiles4-71Executedatawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:CinputType$@;inputType$@;dlm=':'end=LastRec;E:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTREC010208......datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;E:E00705TrueinputType$@;ifType='E'thendo;ifType='E'thendo;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;E:E01442DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECE010211...... 4-72Chapter4ReadingandWritingDifferentTypesofDatadatawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;FalseE:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;if_N_>1thenoutput;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;E:E01442DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECE010212......Youdonotwanttooutputthefirstheaderrecordbeforereadingallthedetailinformation.datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;E:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;inputID$;inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;E:E01442DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECEE01442010213...... 4.3ReadingHierarchicalRawDataFiles4-73datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;E:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;Deduct=0;Deduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;E:E01442DRDDRDeduct=0DDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECEE01442010214......datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;E:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;end;end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;E:E01442DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECEE01442010215......datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;E:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;Falseend;ifLastRecthenoutput;ifLastRecthenoutput;ifLastRecthenoutput;InputBufferrun;E:E01442DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECEE01442010216...... 4-74Chapter4ReadingandWritingDifferentTypesofDatadatawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;E:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;ImplicitreturnE:E01442DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECEE01442010217...SASdoesnotoutputtheinformation,buttheRETAINflagsholditinthePDV.datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;E:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;E:E01442ReinitializePDV.DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECE01442020218...... 4.3ReadingHierarchicalRawDataFiles4-75datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:CinputType$@;inputType$@;dlm=':'end=LastRec;E:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;E:E01442DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECE01442020219......datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;E:E00705inputType$@;ifType='E'thendo;ifType='E'thendo;D:Susan:SifType='E'thendo;Falseif_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;D:Michael:CDRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECDE01442020222......datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;E:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;inputDepName$Relation$;inputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;D:Michael:CDRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECDE01442MichaelC020223...... 4-76Chapter4ReadingandWritingDifferentTypesofDatadatawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;E:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;TrueE:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;ifRelation='C'thenDeduct+25;ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;D:Michael:CDRDDR0+25DDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECDE01442MichaelC25020224......datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;E:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;ImplicitreturnD:Michael:CDRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECDE01442MichaelC2520226...SAScontinuesreadingthedetailrecordsassociatedwiththefirstheaderuntilitreachesthenextheaderrecord. 4.3ReadingHierarchicalRawDataFiles4-77datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;E:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;D:Michael:CReinitializePDV.DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECE014422530227......datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:CinputType$@;inputType$@;dlm=':'end=LastRec;E:E00705inputType$@;D:Susan:SifType='E'thendo;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;D:Michael:CDRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECE014422530228......datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;TrueinputType$@;E:E00705ifType='E'thendo;ifType='E'thendo;ifType='E'thendo;D:Susan:Sif_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;E:E00705DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECEE014425040240...... 4-78Chapter4ReadingandWritingDifferentTypesofDatadatawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'dlm=':'end=LastRec;TrueD:Lindsay:CinputType$@;E:E00705ifType='E'thendo;D:Susan:Sif_N_>1thenoutput;if_N_>1thenoutput;if_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;ExplicitoutputE:E00705DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECEE014425040242Writeoutobservationtoinsurance.......WhenSASencountersthesecondheader,itoutputstheaccumulateddetailinformationandtheappropriateheaderinformation.datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;inputType$@;E:E00705ifType='E'thendo;D:Susan:Sif_N_>1thenoutput;E:E01577inputID$;inputID$;inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;E:E00705DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECEE00705E014425040243......Aftertheinformationforthelastemployeeisoutput,SASbeginstoreadheaderinformationforthenextemployee. 4.3ReadingHierarchicalRawDataFiles4-79datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;inputType$@;E:E00705ifType='E'thendo;D:Susan:Sif_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;Deduct=0;Deduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;E:E00705DRDDRDeduct=0DDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECEE00705E0144250040244......Deductmustberesetwitheachnewemployeeheaderthatisread.datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;inputType$@;E:E00705ifType='E'thendo;D:Susan:Sif_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;end;end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;E:E00705DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECEE00705E0144250040245...... 4-80Chapter4ReadingandWritingDifferentTypesofDatadatawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;inputType$@;E:E00705ifType='E'thendo;D:Susan:Sif_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;Falseend;ifLastRecthenoutput;ifLastRecthenoutput;ifLastRecthenoutput;InputBufferrun;E:E00705DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECEE00705E0144250040246......datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;inputType$@;E:E00705ifType='E'thendo;D:Susan:Sif_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;ContinueprocessingE:E00705untillastrecord.DRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECEE007050060249......SAScontinuesprocessingalloftheemployeerecordsthiswayuntilitreachesthelastrecordinthedatafile. 4.3ReadingHierarchicalRawDataFiles4-81datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;inputType$@;inputType$@;inputType$@;E:E00705ifType='E'thendo;D:Susan:Sif_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;D:John:SDRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECE00224E014420121251......Whenthelastrecordintherawdatafileisread,thevalueofLastRec,whichiscreatedwiththeEND=option,changesto1.datawork.insurance(drop=TypeDepNameRelation);lengthType$1ID$6DepName$20Relation$1;E:E01442retainID;D:Michael:Cinfile'raw-data-file'D:Lindsay:Cdlm=':'end=LastRec;inputType$@;E:E00705ifType='E'thendo;D:Susan:Sif_N_>1thenoutput;E:E01577inputID$;D:Richard:CDeduct=0;E:E00997end;E:E00955elsedo;D:Roberta:CinputDepName$Relation$;E:E00224ifRelation='C'thenDeduct+25;D:John:SelseDeduct+50;end;ifLastRecthenoutput;InputBufferrun;ExplicitD:John:SoutputDRDDRDDTYPEIDDEPNAMERELATIONDEDUCT_N_LASTRECDE00224JohnS2550121258Writeoutobservationtoinsurance.......TheconditionifLastRec(ifLastRecnotequalto0andnotequaltomissing)istrue.Theexplicitoutputisexecuted,andthelastemployee'sinformationiswrittentotheSASdataset. 4-82Chapter4ReadingandWritingDifferentTypesofDataCreatingOneObservationPerHeaderprocprintdata=insurancenoobs;run;PROCPRINTOutputIDDeductE0144250E0070550E0157725E009970E0095525E0022450260c04s3d2.sas 4.3ReadingHierarchicalRawDataFiles4-83SummarizingaHierarchicalFilewithTwoDATASteps(Self-Study)File:c04s3d3.sasItispossibletoreadahierarchicalrawdatafileusingtwoDATAstepsinsteadofthemethodshownabove.Althoughlessefficient,thismethodtendstobeeasiertocode,maintain,anddebug.Step1:ReadthehierarchicalrawdatafileintoaSASdatasetanduseconditionalinput.dataHierStep1;dropDepName;/*WehavetoreadDepName*//*becausethisislistinput,*//*butwedonotneedittosummarize*/lengthType$1ID$6DepName$20Relation$1;retainID;infile'raw-data-file'dlm=':';inputType$@;ifType='E'theninputID$;elseinputDepName$Relation$;run;procprintdata=hierstep1;title'FirstStepinSummarizingaHierarchicalFile';run;FirstStepinSummarizingaHierarchicalFileObsTypeIDRelation1EE014422DE01442C3DE01442C4EE007055DE00705S6EE015777DE01577C8EE009979EE0095510DE00955C11EE0022412DE00224S 4-84Chapter4ReadingandWritingDifferentTypesofDataStep2:TheresultingdatasetisgroupedbyID,thoughtheIDsmightnotbeinorder.YoucanusethetechniquesdescribedinChapter3andtheNOTSORTEDoptionontheBYstatementtosummarizethisdatasetbyID.dataHierstep2;dropTypeRelation;setHierstep1;byIDnotsorted;/*TheNOTSORTEDoptionindicatesthatthe*//*dataisgroupedbytheBYvariablebutthat*//*thevaluesarenotnecessarily*//*insortorder.ThiscreatesFirst.andLast.*/ifFirst.IDthenDeduct=0;ifRelation='C'thenDeduct+25;elseifRelation='S'thenDeduct+50;/*MustmakesureRelation='S'andnotmissing*/ifLast.ID;run;DeductionsforAllEmployeesObsIDDeduct1E01442502E00705503E01577254E0099705E00955256E0022450 4.3ReadingHierarchicalRawDataFiles4-85Exercises11.ReadingaHierarchicalRawDataFileandCreatingOneObservationperDetailRecordTherawdatafileSALARIESishierarchical.Theheaderrecordhastheemployee’sidentificationnumber,firstname,lastname,andthedateheorshewashired.Thedetailrecordshavetheemployee’ssalaryforeachyearthatheorshewasemployedbythecompany.HeaderRecordsOrderFieldNotes1RecordTypeE=Headerrecord,S=Detailrecord2EmployeeIDNumber6-charactercode3FirstNameLongestvalueis8characters4LastNameLongestvalueis8characters5HireDateWritteninDATE9.DetailRecordsOrderFieldNotes1RecordTypeE=Headerrecord,S=Detailrecord2SalaryYear4-digityear3SalaryWritteninCOMMA9.SampleRecordsEE1232JOHNSMITH15OCT1999S199951,684S200056,180S200161,065EE2341ALICEJONES01JUN1997S199765,684S199871,396S199977,604S200084,353S200191,688CreatetheSASdatasetwork.salariesthatcontainsthevariablesID,FName,LName,SalYear,andSalary.Thereshouldbeoneobservationforeachyearthattheemployeeworked. 4-86Chapter4ReadingandWritingDifferentTypesofDataPartialListingofwork.salariesYearlySalariesThrough2001SalObsIDLNameFNameYearSalary1E1232SMITHJOHN1999516842E1232SMITHJOHN2000561803E1232SMITHJOHN2001610654E2341JONESALICE1997656845E2341JONESALICE1998713966E2341JONESALICE1999776047E2341JONESALICE2000843538E2341JONESALICE20019168812.ReadingaHierarchicalRawDataFileandCreatingOneObservationperHeaderRecord(Optional)UsingthesamerawdatafileasinExercise11,createaSASdatasetnamedwork.currentwiththevariablesID,LName,FName,HireDate,andSalary.Thereshouldbeoneobservationforeachemployee,andthevalueofSalaryshouldbeequaltothemostrecentyear’ssalary.Listingofwork.currentSalariesasof2001HireObsIDLNameFNameDateSalary1E1232SMITHJOHN14532610652E2341JONESALICE13666916883E3452MOORELES12352326394E6781LEEJENNIFER11947283055E8321LONGGAYLE13479404406E1052GREENTHOMAS13572394617E1062FOREMANNEIL9991414638E8172THOMPSONRANDY14615406509E1091MCKINSEYSTARR115544095010E9992DALTONRICHARD111414045513.ReadingaHierarchicalFileUsingTwoDATASteps(Optional)UsingthesamerawdatafileasinExercise12,createaSASdatasetnamedwork.twostepandusethetwo-stepprocessdescribedintheself-studysection.Youshouldgetthesameoutputasinthepreviousexercise. 4.3ReadingHierarchicalRawDataFiles4-8714.ReadingaHierarchicalFileWithoutanExplicitIdentifyingField(Optional)TherawdatafileBSTONEScontainsaheaderrecordidentifyingthemonth,plusfiveormoredetailrecordsthatidentifydifferentbirthstonesassociatedwiththatmonth.HeaderRecordFieldNotesMonthLongestvalueis9bytes.Detail1FieldNotesModernBirthstoneLongestvalueis12bytes.Detail2FieldNotesTraditionalBirthstoneLongestvalueis12bytes.Detail3FieldNotesMysticalBirthstoneLongestvalueis11bytes.Detail4FieldNotesAyurvedicBirthstoneLongestvalueis10bytes.Detail5-7FieldNotesOtherBirthstoneLongestvalueis15bytes. 4-88Chapter4ReadingandWritingDifferentTypesofDataSampleRecordsJanuaryGarnetGarnetEmeraldGarnetRoseQuartzFebruaryAmethystAmethystBloodstoneAmethystOnyxMoonstoneMarchAquamarineBloodstoneJadeBloodstoneRockCrystalNotallrecordshavemorethanone“Other”birthstone.Forexample,Januaryhasonlyonebirthstoneclassifiedas“Other”;Februaryhastwo,andOctoberhasthree.AyurvedicbirthstonesarethebirthstonesproscribedbytraditionalIndianmedicaltechniques. 4.3ReadingHierarchicalRawDataFiles4-89Usetherawdatafiletocreatework.birthstones,whichhasoneobservationforeachmonth,andonevariableforeachtypeofbirthstone.Listingofwork.birthstonesVariousBirthstonesforEachMonthObsMonthModernTraditionalMysticalAyurvedic1JanuaryGarnetGarnetEmeraldGarnet2FebruaryAmethystAmethystBloodstoneAmethyst3MarchAquamarineBloodstoneJadeBloodstone4AprilDiamondDiamondOpalDiamond5MayEmeraldEmeraldSapphireAgate6JunePearlAlexandriteMoonstonePearl7JulyRubyRubyRubyRuby8AugustPeridotSardonyxDiamondSapphire9SeptemberSapphireSapphireAgateMoonstone10OctoberOpalTourmalineJasperOpal11NovemberYellowTopazCitrinePearlTopaz12DecemberBlueTopazZirconOnyxRubyObsOther1Other2Other31RoseQuartz2OnyxMoonstone3RockCrystal4QuartzWhiteSapphire5ChrysopraseBeryl6OpalMoonstone7Carnelian8Jade9LapisLazuliDiamondChrsolite10PinkTourmalineZirconAquamarine11Diamond12TurquoiseLapisLazuli 4-90Chapter4ReadingandWritingDifferentTypesofData4.4SolutionstoExercises1.ReadingNonstandardData/*Viewrawdatafilebeforewritingcode*/procfslistfileref='raw-data-file';run;datastates;infile'raw-data-file'dlm='!';/*SetdelimiterwithDLM=*/lengthState$16;inputState$Population:comma9.SizeEnterDate:date9.;/*Usecolonmodifierandinformattoreadnon-standardfields*/run;procprintdata=states;title'StateNamesandFacts';run;2.UsingINFILEStatementOptionstoChangeDefaults/*Viewrawdatafilebeforewritingcode*/procfslistfileref='raw-data-file';run;dataaromas;lengthCondition$11Cure1Cure2Cure3$11;infile'raw-data-file'dsddlm=''missover;/*DSDoptiondealswithembeddeddelimiters.DLM=changesdelimiterbacktoaspace.MISSOVERpreventsSASfromgoingtoanewrecordwhereCure2andCure3aremissing.*/inputCondition$Cure1$Cure2$Cure3$;run;procprintdata=aromas;title'AromatherapyDataSet';run; 4.4SolutionstoExercises4-913.ReadingaFixed-ColumnRawDataFilewithVariableLengthRecords(Optional)Theproblemwiththisrawdatafileisthatnotalltherecordsarethesamelength.Youcanverifythisbylookingatitspropertiesoropeningitinatexteditor.TheTRUNCOVERorPADoptionwouldbeequallyeffectiveinthiscase./*Viewrawdatafilebeforewritingcode*/procfslistfileref='raw-data-file';run;/*ThePADoptionhandlestheproblembyadding*//*spacestoshorterrecords.*/dataaromasf;infile'raw-data-file'pad;input@1Condition$10.@11Cure1$11.@22Cure2$11.@33Cure3$11.;run;procprintdata=aromasf;title'ResultsfromPADOption';run;/*TheTRUNCOVERoptiontellsSASnottoreadfrom*//*thenextlinewhenitrunsoutofdata,butto*//*assignwhateverithasreadtothevariable.*/dataaromasf;infile'raw-data-file'truncover;input@1Condition$10.@11Cure1$11.@22Cure2$11.@33Cure3$11.;run;procprintdata=aromasf;title'ResultsfromTRUNCOVEROption';run;/*TheMISSOVERoptiontellsSAStosetincomplete*//*fieldstomissing.Therefore,anytimeCure3is*//*lessthan11characters,*//*SASsetstheentirevariabletomissing*//*THISISINCORRECTFORTHISDATAFILE.*/dataaromasf;infile'raw-data-file'missover;input@1Condition$10.@11Cure1$11.@22Cure2$11.@33Cure3$11.;run;procprintdata=aromasf;title'ResultsfromMISSOVEROption';run; 4-92Chapter4ReadingandWritingDifferentTypesofDataResultsfromMISSOVEROptionObsConditionCure1Cure2Cure31ANGERYlangYlang2ANXIETYBergamot3BOREDOM4DEPRESSIONBasilBergamot5DULLNESSGrapefruitLemongrass6GRIEF7HEADACHEChamomile8FATIGUEBasilPeppermint9INSOMNIAChamomileLavender10MIGRAINELavender11STRESSBenzoinBergamot12VERTIGOLavenderPeppermint13SHOCKPeppermintPetitgrain4.ReadingMultipleRecordsperObservation/*Viewrawdatafilebeforewritingcode*/procfslistfileref='raw-data-file';run;datamedical(drop=plan);infile'raw-data-file';lengthID$5LNameFName$11Plan$1Blood$3;inputID$LName$FName$;inputPlan$Blood$;/*SecondINPUTstatementloadsnextrecord.Aforwardslash(/)canalsobeused.*/run;procprintdata=medical;title'PatientNamesandBloodTypes';run; 4.4SolutionstoExercises4-935.ReadingMixedRecordTypes/*Viewrawdatafilebeforewritingcode*/procfslistfileref='raw-data-file';run;dataallergies;lengthID$5LNameFName$11Plan$1Blood$3Allergy$1AlgyType$2;infile'raw-data-file';inputID$LName$FName$Plan$Blood$Allergy$@;/*Trailing@preventsnewrecordfrombeingloaded.*/ifallergy='N'theninputdependants;elseifallergy='Y'theninputAlgytype$Dependants;run;procprintdata=allergies;title'PatientsandAllergyCode';run;6.SubsettingfromaRawDataFile(Optional)dataallergies2;lengthID$5LNameFName$11Plan$1Blood$3Allergy$1AlgyType$2;infile'raw-data-file';inputID$LName$FName$Plan$Blood$Allergy$@;ifallergy='Y';/*subsettingIF*/inputAlgytype$Dependants;run;procprintdata=allergies2;title'PatientswithAllergiesOnly';run; 4-94Chapter4ReadingandWritingDifferentTypesofData7.ReadingRawDatawithMultipleObservationsperRecord/*Viewrawdatafilebeforewritingcode*/procfslistfileref='raw-data-file';run;datatransactions;lengthType$1;infile'raw-data-file';inputDate:date9.Type$Amount:comma9.@@;/*Holduntilendofrecord*/run;procprintdata=transactions;title'AccountTransactions';varDateTypeAmount;run;8.CreatingMultipleSASDataSetsfromaSingleRawDataFile(Optional)/*Viewrawdatafilebeforewritingcode*/procfslistfileref='raw-data-file';run;/*Createtwodatasets*/datacreditsdebits;lengthType$1;infile'raw-data-file';inputDate:date9.Type$Amount:comma9.@@;/*Holduntilendofrecord*//*UseTypetodeterminewhethercreditordebit*/ifType='C'thenoutputcredits;ifType='D'thenoutputdebits;run;procprintdata=credits;title'CreditstoAccount';varDateTypeAmount;run;procprintdata=debits;title'DebitstoAccount';varDateTypeAmount;run; 4.4SolutionstoExercises4-959.SubsettingfromaFixed-ColumnRawDataFile(Optional)/*Viewrawdatafilebeforewritingcode*/procfslistfileref='raw-data-file';run;/*Becausefixed-columnfields*//*Canbereadinanyorder,*//*ReadtheJobCodefirst,Usingthetrailing@*/datasalclrks;infile'raw-data-file';input@112JobCode$6.@;ifjobcode='SALCLK';input@1Division$20.@21HireDatemmddyy10.@31Salarydollar10.2@41LastName$15.@56FirstName$15.@71Country$15.@86Location$10.@96IdNumber$6.;run;procprintdata=salclrksnoobs;title'EmployeeInformationforSalesClerks';run;10.UsingtheAbsoluteLinePointerControl(Optional)/*Viewrawdatafilebeforewritingcode*/procfslistfileref='raw-data-file';run;dataempinfo;infile'raw-data-file'dlm=',';input#2IdNumber:$6.LastName:$15.FirstName:$15.#1Division:$20.HireDate:mmddyy10.Salary;run;procprintdata=empinfo;title'EmployeeData';run; 4-96Chapter4ReadingandWritingDifferentTypesofData11.ReadingaHierarchicalRawDataFileandCreatingOneObservationperDetailRecord/*Viewrawdatafilebeforewritingcode*/procfslistfileref='raw-data-file';run;datasalaries(drop=Type);retainIDLNameFName;lengthID$6;infile'raw-data-file';inputType$@;ifType='E'then/*Thisisaheaderrecord*/inputID$FName$LName$;elseifType='S'thendo;inputSalYearSalary:comma8.;output;/*Outputsoneobservationforeachdetailrecord*/end;run;procprintdata=salaries;title'YearlySalariesThrough2001';run; 4.4SolutionstoExercises4-9712.ReadingaHierarchicalRawDataFileandCreatingOneObservationperHeaderRecord(Optional)/*Viewrawdatafilebeforewritingcode*/procfslistfileref='raw-data-file';run;datacurrent(drop=SalYearType);retainIDFNameLNameHireDateSalary;/*Mustretainallvariablesinnewdataset*/lengthID$6;infile'raw-data-file'end=LastRec;inputType$@;ifType='E'thendo;if_n_ne1thenoutput;/*Outputwhennextemployeeisread*/inputID$FName$LName$HireDate:date9.;end;elseifType='S'thendo;inputSalYearSalary:comma8.;end;ifLastRecthenoutput;run;procprintdata=current;title'Salariesasof2001';run; 4-98Chapter4ReadingandWritingDifferentTypesofData13.ReadingaHierarchicalFileUsingTwoDATASteps(Optional)/*ViewRawDataFileBeforeWritingCode*/procfslistfileref='salaries.dat';run;datastepone(drop=SalYearType);retainIDLNameFNameHireDate;lengthID$6;infile'salaries.dat';inputType$@;ifType='E'theninputID$FName$LName$HireDate:date9.;elseifType='S'theninputSalYearSalary:comma8.;run;procprintdata=stepone;title'ReadingaHierarchicalFile--FirstPhase';run;datatwostep;setstepone;byIDnotsorted;ifLast.IDthenoutput;run;procprintdata=twostep;title'Salariesasof2001';run; 4.4SolutionstoExercises4-9914.ReadingaHierarchicalFileWithoutanExplicitIdentifyingField(Optional)databirthstones;dropSpaceStone;retainMonthModernTraditionalMysticalAyurvedicOther1-Other3;infile'raw-data-file'end=LastMonthdlm=',';/*UsetheDLM=optiontomakesure*//*Thespaceisnotusedasadelimiter*//*Youcanalsoreadthebirthstones*//*withformattedinputifyouusethe*//*TRUNCOVERorPADoption*/lengthMonth$9ModernTraditional$12Mystical$11Ayurvedic$10Other1-Other3$15;input@1Space$1.@;/*Detailrecordshavealeadingspace*//*Theleadingspacemustbereadwith*//*formattedinput,orthespacewillbeignored.*/ifSpacene''thendo;if_n_ne1thenoutput;input@1Month$;Stone=1;Other2='';Other3='';end;/*Needtoknowhowmanydetailrecords*//*havebeenreadinordertoknowwhich*//*variableisbeingread.*/elsedo;ifStone=1theninputModern$;elseifStone=2theninputTraditional;elseifStone=3theninputMystical;elseifStone=4theninputAyurvedic;elseifStone=5theninputOther1;elseifStone=6theninputOther2;elseifStone=7theninputOther3;Stone+1;end;ifLastMonththenoutput;run;procprintdata=birthstones;title'VariousBirthstonesforEachMonth';run; 4-100Chapter4ReadingandWritingDifferentTypesofData Chapter5DataTransformations5.1Introduction.....................................................................................................................5-35.2ManipulatingCharacterValues.....................................................................................5-85.3ManipulatingNumericValues......................................................................................5-395.4ManipulatingNumericValuesBasedonDates..........................................................5-485.5ConvertingVariableType.............................................................................................5-535.6SolutionstoExercises.................................................................................................5-71 5-2Chapter5DataTransformations 5.1Introduction5-35.1IntroductionObjectivesReviewthesyntaxofSASfunctions.3SASFunctionsTheSASSystemprovidesalargelibraryoffunctionsformanipulatingdataduringDATAstepexecution.ASASfunctionisoftencategorizedbythetypeofdatamanipulationperformed:truncationsamplestatisticscharacterarithmeticdateandtimefinancialmathematicalrandomnumbertrigonometricstateandZIPcode.special4SeeSASdocumentationforacompletelistoffunctionsandtheirsyntax. 5-4Chapter5DataTransformationsSyntaxforSASFunctionsASASfunctionisaroutinethatperformsacomputationorsystemmanipulationandreturnsavalue.Functionsuseargumentssuppliedbytheuserorbytheoperatingenvironment.GeneralformofaSASfunction:function-namefunction-name((argument-1argument-1,,argument-2argument-2,…,,…,argument-nargument-n))5Eachargumentisseparatedfromtheothersbyacomma.Somefunctionsaccept•multipleargumentsinanyorder•aspecificnumberofargumentsinafixedorder•noarguments.Functionsthatrequireargumentsaccept•constants•variables•functions•expressions. 5.1Introduction5-5UsingSASFunctionsYoucanusefunctionsinexecutableDATAstepstatementsanywherethatanexpressioncanappear.datacontrib;setprog2.donate;Total=sum(Qtr1,Qtr2,Qtr3,Qtr4);ifTotalge50;run;procprintdata=contribnoobs;run;6UsingSASFunctionsPartialPROCPRINTOutputIDQtr1Qtr2Qtr3Qtr4TotalE00224123322.67E0036735484030153E00441.638990242E005871619302994E006211012152562WhatifyouwanttosumQtr1throughQtr400,insteadofQtr1throughQtr4?7Manyfunctionsignoreargumentsthatcontainamissingvalue. 5-6Chapter5DataTransformationsSASVariableListsASASvariablelistisanabbreviatedmethodofreferringtoalistofvariablenames.SASenablesyoutousethefollowingvariablelists:numberedrangelistsnamerangelistsnameprefixlistsspecialSASnamelists.8specifiesallvariablesfromx1toxninclusive.YoucanbeginwithanynumberandendwithNumberedx1-xnanynumberaslongasyoudonotviolatetherangelistsrulesforuser-suppliedvariablenamesandthenumbersareconsecutive.specifiesallvariablesorderedastheyareinthex--aprogramdatavector,fromxtoainclusive.Namespecifiesallnumericvariablesfromxtoax-numeric-arangelistsinclusive.specifiesallcharactervariablesfromxtoax-character-ainclusive.tellsSAStocalculatethesumofalltheNamesum(ofREV:)variablesthatbeginwithREV,suchasprefixlistsREVJAN,REVFEB,andREVMAR._ALL_specifiesallvariablesthatarealreadydefinedinthecurrentDATAstep.Special_NUMERIC_specifiesallnumericvariablesthatarecurrentlySASnamedefinedinthecurrentDATAstep.lists_CHARACTER_specifiesallcharactervariablesthatarecurrentlydefinedinthecurrentDATAstep. 5.1Introduction5-7SASVariableListsWhenyouuseaSASvariablelistinaSASfunction,usethekeywordOFinfrontofthefirstvariablenameinthelist.datacontrib;setprog2.donate;Total=sum(ofQtr1-Qtr4);ifTotalge50;run;IfyouomitthekeywordOF,subtractionisperformed.9 5-8Chapter5DataTransformations5.2ManipulatingCharacterValuesObjectivesUseSASfunctionsandoperatorstoextract,edit,andsearchcharactervalues.11AMailingLabelApplicationTheprog2.freqflyersdatasetcontainsinformationaboutfrequentflyers.Usethisdatasettocreateanotherdatasetsuitableformailinglabels.12 5.2ManipulatingCharacterValues5-9AMailingLabelApplicationIDisacharactervariable.Itslastdigitrepresentsthegender(1denotesfemale,2denotesmale)ofthefrequentflyer.prog2.freqflyersIDNameAddress1Address2F31351Farr,Sue15HarveyRd.Macon,Bibb,GA,31298F161Cox,KayB.163McNeilPl.Kern,Pond,CA,93280F212Mason,Ron442GlenAve.Miami,Dade,FL,33054F25122Ruth,G.H.2491BradySt.Munger,Bay,MI,4874713AMailingLabelApplicationlabelsFullNameAddress1Address2Ms.SueFarr15HarveyRd.Macon,GA31298Ms.KayB.Cox163McNeilPl.Kern,CA93280Mr.RonMason442GlenAve.Miami,FL33054Mr.G.H.Ruth2491BradySt.Munger,MI48747ThefirsttaskistocreateatitleofMr.orMs.basedonthelastdigitofID.14 5-10Chapter5DataTransformationsTheSUBSTRFunction(RightSide)TheSUBSTRfunctionisusedtoextractorreplacecharacters.NewVarNewVar=SUBSTR(=SUBSTR(stringstring,,startstart<,<,length>length>););ThisformoftheSUBSTRfunction(rightsideofassignmentstatement)extractscharacters.15stringcanbeacharacterconstant,variable,orexpression.startspecifiesthestartingposition.lengthspecifiesthenumberofcharacterstoextract.Ifomitted,thesubstringconsistsoftheremainderofstring.IfthelengthofthecreatedvariableisnotpreviouslydefinedwithaLENGTHstatement,itisthesameasthelengthofthefirstargumenttoSUBSTR.TheSUBSTRFunction(RightSide)ExtracttwocharactersfromLocationandstartatposition11.State=substr(Location,11,2);LOCATIONSTATE$$1818Columbus,OH43227OH16...... 5.2ManipulatingCharacterValues5-11AMailingLabelApplicationprocprintdata=prog2.freqflyersnoobs;varID;run;PROCPRINTOutputIDF31351F161F212F25122InwhatpositiondoesthelastdigitofIDoccur?17TheRIGHTFunctionTheRIGHTfunctionreturnsitsargumentright-aligned.Trailingblanksaremovedtothestartofthevalue.NewVarNewVar=RIGHT(=RIGHT(argumentargument););NewID=right(ID);IDNEWID$$66F161F16118......argumentcanbeacharacterconstant,variable,orexpression.IfthelengthofthecreatedvariableisnotpreviouslydefinedwithaLENGTHstatement,itisthesameasthelengthofargument.TheLEFTfunctionreturnsitsargumentleft-aligned.Leadingblanksaremovedtotheendofthevalue.Theargument'slengthdoesnotchange.NewVar=LEFT(argument); 5-12Chapter5DataTransformationsAMailingLabelApplicationdatalabels;setprog2.freqflyers;ifsubstr(right(ID),6)='1'thenTitle='Ms.';elseifsubstr(right(ID),6)='2'thenTitle='Mr.';run;procprintdata=labelsnoobs;varIDTitle;run;TheresultoftheRIGHTfunctionactsasthefirstargumenttotheSUBSTRfunction.19AMailingLabelApplicationPROCPRINTOutputIDTitleF31351Ms.F161Ms.F212Mr.F25122Mr.20AMailingLabelApplicationThenexttaskistoseparatethenamesofthefrequentflyersintotwoparts.NAMEFarr,SueCox,KayB.FMNAMELNAMESueFarrKayB.Cox21...... 5.2ManipulatingCharacterValues5-13TheSCANFunctionTheSCANfunctionreturnsthenthwordofacharactervalue.Itisusedtoextractwordsfromacharactervaluewhentherelativeorderofwordsisknown,buttheirstartingpositionsarenot.NewVarNewVar=SCAN(=SCAN(stringstring,,nn<,<,delimiters>delimiters>););22stringcanbeacharacterconstant,variable,orexpression.nspecifiesthenthwordtoextractfromstring.delimitersdefinescharactersthatdelimit(separate)words.Ifthethirdargumentisomitted,thedefaultdelimitersareASCII(PC,UNIX)blank.<(+|&!$*);-/,%^EBCDIC(z/OS)blank.<(+|&!$*);-/,%|¢¬TheSCANFunctionWhentheSCANfunctionisused,thelengthofthecreatedvariableis200bytesifitisnotpreviouslydefinedwithaLENGTHstatementdelimitersbeforethefirstwordhavenoeffectanycharacterorsetofcharacterscanserveasdelimiterstwoormorecontiguousdelimitersaretreatedasasingledelimiteramissingvalueisreturnediftherearefewerthannwordsinstringifnisnegative,SCANselectsthewordinthecharacterstringstartingfromtheendofstring.23 5-14Chapter5DataTransformationsTheSCANFunctionExtractthesecondwordofPhrase.Second=scan(Phrase,2,'');PHRASESECOND$$21200softwareandservicesand12324......TheSCANFunctionExtractthesecondwordofPhrase.Second=scan(Phrase,2,':');PHRASESECOND$$21200softwareand:servicesservices1225......TheSCANFunctiondatascan;Text='(ThursdayJuly4,1776)';Var1=scan(Text,1);Var2=scan(Text,4);Var3=scan(Text,5);Var4=scan(Text,2,',');Var5=scan(Text,2,',)');run;VAR1VAR2VAR3VAR4VAR5$$$$$200200200200200Thursday17761776)177629...... 5.2ManipulatingCharacterValues5-15AMailingLabelApplicationdatalabels;lengthFMNameLName$10;setprog2.freqflyers;ifsubstr(right(ID),6)='1'thenTitle='Ms.';elseifsubstr(right(ID),6)='2'thenTitle='Mr.';FMName=scan(Name,2,',');LName=scan(Name,1,',');run;30AMailingLabelApplicationprocprintdata=labelsnoobs;varIDNameTitleFMNameLName;run;PROCPRINTOutputIDNameTitleFMNameLNameF31351Farr,SueMs.SueFarrF161Cox,KayB.Ms.KayB.CoxF212Mason,RonMr.RonMasonF25122Ruth,G.H.Mr.G.H.RuthThenexttaskistojointhevaluesofTitle,FMName,andLNameintoanothervariable.31 5-16Chapter5DataTransformationsConcatenationOperatorTheconcatenationoperatorjoinscharacterstrings.Dependingonthecharactersavailableonyourkeyboard,thesymboltoconcatenatecharactervaluescanbetwoexclamationpoints(!!),twoverticalbars(||),ortwobrokenverticalbars(¦¦).NewVarNewVar==string1string1!!!!string2string2;;32IfthelengthofthecreatedvariableisnotpreviouslydefinedwithaLENGTHstatement,itisthesumofthelengthsoftheconcatenatedconstants,variables,andexpressions.ConcatenationOperatorCombineFMNameandLNametocreateFullName.FullName=FMName!!LName;FMNAMELNAMEFULLNAME$$$101020SueFarrSueFarr33......Theconcatenationoperatordoesnottrimtrailingblanks. 5.2ManipulatingCharacterValues5-17TheTRIMFunctionTheTRIMfunctionremovestrailingblanksfromitsargument.NewVarNewVar=TRIM(=TRIM(argument1argument1)!!)!!argument2argument2;;Iftheargumentisblank,TRIMreturnsoneblank.34argument1andargument2canbecharacterconstants,variables,orexpressions.TheTRIMandTRIMNfunctionsaresimilar.TRIMNreturnsanullstring(zeroblanks)iftheargumentisblank.TheCOMPBLfunctionisalsousedtoremovemultipleblanksinacharacterstring.COMPBLtranslateseachoccurrenceoftwoormoreconsecutiveblanksintoasingleblank.ThevaluethattheCOMPBLfunctionreturnshasadefaultlengthof200.TheTRIMFunctiondatatrim;lengthFMNameLName$10;FMName='Sue';LName='Farr';FullName1=trim(FMName);FullName2=trim(FMName)!!LName;FullName3=trim(FMName)!!''!!LName;run;FULLNAME1FULLNAME2FULLNAME3$$$102021SueSueFarrSueFarr36......TheTRIMfunctiondoesnotremoveleadingblanksfromacharacterargument.UseacombinationoftheTRIMandLEFTfunctionstoremoveleadingandtrailingblanksfromacharacterargument.Forexample,ifFMNamecontainedleadingblanks,thefollowingassignmentstatementwouldcorrectlyconcatenateFMNameandLNameintoFullName:FullName=trim(left(FMName))!!''!!LName; 5-18Chapter5DataTransformationsAMailingLabelApplicationdatalabels(keep=FullNameAddress1Address2);lengthFMNameLName$10;setprog2.freqflyers;ifsubstr(right(ID),6)='1'thenTitle='Ms.';elseifsubstr(right(ID),6)='2'thenTitle='Mr.';FMName=scan(Name,2,',');LName=scan(Name,1,',');FullName=Title!!''!!trim(FMName)!!''!!LName;Address2=scan(Address2,1,',')!!','!!scan(Address2,3,',')!!''!!scan(Address2,4,',');run;37AMailingLabelApplicationprocprintdata=labelsnoobs;varFullNameAddress1Address2;run;PROCPRINTOutputFullNameAddress1Address2Ms.SueFarr15HarveyRd.Macon,GA31298Ms.KayB.Cox163McNeilPl.Kern,CA93280Mr.RonMason442GlenAve.Miami,FL33054Mr.G.H.Ruth2491BradySt.Munger,MI48747c05s2d1.sas38 5.2ManipulatingCharacterValues5-19TheCATXFunctionTheCATXfunctionconcatenatescharacterstrings,removesleadingandtrailingblanks,andinsertsseparators.CATXCATX((separatorseparator,,string-1,string-1,……string-nstring-n))TheCATXfunctionisnewinSAS®9.39®OtherSAS9concatenationfunctionsareCATconcatenatescharacterstringswithoutremovingleadingortrailingblanks.CATSconcatenatescharacterstringsandremovesleadingandtrailingblanks.CATTconcatenatescharacterstringsandremovestrailingblanksonly.AMailingLabelApplicationdatalabels(keep=FullNameAddress1Address2);lengthFMNameLName$10;setprog2.freqflyers;ifsubstr(right(ID),6)='1'thenTitle='Ms.';elseifsubstr(right(ID),6)='2'thenTitle='Mr.';FMName=scan(Name,2,',');Lname=scan(Name,1,',');FullName=catx('',Title,FMName,LName);Address2=catx('',scan(Address2,1,','),scan(Address2,3,',')||’,’,scan(Address2,4,','));run;40 5-20Chapter5DataTransformationsAMailingLabelApplicationprocprintdata=labelsnoobs;varFullNameAddress1Address2;run;PROCPRINTOutputFullNameAddress1Address2Ms.SueFarr15HarveyRd.Macon,GA31298Ms.KayB.Cox163McNeilPl.Kern,CA93280Mr.RonMason442GlenAve.Miami,FL33054Mr.G.H.Ruth2491BradySt.Munger,MI48747c05s2d2.sas41 5.2ManipulatingCharacterValues5-21Exercises1.ManipulatingCharacterValuesAllvaluesofNameinprog2.peopleconsistofalastname,firstname,andmiddleinitial.Listingofprog2.peopleNameCityStateDEAN,LINDSAYA.WILMINGTON,NCFLORENTINO,HELEN-ASHEH.WASHINGTON,DCVANALLSBURG,JANF.SHORTHILLS,NJLAFF,STANLEYX.SPRINGFIELD,ILRIZEN,GEORGEQ.CHICAGO,ILMITCHELL,MARCJ.CHICAGO,ILMILLS,DOROTHYE.JOE,MTWEBB,JONATHANW.MORRISVILLE,NCKEENAN,MAYNARDJ.SEDONA,AZLACK,PHYLLISM.WALTHAM,MATHOMPSON,KERRYL.WINTERPARK,FLCOX,DOROTHYE.TIMONIUM,MDSEPTOFF,DONALDE.BOSTON,MAPHOENIX,JANICEA.SOMERVILLE,NJHUNEYCUTT,MURRAYY.DIMEBOX,TXERICKSON,SHERRYA.ELPASO,TXSCHNEIDER,CLIVEJ.CAPEMAY,NJPUTNAM,KIMBERLYM.DUNWOODY,GAPITTMAN,JENNIFERR.BENNINGTON,VTROLEN,STACYD.CODY,WYSomenamescontainhyphenatedfirstnamesormultiple-wordlastnames.ReadthevariablesNameandCityStatefromprog2.peopletocreateatemporarySASdatasetnamedseparatethatcontainsthevariablesFirst,MI,andLast.Payspecialattentiontotrailingandleadingblanks,andthelengthsofFirst,MI,andLast.TocreateFirstandMI,createavariablethatcontainseachperson'sfirstnameandmiddleinitial.Donotincludethisvariableintheseparatedataset. 5-22Chapter5DataTransformationsPrinttheseparatedatasettoverifyyourresults.PartialListingofseparateObsNameCityStateFirstMILast1DEAN,LINDSAYA.WILMINGTON,NCLINDSAYA.DEAN2FLORENTINO,HELEN-ASHEH.WASHINGTON,DCHELEN-ASHEH.FLORENTINO3VANALLSBURG,JANF.SHORTHILLS,NJJANF.VANALLSBURG4LAFF,STANLEYX.SPRINGFIELD,ILSTANLEYX.LAFF5RIZEN,GEORGEQ.CHICAGO,ILGEORGEQ.RIZEN2.CombiningCharacterValuesUsetheDATAstepthatcreatesseparatetocreateatemporarySASdatasetnamedflnamethatcontainsthevariablesNewNameandCityState.ThevaluesofNewNameshouldbetheconcatenationofeachperson'sfirstnameandlastnamewithasingleblankbetweenthem.PartialListingofprog2.peopleNameCityStateDEAN,LINDSAYA.WILMINGTON,NCFLORENTINO,HELEN-ASHEH.WASHINGTON,DCVANALLSBURG,JANF.SHORTHILLS,NJLAFF,STANLEYX.SPRINGFIELD,ILRIZEN,GEORGEQ.CHICAGO,ILSomenamescontainhyphenatedfirstnamesormultiple-wordlastnames.Printtheflnamedatasettoverifyyourresults.PartialListingofflnameObsNewNameCityState1LINDSAYDEANWILMINGTON,NC2HELEN-ASHEFLORENTINOWASHINGTON,DC3JANVANALLSBURGSHORTHILLS,NJ4STANLEYLAFFSPRINGFIELD,IL5GEORGERIZENCHICAGO,IL 5.2ManipulatingCharacterValues5-233.PerformingAdditionalCharacterManipulations(Optional)UsetheDATAstepthatcreatesseparatetocreateatemporarySASdatasetnamedinitthatcontainsthevariablesName,Initials,andCityState.ThevaluesofInitialsshouldbetheconcatenationofthefirstcharacterfromeachperson'sfirstname,middlename,andlastnamewithnodelimitersseparatingthecharacters.Printtheinitdatasettoverifyyourresults.PartialListingofinitObsNameCityStateInitials1DEAN,LINDSAYA.WILMINGTON,NCLAD2FLORENTINO,HELEN-ASHEH.WASHINGTON,DCHHF3VANALLSBURG,JANF.SHORTHILLS,NJJFV4LAFF,STANLEYX.SPRINGFIELD,ILSXL5RIZEN,GEORGEQ.CHICAGO,ILGQR 5-24Chapter5DataTransformationsASearchApplicationTheprog2.ffhistorydatasetcontainsinformationaboutthehistoryofeachfrequentflyer.Thishistoryinformationconsistsofeachmembershiplevelthattheflyerattained(bronze,silver,orgold)theyearthattheflierattainedeachlevel.Createareportthatshowsallfrequentflyerswhoattainedsilvermembershipstatusandtheyeareachofthembecamesilvermembers.43ASearchApplicationprog2.ffhistorySeatIDStatusPrefF31351Silver1998,Gold2000AISLEF161Bronze1999WINDOWF212Bronze1992,silver1995WINDOWF25122Bronze1994,Gold1996,Silver1998AISLETodeterminewhoattainedsilvermembershipstatus,youmustsearchtheStatusvariableforthevalue"Silver".44 5.2ManipulatingCharacterValues5-25TheFINDFunctionTheFINDfunctionsearchesforaspecificsubstringofcharacterswithinacharacterstringthatyouspecifyandreturnsitslocation.PositionPosition=FIND(=FIND(targettarget,,value<,modifiers,startpos>value<,modifiers,startpos>););TheFINDfunctionreturnsthestartingpositionofthefirstoccurrenceofvaluewithintarget,ifvalueisfound0,ifvalueisnotfound.TheFINDfunctionisnewinSAS®9.45AmodifiercanbethevalueIorT.Iindicatesthatthesearchiscase-insensitive.Tindicatesthatthesearchignorestrailingblanks.Thesetwovaluescanbecombinedineitherorderandineithercase.Ifthisargumentisomitted,thesearchiscase-sensitiveandtrailingblanksaretakenintoconsideration.Thestartposisanintegerthatspecifiesthepositionatwhichthesearchshouldstartandthedirectionofthesearch.Apositivevalueindicatesaforward(right)search.Anegativevalueindicatesabackward(left)search.Ifthisargumentisomitted,thesearchstartsatposition1andmovesforward.Thesetwooptionalargumentscanbeineitherorder(thatis,startposcanprecedemodifier).TheFINDFunctionDeterminewhetherTextcontainsthestring"BULL'S-EYE".Text="ThistargetcontainsaBULL'S-EYE.";Pos=find(Text,"BULL'S-EYE");24TEXTPOS$N348ThistargetcontainsaBULL'S-EYE.2446...... 5-26Chapter5DataTransformationsTheFINDFunction68dataindex;Text='DELIMITITWITHBLANKS.';Pos1=find(Text,'IT');Pos2=find(Text,'IT');Pos3=find(Text,'it');Pos4=find(Text,'it','I');run;POS1POS2POS3POS4NNNN8888680647......TheFINDFunctiondataindex2;68lengthString$5;String='IT';Text='DELIMITITWITHBLANKS.';Pos5=find(Text,String);Pos6=find(Text,String,'T');Pos7=find(Text,''!!trim(String)!!'');run;STRINGPOS5POS6POS7$NNN5888IT06848......ASearchApplicationprog2.ffhistorySeatIDStatusPrefF31351Silver1998,Gold2000AISLEF161Bronze1999WINDOWF212Bronze1992,silver1995WINDOWF25122Bronze1994,Gold1996,Silver1998AISLEdatasilver;setprog2.ffhistory;iffind(Status,'silver','I')>0;run;49 5.2ManipulatingCharacterValues5-27ASearchApplicationprocprintdata=silvernoobs;run;PROCPRINTOutputSeatIDStatusPrefF31351Silver1998,Gold2000AISLEF212Bronze1992,silver1995WINDOWF25122Bronze1994,Gold1996,Silver1998AISLE50TheINDEXFunctionTheINDEXfunctionsearchesacharacterargumentforthelocationofaspecifiedcharactervalueandreturnsitslocation.PositionPosition=INDEX(=INDEX(targettarget,,valuevalue););TheINDEXfunctionreturnsthestartingpositionofthefirstoccurrenceofvaluewithintarget,ifvalueisfound0,ifvalueisnotfound.51targetspecifiesthecharacterexpressiontosearch.valuespecifiesthestringofcharacterstosearchforinthecharacterexpression.Thesearchforvalueisliteral.Capitalizationandblanks(leading,embedded,andtrailing)areconsidered.INDEXdiffersfromFINDinthatitdoesnothavemodifierorstartposfunctionality. 5-28Chapter5DataTransformationsTheINDEXFunctionDeterminewhetherTextcontainsthestring"BULL'S-EYE".Text="ThistargetcontainsaBULL'S-EYE.";Pos=index(Text,"BULL'S-EYE");24TEXTPOS$N348ThistargetcontainsaBULL'S-EYE.2452......TheINDEXFunction68dataindex;Text='DELIMITITWITHBLANKS.';Pos1=index(Text,'IT');Pos2=index(Text,'IT');Pos3=index(Text,'it');run;POS1POS2POS3NNN88868053......TheINDEXFunctiondataindex2;68lengthString$5;String='IT';Text='DELIMITITWITHBLANKS.';Pos4=index(Text,String);Pos5=index(Text,trim(String));Pos6=index(Text,''!!trim(String)!!'');run;STRINGPOS4POS5POS6$NNN5888IT06854...... 5.2ManipulatingCharacterValues5-29ASearchApplicationprog2.ffhistorySeatIDStatusPrefF31351Silver1998,Gold2000AISLEF161Bronze1999WINDOWF212Bronze1992,silver1995WINDOWF25122Bronze1994,Gold1996,Silver1998AISLEdatasilver;setprog2.ffhistory;ifindex(Status,'Silver')>0;run;55ASearchApplicationprocprintdata=silvernoobs;run;PROCPRINTOutputSeatIDStatusPrefF31351Silver1998,Gold2000AISLEF25122Bronze1994,Gold1996,Silver1998AISLEWhywasF212notselected?56 5-30Chapter5DataTransformationsTheUPCASEFunctionTheUPCASEfunctionconvertsalllettersinitsargumenttouppercasehasnoeffectondigitsandspecialcharacters.NewValNewVal=UPCASE(=UPCASE(argumentargument););57argumentspecifiesanycharacterargument.ASearchApplicationdatasilver(drop=Location);lengthYear$4;setprog2.ffhistory;Location=index(upcase(Status),'SILVER');ifLocation>0;Year=substr(Status,Location+7,4);run;procprintdata=silvernoobs;varIDStatusYearSeatPref;run;58 5.2ManipulatingCharacterValues5-31ASearchApplicationYear=substr(Status,Location+7,4);PROCPRINTOutputLocationLocation+7SeatIDStatusYearPrefF31351Silver1998,Gold20001998AISLEF212Bronze1992,silver19951995WINDOWF25122Bronze1994,Gold1996,Silver19981998AISLEDidthecapitalizationofthevaluesofStatuspermanentlychange?59......ThePROPCASEFunctionThePROPCASEfunctionconvertsallwordsinanargumenttopropercase,inwhichthefirstletterisuppercaseandtheremaininglettersarelowercase.NewValNewVal=PROPCASE(=PROPCASE(argumentargument<,<,delimiter(s)delimiter(s)>);>);ThePROPCASEfunctionisnewinSAS®9.60Delimitersarecharacterswhichseparatewords.ThedefaultdelimitersforthePROPCASEfunctionaretheblank,forwardslash,hyphen,openparenthesis,period,andtabcharacters. 5-32Chapter5DataTransformationsASearchApplicationdatasilver(drop=Location);lengthYear$4;setprog2.ffhistory;Status=propcase(Status,',');Location=find(Status,'Silver');ifLocation>0;SeatPref=propcase(Seatpref);Year=substr(Status,Location+7,4);run;procprintdata=silvernoobs;varIDStatusYearSeatPref;run;c05s2d3.sas61ASearchApplicationPROCPRINTOutputSeatIDStatusYearPrefF31351Silver1998,Gold20001998AisleF212Bronze1992,Silver19951995WindowF25122Bronze1994,Gold1996,Silver19981998Aisle62 5.2ManipulatingCharacterValues5-33ThePROPCASEFunctionThePROPCASEfunctionconvertsallwordsinanargumenttopropercase,inwhichthefirstletterisuppercaseandtheremaininglettersarelowercase.NewValNewVal=PROPCASE(=PROPCASE(argumentargument<,<,delimiter(s)delimiter(s)>);>);ThePROPCASEfunctionisnewinSAS®9.60sourcespecifiesthesourcestringthatyouwanttotranslate.targetspecifiesthestringsearchedforinsource.replacementspecifiesthestringthatreplacestarget.IfthelengthofthecreatedvariableisnotpreviouslydefinedwithaLENGTHstatement,itis200bytes.TheTRANWRDFunctionReplacethefirstwordofDessert.Dessert=tranwrd(Dessert,'Pumpkin','Apple');DESSERTDESSERT$$2020PumpkinpieApplepie64......UsingtheTRANWRDfunctiontoreplaceanexistingstringwithalongerstringmightcausetruncationoftheresultingvalueifaLENGTHstatementisnotused. 5-34Chapter5DataTransformationsASearchApplicationdatasilver(drop=Location);lengthYear$4;setprog2.ffhistory;Status=tranwrd(Status,'silver','Silver');Location=index(Status,'Silver');ifLocation>0;Year=substr(Status,Location+7,4);run;procprintdata=silvernoobs;varIDStatusYearSeatPref;run;c05s2d4.sas65ASearchApplicationPROCPRINTOutputSeatIDStatusYearPrefF31351Silver1998,Gold20001998AISLEF212Bronze1992,Silver19951995WINDOWF25122Bronze1994,Gold1996,Silver19981998AISLE66 5.2ManipulatingCharacterValues5-35TheLOWCASEFunctionTheLOWCASEfunctionconvertsalllettersinitsargumenttolowercasehasnoeffectondigitsandspecialcharacters.NewValNewVal=LOWCASE(=LOWCASE(argumentargument););67argumentspecifiesanycharacterargument.TheSUBSTRFunction(LeftSide)TheSUBSTRfunctionisusedtoextractorreplacecharacters.SUBSTR(SUBSTR(stringstring,,startstart<,<,lengthlength>)=>)=valuevalue;;ThisformoftheSUBSTRfunction(leftsideofassignmentstatement)replacescharactersinacharactervariable.68stringspecifiesacharactervariable.startspecifiesanumericexpressionthatisthebeginningcharacterposition.lengthspecifiesanumericexpressionthatisthelengthofthesubstringthatwillbereplaced.Thelengthvaluecannotbelargerthantheremaininglengthofstring(includingtrailingblanks)afterstart.Ifyouomitlength,SASusesallofthecharactersontherightsideoftheassignmentstatementtoreplacethevaluesofstring,uptothelimitindicatedbythepreviousnote. 5-36Chapter5DataTransformationsTheSUBSTRFunction(LeftSide)ReplacetwocharactersfromLocationstartingatposition11.substr(Location,11,2)='OH';LOCATIONLOCATION$$1818Columbus,GA43227Columbus,OH4322769......TheLOWCASEFunctiondatasilver;setsilver;substr(SeatPref,2)=lowcase(substr(SeatPref,2));run;SEATPREFSEATPREF$$66AISLEAisleislec05s2d5.sas70......ASearchApplicationprocprintdata=silvernoobs;varIDYearSeatPref;run;PROCPRINTOutputSeatIDYearPrefF313511998AisleF2121995WindowF251221998Aisle71 5.2ManipulatingCharacterValues5-37Exercises4.SearchingforaCharacterValueReadthevariablesNameandCityStatefromprog2.peopletocreateatemporarySASdatasetnamedprairiethatcontainsonlythosepeoplewholiveinthestateofIllinois(IL).UseanappropriatefunctiontosearchthroughthevaluesofCityState.PartialListingofprog2.peopleNameCityStateDEAN,LINDSAYA.WILMINGTON,NCFLORENTINO,HELEN-ASHEH.WASHINGTON,DCVANALLSBURG,JANF.SHORTHILLS,NJLAFF,STANLEYX.SPRINGFIELD,ILRIZEN,GEORGEQ.CHICAGO,ILPrinttheprairiedatasettoverifyyourresults.Thereshouldbethreeobservations.ListingofprairieObsNameCityState1LAFF,STANLEYX.SPRINGFIELD,IL2RIZEN,GEORGEQ.CHICAGO,IL3MITCHELL,MARCJ.CHICAGO,ILIllinoisisunofficiallyknownasthePrairieState.ThisnicknameoriginatesfromthepracticeofdeclaringthethirdfullweekinSeptembereachyearasIllinoisPrairieWeektodemonstratethevalueofpreservingandre-establishingnativeIllinoisprairies.5.PerformingAdditionalCharacterManipulationsReadthevariablesNameandCityStatefromprairietocreateatemporarySASdatasetnamedmixedprairiethatcontainsthevaluesofNameconvertedfromalluppercasetomixedcaseasshownbelow.Printthemixedprairiedatasettoverifyyourresults.ListingofmixedprairieObsNameCityState1Laff,StanleyX.SPRINGFIELD,IL2Rizen,GeorgeQ.CHICAGO,IL3Mitchell,MarcJ.CHICAGO,IL 5-38Chapter5DataTransformations6.UsingAdditionalCharacterFunctionsReadthevariablesNameandCityStatefromprog2.peopletocreateatemporarySASdatasetnamedstatelong.UsetheSTNAMELfunctiontoconvertthestatepostalcodeinCityStatetothecorrespondingstatename.StorethesestatenamesinavariablenamedStateName.TheSTNAMELfunctionconvertsatwo-characterstatepostalcode(orworld-wideGSAgeographiccodeforU.S.territories),suchasILforIllinois,tothecorrespondingstatenameinmixedcase.Returnedvaluescancontainupto20characters.NewState=STNAMEL(postal-code);postal-codespecifiesacharacterexpressionthatcontainsthetwo-characterstandardstatepostalcode.Characterscanbemixedcase.STNAMELignorestrailingblanksbutgeneratesanerroriftheexpressioncontainsleadingblanks.PartialListingofstatelongObsNameStateName1DEAN,LINDSAYA.NorthCarolina2FLORENTINO,HELEN-ASHEH.DistrictofColumbia3VANALLSBURG,JANF.NewJersey4LAFF,STANLEYX.Illinois5RIZEN,GEORGEQ.Illinois7.PerformingAdditionalCharacterManipulations(Optional)ReadthevariablesNameandCityStatefromprog2.peopletocreateatemporarySASdatasetnamedmixedallthatcontainsthevaluesofNameconvertedfromalluppercasetomixedcaseasshownbelow.Printthemixedalldatasettoverifyyourresults.PartialListingofmixedallObsNameCityState1Dean,LindsayA.WILMINGTON,NC2Florentino,Helen-AsheH.WASHINGTON,DC3VanAllsburg,JanF.SHORTHILLS,NJ4Laff,StanleyX.SPRINGFIELD,IL5Rizen,GeorgeQ.CHICAGO,ILSomenamescontainhyphenatedfirstnamesormultiple-wordlastnames. 5.3ManipulatingNumericValues5-395.3ManipulatingNumericValuesObjectivesUseSASfunctionstotruncatenumericvalues.UseSASfunctionstocomputesamplestatisticsofnumericvalues.74TruncationFunctionsSelectedfunctionsthattruncatenumericvaluesincludeROUNDfunctionCEILfunctionFLOORfunctionINTfunction.75 5-40Chapter5DataTransformationsTheROUNDFunctionTheROUNDfunctionreturnsavalueroundedtothenearestround-offunit.NewVarNewVar=ROUND(=ROUND(argumentargument<,<,round-off-unitround-off-unit>);>);Ifround-off-unitisnotprovided,argumentisroundedtothenearestinteger.76argumentisnumeric.round-off-unitisnumericandpositive.TheROUNDFunctiondatatruncate;NewVar1=round(12.12);NewVar2=round(42.65,.1);NewVar3=round(6.478,.01);NewVar4=round(96.47,10);run;NEWVAR1NEWVAR2NEWVAR3NEWVAR41242.76.4810077...... 5.3ManipulatingNumericValues5-41TheCEILFunctionTheCEILfunctionreturnsthesmallestintegergreaterthanorequaltotheargument.NewVarNewVar=CEIL(=CEIL(argumentargument););345X4.4x=ceil(4.4);x=5;78......argumentisnumeric.TheFLOORFunctionTheFLOORfunctionreturnsthegreatestintegerlessthanorequaltotheargument.NewVarNewVar=FLOOR(=FLOOR(argumentargument););345X3.6y=floor(3.6);y=3;79......argumentisnumeric. 5-42Chapter5DataTransformationsTheINTFunctionTheINTfunctionreturnstheintegerportionoftheargument.NewVarNewVar=INT(=INT(argumentargument););345X3.9z=int(3.9);z=3;80......argumentisnumeric.TruncationFunctionsdatatruncate;Var1=6.478;NewVar1=ceil(Var1);NewVar2=floor(Var1);NewVar3=int(Var1);run;VAR1NEWVAR1NEWVAR2NEWVAR36.47876681...... 5.3ManipulatingNumericValues5-43TruncationFunctionsdatatruncate;Var1=-6.478;NewVar1=ceil(Var1);NewVar2=floor(Var1);NewVar3=int(Var1);run;VAR1NEWVAR1NEWVAR2NEWVAR3-6.478-6-7-682......Forvaluesgreaterthan0,FLOORandINTreturnthesamevalue.Forvalueslessthan0,CEILandINTreturnthesamevalue. 5-44Chapter5DataTransformationsFunctionsThatComputeStatisticsSelectedfunctionsthatcomputesamplestatisticsbasedonagroupofvaluesincludeSUMfunction(totalofvalues)MEANfunction(averageofvalues)MINfunction(lowestvalue)MAXfunction(highestvalue).83Thesefunctions•acceptmultipleargumentsinanyorder•usethesamealgorithmasSASstatisticalprocedures•ignoremissingvalues.TheMINfunctionreturnsthesmallestnon-missingvalue.MIN(argument-1,argument-2,…,argument-n)TheMAXfunctionreturnsthelargestvalue:MAX(argument-1,argument-2,…,argument-n)argument-1throughargument-narenumeric.Atleasttwoargumentsarerequired.Theargumentlistmightconsistofavariablelist,whichisprecededbyOF. 5.3ManipulatingNumericValues5-45TheSUMFunctionTheSUMfunctionaddsvaluestogetherandignoresmissingvalues.NewVarNewVar=SUM(=SUM(argument-1argument-1,,argument-2argument-2,,……,,argument-nargument-n););84argument-1throughargument-narenumeric.TheassignmentstatementcanberewrittentotakeadvantageofSASvariablelists:NewVar=sum(ofVar1-Var3);TheSUMFunctiondatasummary;Var1=12;Var2=.;Var3=6;NewVar=sum(Var1,Var2,Var3);run;VAR1VAR2VAR3NEWVAR12.618WhatwouldbethevalueofNewVarifanarithmeticoperatorwasusedinsteadoftheSUMfunction?85... 5-46Chapter5DataTransformationsTheMEANFunctionTheMEANfunctioncalculatesthearithmeticmean(average)ofvaluesandignoresmissingvalues.NewVarNewVar=MEAN(=MEAN(argument-1argument-1,,argument-2argument-2,,……,,argument-nargument-n););86......argument-1throughargument-narenumeric.TheassignmentstatementcanberewrittentotakeadvantageofSASvariablelists:NewVar=mean(ofVar1-Var3);TheMEANFunctiondatasummary;Var1=12;Var2=.;Var3=6;NewVar=mean(Var1,Var2,Var3);run;VAR1VAR2VAR3NEWVAR12.6987...... 5.3ManipulatingNumericValues5-47Exercises8.ManipulatingNumericValuesCreateadatasetnamedfinalfromprog2.grade.ThefinaldatasetshouldcontainanewvariableOverallthatisthesemesteraveragegrade.CalculateOverallbyaveragingallthetestsplusthefinal.Thefinalisweightedtwiceasmuchasanyoftheothertests,socountthefinaltwicewhencalculatingOverall.StoreOverallroundedtothenearestinteger.Printthefinaldataset.PartialListingofprog2.gradeSSNCourseTest1Test2Test3Final012-40-4928BUS45080708080012-83-3816BUS45090906080341-44-0781MATH40078879091423-01-7721BUS45080707595448-23-8111MATH400889110095PartialListingoffinalObsSSNCourseTest1Test2Test3FinalOverall1012-40-4928BUS45080708080782012-83-3816BUS45090906080803341-44-0781MATH40078879091874423-01-7721BUS45080707595835448-23-8111MATH400889110095949.PerformingAdditionalNumericManipulations(Optional)ModifytheDATAstepcreatedinthepreviousexercisesothatthevalueofOverallistheaverageofthetwohighesttestscoresandthefinal.(ThelowesttestscoreshouldnotbeusedtocalculateOverall.)Asbefore,thefinalshouldbecountedtwice.StoreOverallroundedtothenearestinteger.Printthefinaldataset.PartialListingoffinalObsSSNCourseTest1Test2Test3FinalOverall1012-40-4928BUS45080708080802012-83-3816BUS45090906080853341-44-0781MATH40078879091904423-01-7721BUS45080707595865448-23-8111MATH40088911009595 5-48Chapter5DataTransformations5.4ManipulatingNumericValuesBasedonDatesObjectivesReviewSASfunctionsusedtocreateSASdatevalues.ReviewSASfunctionstoextractinformationfromSASdatevalues.UseSASfunctionstodetermineintervalsbetweentwoSASdatevalues.90CreatingSASDateValuesYoucanusetheMDYorTODAYfunctionstocreateSASdatevalues.TheMDYfunctioncreatesaSASdatevaluefrommonth,day,andyearvalues.NewDateNewDate=MDY(=MDY(monthmonth,,dayday,,yearyear););TheTODAYfunctionreturnsthecurrentdateasaSASdatevalue.NewDateNewDate=TODAY();=TODAY();91monthspecifiesanumericexpressionrepresentinganintegerfrom1to12.dayspecifiesanumericexpressionrepresentinganintegerfrom1to31.yearspecifiesanumericexpressionrepresentinganintegerthatidentifiesaspecifictwo-orfour-digityear.TheDATEfunctionissynonymouswiththeTODAYfunction. 5.4ManipulatingNumericValuesBasedonDates5-49ExtractingInformationYoucanusetheMONTH,DAY,andYEARfunctionstoextractinformationfromSASdatevalues.TheMONTHfunctioncreatesanumericvalue(1-12)thatrepresentsthemonthofaSASdatevalue.NewMonthNewMonth=MONTH(=MONTH(SAS-date-valueSAS-date-value););continued...92ExtractingInformationTheDAYfunctioncreatesanumericvalue(1-31)thatrepresentsthedayofaSASdatevalue.NewDayNewDay=DAY(=DAY(SAS-date-valueSAS-date-value););TheYEARfunctioncreatesafour-digitnumericvaluethatrepresentstheyear.NewYearNewYear=YEAR(=YEAR(SAS-date-valueSAS-date-value););93OthersimilarfunctionsincludeQTRreturnsthequarteroftheSASdatevalue(1-4;1representsJanuarythroughMarch,2representsAprilthroughJune,andsoon).WEEKDAYreturnsthedayoftheweekofaSASdatevalue(1-7;1representsSunday,7representsSaturday). 5-50Chapter5DataTransformationsCalculatinganIntervalofYearsTheYRDIFfunctionreturnsthenumberofyearsbetweentwoSASdatevalues.NewVal=NewVal=YRDIF(YRDIF(sdatesdate,,edateedate,,basisbasis););94sdatespecifiesaSASdatevaluethatidentifiesthestartingdate.edatespecifiesaSASdatevaluethatidentifiestheendingdate.basisidentifiesacharacterconstantorvariablethatdescribeshowSAScalculatesthedatedifference.Thefollowingcharacterstringsarevalid:'ACT/ACT'usestheactualnumberofdaysbetweendatesincalculatingthenumberofyears.SAScalculatesthisvalueasthenumberofdaysthatfallin365-dayyearsdividedby365plusthenumberofdaysthatfallin366-dayyearsdividedby366.Youcanuse'ACTUAL'asanalias.'30/360'specifiesa30-daymonthanda360-dayyearincalculatingthenumberofyears.Eachmonthisconsideredtohave30daysandeachyear360days,regardlessoftheactualnumberofdaysineachmonthoryear.SAStreatsthelastdayofanymonthasthelastdayofa30-daymonth.Youcanuse'360'asanalias.'ACT/360'usestheactualnumberofdaysbetweendatesincalculatingthenumberofyears.SAScalculatesthisvalueasthenumberofdaysdividedby360,regardlessoftheactualnumberofdaysineachyear.'ACT/365'usestheactualnumberofdaysbetweendatesincalculatingthenumberofyears.SAScalculatesthisvalueasthenumberofdaysdividedby365,regardlessoftheactualnumberofdaysineachyear.Tocalculatetheactualnumberofmonthsbetweentwodates,usetheYRDIFfunctionandmultiplyby12.NumMonths=yrdif(Date1,Date2,'ACT/ACT')*12; 5.4ManipulatingNumericValuesBasedonDates5-51TheYRDIFFunctionThevariableDOBrepresentsaperson’sdateofbirth.Assumetoday’sdateisMay3,2008,andDOBis8November1972.Whatisthisperson’sage?MyVal=yrdif(DOB,'3may2008'd,'act/act');MYVAL35.483606557Howcanyoualterthisprogramtocomputeeachemployee'sagebasedontoday'sdate?truncateallofthedecimalplaceswithoutrounding?95......TheDATDIFfunctioncanbeusedtoreturnthenumberofdaysbetweentwoSASdatevalues.OnlytwobasisvaluesarevalidfortheDATDIFfunction('ACT/ACT'and'30/360'). 5-52Chapter5DataTransformationsExercises10.ManipulatingNumericValuesBasedonDatesTheprog2.nodaydatasetcontainsinformationaboutemployees.Useprog2.nodaytocreateanewdatasetnamedemphire.UsetheexistingHiredMonthandHiredYearvariablestocreateanewvariable,Hired,thatstorestheSASdatevalueforeachemployee'sdateofhire.thAssumeeachemployeewashiredonthe15dayofthemonth.Createasecondnewvariable,Years,thatstoresthenumberofyearsbetweeneachemployee'sdateofhireandtoday'sdate.ThevaluesofHiredshouldbedisplayedusingaDATE9.format.ThevaluesofYearsshouldbetruncatedtoremovealldecimalswithoutrounding.Theemphiredatasetshouldcontainthreevariables:ID,Hired,andYears.Printthedatasettoverifyyourresults.Listingofprog2.nodayHiredHiredIDMonthYearE0346431994E0652381996E0734611997E09965101999E1346722000ListingofemphireObsIDHiredYears1E0346415MAR199482E0652315AUG199653E0734615JAN199754E0996515OCT199925E1346715FEB20002Theresultsaboveweregeneratedon3May2002.YourvaluesofYearsmaydiffer. 5.5ConvertingVariableType5-535.5ConvertingVariableTypeObjectivesUnderstandautomaticconversionofcharacterdataintonumericdata.Explicitlyconvertcharacterdataintonumericdata.Understandautomaticconversionofnumericdataintocharacterdata.Explicitlyconvertnumericdataintocharacterdata.98DataConversionInmanyapplications,youmightneedtoconvertonedatatypetoanother.Youmightneedtoreaddigitsincharacterformintoanumericvalue.Youmightneedtowriteanumericvaluetoacharacterstring.99 5-54Chapter5DataTransformationsDataConversionYoucanconvertdatatypesimplicitlybyallowingtheSASSystemtodoitforyouexplicitlywiththesefunctions:–INPUTcharacter-to-numericconversion–PUTnumeric-to-characterconversion.100TheINPUTstatementusesaninformattoreadadatavalueandthenoptionallystoresthatvalueinavariable.TheINPUTfunctionreturnsthevalueproducedwhenaSASexpressionisreadusingaspecifiedinformat.ThePUTstatementwritesavaluetoanexternaldestination(eitherthelogoradestinationyouspecify).ThePUTfunctionreturnsavalueusingaspecifiedformat.AutomaticCharacter-to-NumericConversionTheprog2.salary1datasetcontainsacharactervariableGrosspay.Computeatenpercentbonusforeachemployee.WhatwillhappenwhenthecharactervaluesofGrosspayareusedinanarithmeticexpression?101 5.5ConvertingVariableType5-55AutomaticCharacter-to-NumericConversionprog2.salary1IDGrossPay$11$5201-92-249852000482-87-794532000330-40-717249000databonuses;setprog2.salary1;Bonus=.10*GrossPay;run;c05s5d1.sas102AutomaticCharacter-to-NumericConversionPartialLog2databonuses;3setprog2.salary1;4Bonus=.10*GrossPay;5run;NOTE:Charactervalueshavebeenconvertedtonumericvaluesattheplacesgivenby:(Line):(Column).4:14NOTE:ThedatasetWORK.BONUSEShas3observationsand3variables.103AutomaticCharacter-to-NumericConversionprocprintdata=Bonusesnoobs;run;PROCPRINTOutputIDGrossPayBonus201-92-2498520005200482-87-7945320003200330-40-7172490004900104 5-56Chapter5DataTransformationsAutomaticCharacter-to-NumericConversionSASautomaticallyconvertsacharactervaluetoanumericvaluewhenthecharactervalueisusedinanumericcontext,suchasassignmenttoanumericvariableanarithmeticoperationlogicalcomparisonwithanumericvalueafunctionthattakesnumericarguments.105TheWHEREstatementandWHERE=datasetoptiondonotperformanyautomaticconversionincomparisons.AutomaticCharacter-to-NumericConversionTheautomaticconversionusesthew.informatproducesanumericmissingvaluefromacharactervaluethatdoesnotconformtostandardnumericnotation(digitswithoptionaldecimalpointand/orleadingsignand/orE-notation).106 5.5ConvertingVariableType5-57AutomaticCharacter-to-NumericConversionCharacterNumericvaluevalue5200052000-8.96-8.96Automatic1.243E1conversion12.431,742.64.107TheINPUTFunctionTheINPUTfunctionisusedprimarilyforconvertingcharactervaluestonumericvalues.NumVarNumVar=INPUT(=INPUT(sourcesource,,informatinformat););TheINPUTfunctionreturnsthevalueproducedwhensourceisreadwithinformat.108sourcecontainstheSAScharacterexpressiontowhichyouwanttoapplyaspecificinformat.informatistheSASinformatthatyouwanttoapplytothesource.IfyouusetheINPUTfunctiontocreateavariablenotpreviouslydefined,thetypeandlengthofthevariableisdefinedbytheinformat.NoconversionmessagesarewrittentothelogbytheINPUTfunction. 5-58Chapter5DataTransformationsTheINPUTFunctiondataconversion;CVar1='32000';CVar2='32,000';CVar3='03may2008';CVar4='050308';NVar1=input(CVar1,5.);NVar2=input(CVar2,comma6.);NVar3=input(CVar3,date9.);NVar4=input(CVar4,mmddyy6.);run;proccontentsdata=conversion;run;109TheINPUTFunctionPartialPROCCONTENTSOutput----AlphabeticListofVariablesandAttributes----#VariableTypeLenPosƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ1CVar1Char5322CVar2Char6373CVar3Char9434CVar4Char6525NVar1Num806NVar2Num887NVar3Num8168NVar4Num824110TheINPUTFunctionprocprintdata=conversionnoobs;run;PROCPRINTOutputCVar1CVar2CVar3CVar4NVar13200032,00003may200805030832000NVar2NVar3NVar4320001765517655111 5.5ConvertingVariableType5-59ExplicitCharacter-to-NumericConversionThevaluesofthevariableGrosspayintheSASdatasetprog2.salary2containcommas.Attempttouseautomaticconversiontocomputeatenpercentbonus.prog2.salary2IDGrossPay$11$6201-92-249852,000482-87-794532,000330-40-717249,000112ExplicitCharacter-to-NumericConversiondatabonuses;setprog2.salary2;Bonus=.10*GrossPay;run;procprintdata=bonuses;run;PROCPRINTOutputIDGrossPayBonus201-92-249852,000.482-87-794532,000.330-40-717249,000.113ExplicitCharacter-to-NumericConversiondatabonuses;setprog2.salary2;Bonus=.10*input(GrossPay,comma6.);run;procprintdata=bonuses;run;PROCPRINTOutputIDGrossPayBonus201-92-249852,0005200482-87-794532,0003200330-40-717249,0004900c05s5d2.sas114 5-60Chapter5DataTransformationsDataConversionproccontentsdata=bonuses;run;PartialPROCCONTENTSOutput----AlphabeticListofVariablesandAttributes----#VariableTypeLenPosƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ3BonusNum802GrossPayChar6191IDChar118HowcanyouconvertGrosspaytoanumericvariablewiththesamename?115ThevaluesoftheGrosspayvariablewereexplicitlyconvertedtonumericvaluestocreatetheBonusvariable.However,Grosspayremainsacharactervariable.DataConversionYoucannotconvertdatabyassigningtheconvertedvariablevaluetoavariablewiththesamename.GrossPay=input(GrossPay,comma6.);ThisassignmentstatementThisassignmentstatementdoesdoesnotchangenotchangeGrosspayGrosspayfromacharactervariablefromacharactervariabletoanumericvariable.toanumericvariable.116 5.5ConvertingVariableType5-61DataConversionOntheleftsideoftheassignmentstatement,youwantGrosspaytobenumeric.However,ontherightsideoftheassignmentstatement,Grosspayischaracter.GrossPay=input(GrossPay,comma6.);AvariableischaracterorAvariableischaracterornumeric.Afterthevariabletypenumeric.Afterthevariabletypeisestablished,itcannotbeisestablished,itcannotbechanged.changed.117DataConversionFirst,usetheRENAME=datasetoptiontorenamethevariableyouwanttoconvert.SAS-data-setSAS-data-set(RENAME=((RENAME=(old-nameold-name==new-namenew-name))))databonuses;setprog2.salary2(rename=(GrossPay=CharGross));run;118old-namespecifiesthevariableyouwanttorename.new-namespecifiesthenewnameofthevariable.ItmustbeavalidSASname.Thenewnameofthevariableyouwanttoconvertisarbitrary.Inthisexample,theexistingvariableisrenamedCharGrosstoemphasizethatacharactervariableisbeingconverted.Torenamemorethanonevariablefromthesamedataset,separatethevariablesyouwanttorenamewithaspace.Forexample,torenamenotonlyGrossPay,butalsoID,usethefollowingstatement.setprog2.salary2(rename=(GrossPay=CharGrossID=IDNum)); 5-62Chapter5DataTransformationsDataConversionSecond,usetheINPUTfunctioninanassignmentstatementtocreateanewvariablewhosenameistheoriginalnameofthevariableyourenamedpreviously.databonuses;setprog2.salary2(rename=(GrossPay=CharGross));GrossPay=input(CharGross,comma6.);run;119DataConversionThird,useaDROP=datasetoptionintheDATAstatementtoexcludetheoriginalvariablefromtheoutputSASdataset.databonuses(drop=CharGross);setprog2.salary2(rename=(GrossPay=CharGross));GrossPay=input(CharGross,comma6.);Bonus=.10*GrossPay;run;c05s5d3.sas120DataConversiondatabonuses(drop=CharGross);setprog2.salary2(rename=(GrossPay=CharGross));GrossPay=input(CharGross,comma6.);Bonus=.10*GrossPay;run;PDVIDCHARGROSSGROSSPAYBONUS$$NND4688125...... 5.5ConvertingVariableType5-63ConvertingCharacterDatestoSASDatesprog2.bornNameDate$12$7Ruth,G.H.13apr72Delgado,Ed25aug68Overby,Phil08jun71databirth(drop=Date);setprog2.born;Birthday=input(Date,date7.);Age=int(yrdif(Birthday,'3may2008'd,'ACT/ACT'));run;Howcanyoualterthisprogramtocomputeeachperson’sagebasedontoday'sdate?c05s5d4.sas126ConvertingCharacterDatestoSASDatesprocprintdata=birthnoobs;run;PROCPRINTOutputNameBirthdayAgeRuth,G.H.448636Delgado,Ed315939Overby,Phil417636127AutomaticNumeric-to-CharacterConversionTheprog2.phonesdatasetcontainsanumericvariableCode(areacode)andacharactervariableTelephone(telephonenumber).Createacharactervariablethatcontainstheareacodeinparenthesesfollowedbythetelephonenumber.128 5-64Chapter5DataTransformationsAutomaticNumeric-to-CharacterConversionprog2.phonesCodeTelephone8$8303393-0956919770-8292301449-5239dataphonenumbers;setprog2.phones;Phone='('!!Code!!')'!!Telephone;run;c05s5d5.sas129WhatwillhappenwhenthenumericvariableCodeisusedinacharacterexpression?AutomaticNumeric-to-CharacterConversionPartialLog13dataphonenumbers;14setprog2.phones;15Phone='('!!Code!!')'!!Telephone;16run;NOTE:Numericvalueshavebeenconvertedtocharactervaluesattheplacesgivenby:(Line):(Column).15:17NOTE:ThedatasetWORK.PHONENUMBERShas3observationsand3variables.130 5.5ConvertingVariableType5-65AutomaticNumeric-to-CharacterConversionprocprintdata=phonenumbersnoobs;run;PROCPRINTOutputCodeTelephonePhone303393-0956(303)393-0956919770-8292(919)770-8292301449-5239(301)449-5239131AutomaticNumeric-to-CharacterConversionSASautomaticallyconvertsanumericvaluetoacharactervaluewhenthenumericvalueisusedinacharactercontext,suchasassignmenttoacharactervariableaconcatenationoperationafunctionthatacceptscharacterarguments.132TheWHEREstatementandWHERE=datasetoptiondonotperformanyautomaticconversionincomparisons. 5-66Chapter5DataTransformationsAutomaticNumeric-to-CharacterConversionTheautomaticconversionusestheBEST12.formatright-alignstheresultingcharactervalue.NumericCharactervalue:value:8bytes12bytes303Automaticconversion3039leadingblanks133AutomaticNumeric-to-CharacterConversiondataphonenumbers;setprog2.phones;Phone='('!!Code!!')'!!Telephone;run;PHONE$23(303)393-09569leadingblanks134 5.5ConvertingVariableType5-67ThePUTFunctionThePUTfunctionwritesvalueswithaspecificformat.CharVarCharVar=PUT(=PUT(sourcesource,,formatformat););ThePUTfunctionreturnsthevalueproducedwhensourceiswrittenwithformat.135sourceidentifiestheSASvariableorconstantwhosevalueyouwanttoreformat.Thisargumentcanbecharacterornumeric.formatcontainstheSASformatthatyouwantappliedtothevariableorconstantthatisspecifiedinthesource.Itmustagreewiththesourceintype.ThePUTfunctionalwaysreturnsacharacterstring.Numericformatsright-aligntheresults.Characterformatsleft-aligntheresults.IfyouusethePUTfunctiontocreateavariablenotpreviouslydefined,itcreatesacharactervariablewithalengthequaltotheformatwidth.NoconversionmessagesarewrittentothelogbythePUTfunction.ThePUTFunctiondataconversion;NVar1=614;NVar2=55000;NVar3=366;CVar1=put(NVar1,3.);CVar2=put(NVar2,dollar7.);CVar3=put(NVar3,date9.);run;proccontentsdata=conversionvarnum;run;TheVARNUMoptioninthePROCCONTENTSstatementprintsalistofthevariablesbytheirlogicalpositioninthedataset.136 5-68Chapter5DataTransformationsThePUTFunctionPartialPROCCONTENTSOutput-----VariablesOrderedbyPosition-----#VariableTypeLenƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ1NVar1Num82NVar2Num83NVar3Num84CVar1Char35CVar2Char76CVar3Char9137ThePUTFunctionprocprintdata=conversionnoobs;run;PROCPRINTOutputNVar1NVar2NVar3CVar1CVar2CVar361455000366614$55,00001JAN1961138ExplicitNumeric-to-CharacterConversiondataphonenumbers;setprog2.phones;Phone='('!!put(Code,3.)!!')'!!Telephone;run;PartialLog20dataphonenumbers;21setprog2.phone;22Phone='('!!put(Code,3.)!!')'!!Telephone;23run;NOTE:ThedatasetWORK.PHONENUMBERShas3observationsand3variables.c05s5d6.sas139 5.5ConvertingVariableType5-69AutomaticNumeric-to-CharacterConversionprocprintdata=phonenumbersnoobs;run;PROCPRINTOutputCodeTelephonePhone303393-0956(303)393-0956919770-8292(919)770-8292301449-5239(301)449-5239140 5-70Chapter5DataTransformationsExercises11.ConvertingVariableTypeThedatasetprog2.studentscontainsinformationaboutstudents.PartialPROCCONTENTSOutputofprog2.students-----AlphabeticListofVariablesandAttributes-----#VariableTypeLenPosƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ3DOBChar9192NumberNum801SSNChar118PartialListingofprog2.studentsSSNNumberDOB012-40-4928546788705DEC1968012-83-3816688832103MAY1965341-44-0781941812323NOV1972423-01-7721783919128JUN1967448-23-8111942812230NOV1960Createanewdatasetnamedstudentsfromprog2.students.CreateanewcharactervariableTelephonethathasthispattern:XXX-XXXX,whereXXXXXXXisthevalueofNumber.Printthestudentsdatasetandlistallthevariablestoverifythedataconversion.RecallthepreviousprogramandalterittocreateanewnumericvariableBirthdayfromtheDOBvariable.BirthdayshouldcontainSASdatevaluesandhaveaformatofMMDDYY10.Printthestudentsdatasetandlistallthevariablestoverifythedataconversion.Whenyouareconfidentthatbothvariableswereconvertedcorrectly,useaDROP=orKEEP=datasetoptiontoensurethattheonlyvariablesinthestudentsdatasetareSSN,Telephone,andBirthday.Printyourdatasettoverifyyourresults.PartialListingofstudentsObsSSNTelephoneBirthday1012-40-4928546-788712/05/19682012-83-3816688-832105/03/19653341-44-0781941-812311/23/19724423-01-7721783-919106/28/19675448-23-8111942-812211/30/1960 5.6SolutionstoExercises5-715.6SolutionstoExercises1.ManipulatingCharacterValuesdataseparate(drop=FMnames);lengthFMnamesFirstMILast$30;setprog2.people;/*Lastnameiseverythingbeforethecomma.Everythingafterthecommaisfirstnameandmiddleinitial.Firstnameisfollowedbyablank.Middleinitialisprecededbyablank.*/FMnames=left(scan(Name,2,','));First=scan(FMnames,1,'');MI=left(scan(FMnames,2,''));Last=left(scan(Name,1,','));run;procprintdata=separate;varNameCityStateFirstMILast;run;2.CombiningCharacterValuesdataflname(keep=NewNameCityState);lengthFMnamesFirstMILast$30;setprog2.people;FMnames=left(scan(Name,2,','));First=scan(FMnames,1,'');MI=left(scan(FMnames,2,''));Last=left(scan(Name,1,','));/*Puttogetherjustthefirstnameandthelastname.*/NewName=trim(First)!!''!!Last;run;procprintdata=flname;varNewNameCityState;run; 5-72Chapter5DataTransformations®Alternatesolution(SAS9):dataflname(keep=NewNameCityState);lengthFMnamesFirstMILast$30;setprog2.people;FMnames=left(scan(Name,2,','));First=scan(FMnames,1,'');MI=left(scan(FMnames,2,''));Last=left(scan(Name,1,','));NewName=catx('',First,Last);run;procprintdata=flname;varNewNameCityState;run;3.PerformingAdditionalCharacterManipulations(Optional)datainit(drop=FirstMILastFMNames);lengthInitials$3LastFMNamesFirstMI$30;setprog2.people;FMNames=scan(Name,2,',');First=scan(FMNames,1,'');MI=scan(FMNames,2,'');Last=scan(Name,1,',');/*Puttogetherjustthefirstletters*/Initials=substr(First,1,1)!!substr(MI,1,1)!!substr(Last,1,1);run;procprintdata=init;varNameCityStateinitials;run;EachvalueofNamecontainsamiddleinitial.TheassignmentstatementthatcreatesINITIALScanbealteredifsomevaluesofNamecontainamiddleinitial,andothervaluesofNamedonotcontainamiddleinitial.TheremainderoftheDATAstepdoesnotneedtobechanged.Initials=FI!!trimn(MI)!!LI; 5.6SolutionstoExercises5-73TheTRIMNfunctionreturnsanullstring(zeroblanks)forablankstring.TheTRIMfunctionreturnsasingleblank.®Alternatesolution(SAS9):datainit(drop=FirstMILastFMNames);lengthInitials$3LastFirstMI$1FMNames$30;setprog2.people;FMNames=scan(Name,2,',');/*Byassigningentirenamesinto1-bytecharactervariables,everythingistruncatedexceptthefirstletter.*/First=left(FMNames);MI=scan(FMNames,2,'');Last=Name;/*TheCATfunctionconcatenateswithouttrimmingorinsertingseparators.*/Initials=cat(First,MI,Last);run;procprintdata=init;varNameCityStateinitials;run;4.SearchingforaCharacterValuedataprairie;setprog2.people;/*SecondargumenttoINDEXfunctionmustincludea®leadingblanktoavoidextraneousresults.TheSAS9FINDfunctioncanalsobeusedhere.*/ifindex(CityState,'IL')>0;run;procprintdata=prairie;run; 5-74Chapter5DataTransformations5.PerformingAdditionalCharacterManipulationsdatamixedprairie(drop=FirstMILastFMNames);lengthLastFMNamesFirstMI$30;setprairie;Last=scan(Name,1,',');FMnames=scan(Name,2,',');First=scan(FMnames,1,'');MI=scan(FMnames,2,'');substr(First,2)=lowcase(substr(First,2));substr(Last,2)=lowcase(substr(Last,2));Name=trim(Last)!!','!!trim(First)!!''!!MI;run;procprintdata=mixedprairie;run;®Alternatesolution(SAS9):datamixedprairie;setprairie;Name=propcase(Name);run;procprintdata=mixedprairie;run;6.UsingAdditionalCharacterFunctionsdatastatelong(keep=NameStateName);lengthStateNameState$20;setprog2.people;/*SecondwordofCITYSTATEisextracted.ResultingsecondwordofCITYSTATEisleft-alignedtoeliminateleadingblank.Oncetheleadingblankisremoved,STNAMELcanbeusedtodeterminestatename.*/State=left(scan(CityState,2,','));StateName=stnamel(State);run;procprintdata=statelong;varNameStateName;run; 5.6SolutionstoExercises5-757.PerformingAdditionalCharacterManipulations(Optional)datamixedall(keep=NameCityState);lengthFMNamesMNameFNameLName$30;setprog2.people;/*TheentirevalueofNameistransformedintolowercaselettersbecause,inyourfinalresults,mostofthelettersinthevalueofNamearelowercase.*/Name=lowcase(Name);/*Extractthelastname,andplaceitsfirstcharacterbackintouppercase.*/LName=scan(Name,1,',');substr(LName,1,1)=upcase(substr(LName,1,1));/*UsetheINDEXfunctiontosearchforablankwithinthevalueofLName.Ifablankisfound,uppercasethecharacteronepositiontoitsright.Thisisthefirstcharacterofthesecondwordofamultiple-wordlastname.*/BlankPos=index(LName,'');ifBlankPosgt0thensubstr(LName,BlankPos+1,1)=upcase(substr(LName,BlankPos+1,1));/*Extractthefirstandmiddlenames,andplacetheirfirstcharactersbackintouppercase.*/FMNames=left(scan(Name,2,','));FName=scan(FMNames,1,'');MName=scan(FMNames,2,'');substr(FName,1,1)=upcase(substr(FName,1,1));substr(MName,1,1)=upcase(substr(MName,1,1));/*UsetheINDEXfunctiontosearchforahyphenwithinthevalueofLName.Ifahyphenisfound,uppercasethecharacteronepositiontoitsright.Thisisthefirstcharacterofthesecondwordofamultiple-wordlastname.*/DashPos=index(FName,'-');ifDashPosgt0thensubstr(FName,DashPos+1,1)=upcase(substr(FName,DashPos+1,1));Name=trim(LName)!!','!!trim(FName)!!''!!MName;run;procprintdata=mixedall;run; 5-76Chapter5DataTransformationsAlternatesolution(SAS9):datamixedall;setprog2.people;Name=propcase(Name);run;procprintdata=mixedall;run;8.ManipulatingNumericValuesdatafinal;setprog2.grade;Overall=round(mean(Test1,Test2,Test3,Final,Final));run;/*TheassignmentstatementabovecouldbereplacedwithOverall=mean(ofTest1-Test3,Final,Final);*/procprintdata=final;run;9.PerformingAdditionalNumericManipulations(Optional)datafinal(drop=OverallTotal);setprog2.grade;OverallTotal=sum(Test1,Test2,Test3,Final,Final)-min(Test1,Test2,Test3);Overall=round(OverallTotal/4);run;/*ThefirstassignmentstatementabovecouldbereplacedwithOverallTotal=sum(ofTest1-Test3,Final,Final)-min(ofTest1-Test3);*/procprintdata=final;run; 5.6SolutionstoExercises5-7710.ManipulatingNumericValuesBasedonDatesdataemphire(keep=IDHiredYears);setprog2.noday;Hired=mdy(HiredMonth,15,HiredYear);/*TheFLOORfunctioncouldbeusedinthefollowingassignmentstatement:*/Years=int(yrdif(Hired,today(),'act/act'));formatHireddate9.;run;procprintdata=emphire;run;11.ConvertingVariableTypedatastudents(drop=NumberDOB);setprog2.students;/*ThePUTfunctionisusedtoconvertNUMBERfromnumerictocharacter,andthentheresultingcharactervalueismanipulatedwiththeSUBSTRfunctiontoextractthefirstthreecharacters,andthelastfourcharacters.*/Telephone=substr(put(Number,7.),1,3)!!'-'!!substr(put(Number,7.),4);/*TheINPUTfunctionisusedtoconvertDOBfromcharactertonumeric.Becausethecharactervaluesareintheform,ddMMMyyyy,theDATE9.formatisusedintheconversion.*/Birthday=input(DOB,date9.);formatBirthdaymmddyy10.;run;procprintdata=students;run; 5-78Chapter5DataTransformations Chapter6DebuggingTechniques(Self-Study)6.1UsingthePUTStatement...............................................................................................6-36.2UsingtheDEBUGOption.............................................................................................6-14 6-2Chapter6DebuggingTechniques(Self-Study) 6.1UsingthePUTStatement6-36.1UsingthePUTStatementObjectivesUsethePUTstatementintheDATAsteptohelpidentifylogicproblems.3ScenarioYoutookanewpositioninthe??company.Yourpredecessorwrotesomecodethatwasnotworking?atthetimeheleft.Youmustidentifywhattheprogramcodeiscurrentlydoinganddeterminewheretheproblemis.4 6-4Chapter6DebuggingTechniques(Self-Study)InputDataCityCountryStateAuckland,NewZealandKansasCity,USAMissouriCanberra,AustraliaAustralianCapitalAthens(Athinai),GreeceAmsterdam,NetherlandsAnchorage,USAAlaskaBirmingham,USAAlabamaBangkok,ThailandNashville,USATennesseeBoston,USAMassachusetts5ExpectedResultsTrueLocationAuckland,NewZealandKansasCity,MissouriCanberra,AustraliaAthens(Athinai),GreeceAmsterdam,NetherlandsAnchorage,AlaskaBirmingham,AlabamaBangkok,ThailandNashville,TennesseeBoston,Massachusetts6CurrentProgramdatawork.agents2;setprog2.agents;lengthCountry$20;Country=scan(CityCountry,2,',');ifCountry='USA'thenTrueLocation=scan(CityCountry,1,',')!!','!!State;else/*notUSA*/TrueLocation=CityCountry;run;7 6.1UsingthePUTStatement6-5CurrentResultsTrueLocationAuckland,NewZealandKansasCity,USACanberra,AustraliaAthens(Athinai),GreeceAmsterdam,NetherlandsAnchorage,USABirmingham,USABangkok,ThailandNashville,USABoston,USA8SyntaxErrorsVersusLogicErrorsAsyntaxerroroccurswhenprogramstatementsdonotconformtotherulesoftheSASlanguage.AnerrormessageisproducedbytheSASSystemandwrittentothelog.Alogicerroroccurswhentheprogramstatementsfollowtherules,buttheresultsarenotcorrect.Thissectionfocusesonlogicerrors.9Becauselogicerrorsdonotproducenotesinthelog,theyareoftendifficulttodetectandcorrect.ThePUTstatementandtheSASdebugger(discussedinthenextsection)aretwomethodsfordetectinglogicerrors. 6-6Chapter6DebuggingTechniques(Self-Study)ThePUTStatementIfyoudonotspecifyaFILEstatement,thePUTstatementwritesinformationtothelog.Thisisusefultodeterminewhichpieceofcodeisexecutingwhichpieceofcodeisnotexecutingthecurrentvalueofaparticularvariablethecurrentvaluesofallvariables.10GeneralFormsofthePUTStatementPUTPUT'text'text';';writestheliteraltextstring.Example:put'Iamhere.';writestothelog.Iamhere.11GeneralFormsofthePUTStatementPUTPUTvariable-namevariable-name=;=;writesthenameofthevariablefollowedbyanequalsignandthevalue.Example:IfthevalueofthevariableVaris5,thestatementputVar=;writesVar=5tothelog.12 6.1UsingthePUTStatement6-7GeneralFormsofthePUTStatementPUTPUTvariable-nameformat-namevariable-nameformat-name.;.;writesthevariablevaluewiththeindicatedformat.Example:IfthevalueofthevariableChVarisTHISwithaleadingspace,thestatementputChVar$quote20.;writestothelog."THIS"13Theformat$QUOTEw.writesacharactervaluewithquotesarounditandpreservesanyleadingspaces.GeneralFormsofthePUTStatementPUTPUT_ALL_;_ALL_;writesthenameofeachvariableinthePDVfollowedbyanequalsignandthevalueofthevariable.14ThePUTstatementcanbeusedinSASinboththebatchandinteractivemodes. 6-8Chapter6DebuggingTechniques(Self-Study)ThePUTLOGStatementThePUTLOGstatementissimilartothePUTstatement,exceptthatitwritesonlytothelog.ItisunaffectedbytheFILEstatement.PUTLOGPUTLOGmessagemessage;;wheremessagecanincludecharacterliterals(enclosedinquotationmarks),variablenames,formats,andpointercontrols.ThePUTLOGstatementisnewinSAS®9.15TheadvantageofthePUTLOGstatementisthatitcanbeinsertedinaDATAstepwhichhasPUTandFILEstatementswritingtoanon-logdestination(suchasadiskfile)withoutaffectingthatlogic. 6.1UsingthePUTStatement6-9DeterminingLogicErrorsPrograms:c06s1d1.sas,c06s1d2.sasThisdemonstrationshowshowtodetectandcorrectlogicerrorsusingthePUTstatement.datawork.agents2;setprog2.agents;lengthCountry$20TrueLocation$40;Country=scan(CityCountry,2,',');ifcountry='USA'thenTrueLocation=scan(CityCountry,1,',')!!','!!State;else/*notUSA*/TrueLocation=CityCountry;run;procprintdata=work.agents2noobs;varTrueLocationCityCountryState;title'CurrentOutputfromProgram';run;PROCPRINTOutputCurrentOutputfromProgramCityCountryTrueLocationStateAuckland,NewZealandAuckland,NewZealandKansasCity,USAKansasCity,USAMissouriCanberra,AustraliaCanberra,AustraliaAustralianCapitalAthens(Athinai),GreeceAthens(Athinai),GreeceAmsterdam,NetherlandsAmsterdam,NetherlandsAnchorage,USAAnchorage,USAAlaskaBirmingham,USABirmingham,USAAlabamaBangkok,ThailandBangkok,ThailandNashville,USANashville,USATennesseeBoston,USABoston,USAMassachusettsBringthecodeintotheEditorwindow. 6-10Chapter6DebuggingTechniques(Self-Study)1.Determinewhatcodeisexecuting.ConverttheIF-THENstatementthatcreatesTrueLocationforUSAbranches®intoaDOgroup,andinsertaPUT(orPUTLOG–SAS9)statementtodeterminewhetherthecodeisexecuting.datawork.agents2;setprog2.agents;lengthCountry$20TrueLocation$40;Country=scan(CityCountry,2,',');ifCountry='USA'thendo;TrueLocation=scan(CityCountry,1,',')!!','!!State;put'CountryisUSA';end;else/*notUSA*/TrueLocation=CityCountry;run;Submitthecode.ThetextstringinthePUTstatementdoesnotappearinthelog.2.DeterminethevalueofCountrybeforetheIF-THENstatement.InsertaPUTstatementbetweentheassignmentstatementforCountryandtheIF-THENstatementthatcreatesTrueLocationforUSAbranches.datawork.agents2;setprog2.agents;lengthCountry$20TrueLocation$40;Country=scan(CityCountry,2,',');putCountry=;ifCountry='USA'thendo;TrueLocation=scan(CityCountry,1,',')!!','!!State;put'CountryisUSA';end;else/*notUSA*/TrueLocation=CityCountry;run;Submitthecode.ThevaluesofCountryseemtobecreatedappropriately.PartialLogCountry=NewZealandCountry=USACountry=AustraliaCountry=GreeceCountry=NetherlandsCountry=USA 6.1UsingthePUTStatement6-113.Usethe$QUOTEw.formattocheckforleadingblanks.Bydefault,charactervaluesarewrittenwiththestandardcharacterformat$w.,wherewisthelengthofthecharactervariable.Thestandardcharacterformatleft-justifiesthevalueandremovesleadingblanks.TocheckforleadingblanksinthevalueforCountry,changethePUTstatementasshownbelow:datawork.agents2;setprog2.agents;lengthCountry$20TrueLocation$40;Country=scan(CityCountry,2,',');putCountry$quote20.;ifCountry='USA'thendo;TrueLocation=scan(CityCountry,1,',')!!','!!State;put'CountryisUSA';end;else/*notUSA*/TrueLocation=CityCountry;run;Submitthecodeandcheckthelog.Noticethateachvalueshowsoneleadingblank.PartialLog"NewZealand""USA""Australia""Greece""Netherlands""USA"4.UsetheLEFTfunctiontoremoveleadingblanksfromthevaluesofCountry.datawork.agents2;setprog2.agents;lengthCountry$20TrueLocation$40;Country=left(scan(CityCountry,2,','));putCountry$quote20.;ifCountry='USA'thendo;TrueLocation=scan(CityCountry,1,',')!!','!!State;put'CountryisUSA';end;else/*notUSA*/TrueLocation=CityCountry;run;Submitthecodeandcheckthelog.ThePUTstatementintheDOgroupwritestothelogattheappropriatetime. 6-12Chapter6DebuggingTechniques(Self-Study)PartialLog"NewZealand""USA"CountryisUSA"Australia""Greece""Netherlands""USA"CountryisUSAprocprintdata=work.agents2noobs;varTrueLocationCityCountryState;title'OutputwithLeadingSpacesRemoved';run;PROCPRINTOutputOutputwithLeadingSpacesRemovedTrueLocationCityCountryStateAuckland,NewZealandAuckland,NewZealandKansasCity,MissouriKansasCity,USAMissouriCanberra,AustraliaCanberra,AustraliaAustralianCapitalAthens(Athinai),GreeceAthens(Athinai),GreeceAmsterdam,NetherlandsAmsterdam,NetherlandsAnchorage,AlaskaAnchorage,USAAlaskaBirmingham,AlabamaBirmingham,USAAlabamaBangkok,ThailandBangkok,ThailandNashville,TennesseeNashville,USATennesseeBoston,MassachusettsBoston,USAMassachusetts 6.1UsingthePUTStatement6-135.RemovethePUTstatementsandDOgroupsfromtheDATAstep.datawork.agents2(drop=Country);setprog2.agents;lengthCountry$20TrueLocation$40;Country=left(scan(CityCountry,2,','));ifCountry='USA'thenTrueLocation=scan(CityCountry,1,',')!!','!!State;else/*notUSA*/TrueLocation=CityCountry;run;procprintdata=work.agents2noobs;varTrueLocationCityCountryState;title'CorrectedOutput';run;Submitthecodeandcheckthelogandoutput.PROCPRINTOutputCorrectedOutputTrueLocationCityCountryStateAuckland,NewZealandAuckland,NewZealandKansasCity,MissouriKansasCity,USAMissouriCanberra,AustraliaCanberra,AustraliaAustralianCapitalAthens(Athinai),GreeceAthens(Athinai),GreeceAmsterdam,NetherlandsAmsterdam,NetherlandsAnchorage,AlaskaAnchorage,USAAlaskaBirmingham,AlabamaBirmingham,USAAlabamaBangkok,ThailandBangkok,ThailandNashville,TennesseeNashville,USATennesseeBoston,MassachusettsBoston,USAMassachusetts 6-14Chapter6DebuggingTechniques(Self-Study)6.2UsingtheDEBUGOptionObjectivesUsetheDEBUGoptionintheDATAstatementtohelpidentifylogicproblems.18ScenarioYoutookanewpositioninthecompany.Yourpredecessor??wrotesomecodethatwasnot?workingatthetimeheleft.Youmustidentifywhattheprogramcodeiscurrentlydoinganddeterminewheretheproblemis.19 6.2UsingtheDEBUGOption6-15InputDataCityCountryStateAuckland,NewZealandKansasCity,USAMissouriCanberra,AustraliaAustralianCapitalAthens(Athinai),GreeceAmsterdam,NetherlandsAnchorage,USAAlaskaBirmingham,USAAlabamaBangkok,ThailandNashville,USATennesseeBoston,USAMassachusetts20ExpectedResultsTrueLocationAuckland,NewZealandKansasCity,MissouriCanberra,AustraliaAthens(Athinai),GreeceAmsterdam,NetherlandsAnchorage,AlaskaBirmingham,AlabamaBangkok,ThailandNashville,TennesseeBoston,Massachusetts21CurrentProgramdatawork.agents2;setprog2.agents;lengthCountry$20;Country=scan(CityCountry,2,',');ifCountry='USA'thenTrueLocation=scan(CityCountry,1,',')!!','!!State;else/*notUSA*/TrueLocation=CityCountry;run;22 6-16Chapter6DebuggingTechniques(Self-Study)CurrentResultsTrueLocationAuckland,NewZealandKansasCity,USACanberra,AustraliaAthens(Athinai),GreeceAmsterdam,NetherlandsAnchorage,USABirmingham,USABangkok,ThailandNashville,USABoston,USA23TheDEBUGOptionTheDEBUGoptionisaninteractiveinterfacetotheDATAstepduringDATAstepexecution.Thisoptionisusefultodeterminewhichpieceofcodeisexecutingwhichpieceofcodeisnotexecutingthecurrentvalueofaparticularvariablewhenthevalueofavariablechanges.24TheDEBUGoptioncanbeusedonlyintheSASSystem'sinteractivemode. 6.2UsingtheDEBUGOption6-17TheDEBUGOptionGeneralformoftheDEBUGoption:DATADATAdata-set-namedata-set-name/DEBUG;/DEBUG;25DEBUGCommandsCommoncommandsusedwiththeDEBUGoption:CommandAbbreviationActionSTEPENTERkeyStepsthroughaprogramonestatementatatime.EXAMINEEvariable(s)Displaysthevalueofthevariable.WATCHWvariable(s)Suspendsexecutionwhenthevalueofthevariablechanges.LISTWATCHLWListsvariablesthatarewatched.QUITQHaltsexecutionoftheDATAstep.26TheWandEcommandsprecedethenameofthevariable,forexample:WCountryToviewthevaluesofallvariables,usethecommande_all_Youcanalsoselectthesecommandsfromthedrop-downmenuifitisturnedon. 6-18Chapter6DebuggingTechniques(Self-Study)DeterminingLogicErrorsProgram:c06s2d1.sasUsetheDEBUGoptiontodetectthelogicerrorinthefollowingprogram:datawork.agents2;setprog2.agents;lengthCountry$20TrueLocation$40;Country=scan(CityCountry,2,',');ifCountry='USA'thenTrueLocation=scan(CityCountry,1,',')!!','!!State;else/*notUSA*/TrueLocation=CityCountry;run;procprintdata=work.agents2noobs;varTrueLocationState;title'LocationsofTicketAgents';run;PROCPRINTOutputLocationsofTicketAgentsTrueLocationCityCountryStateAuckland,NewZealandAuckland,NewZealandKansasCity,USAKansasCity,USAMissouriCanberra,AustraliaCanberra,AustraliaAustralianCapitalAthens(Athinai),GreeceAthens(Athinai),GreeceAmsterdam,NetherlandsAmsterdam,NetherlandsAnchorage,USAAnchorage,USAAlaskaBirmingham,USABirmingham,USAAlabamaBangkok,ThailandBangkok,ThailandNashville,USANashville,USATennesseeBoston,USABoston,USAMassachusettsThecorrectoutputisshownbelow:LocationsofTicketAgentsTrueLocationCityCountryStateAuckland,NewZealandAuckland,NewZealandKansasCity,MissouriKansasCity,USAMissouriCanberra,AustraliaCanberra,AustraliaAustralianCapitalAthens(Athinai),GreeceAthens(Athinai),GreeceAmsterdam,NetherlandsAmsterdam,NetherlandsAnchorage,AlaskaAnchorage,USAAlaskaBirmingham,AlabamaBirmingham,USAAlabamaBangkok,ThailandBangkok,ThailandNashville,TennesseeNashville,USATennesseeBoston,MassachusettsBoston,USAMassachusetts 6.2UsingtheDEBUGOption6-191.AddtheDEBUGoptiontotheendoftheDATAstatement.datawork.agents2/debug;setprog2.agents;lengthCountry$20TrueLocation$40;Country=scan(CityCountry,2,',');ifCountry='USA'thenTrueLocation=scan(CityCountry,1,',')!!','!!State;else/*notUSA*/TrueLocation=CityCountry;run;2.SubmittheDATAstep.Thedebuggersourcehighlightsthenextstatementtobeexecuted.3.PresstheEnterkeytoexecutetheSETstatement. 6-20Chapter6DebuggingTechniques(Self-Study)4.UsetheExaminecommandtoexaminethevalueofCityCountry:ecitycountry5.PressEntertoexecutetheassignmentstatementforCountry.6.UsetheExaminecommandtoexaminethevalueofCountry:ecountry7.PressEntertochecktheconditionalstatement.8.PressEntertoexecutetheELSEstatement.9.UsetheExaminecommandtoexaminethevalueofTrueLocation:etruelocation 6.2UsingtheDEBUGOption6-2110.UsetheWatchcommandtomonitorthevaluesofTrueLocation,Country,andCityCountry:wtruelocationcountrycitycountry11.PressEnteruntilyouexecutetheSETstatementandtheassignmentstatementforCountry.CountryisnowUSA.12.PressEntertochecktheIF-THENstatements.13.Noticethechangesinthevaluesofthewatchedvariablesfromstep10. 6-22Chapter6DebuggingTechniques(Self-Study)14.UsetheExaminecommandtocheckthevalueofCountryforleadingspaces:ecountry$quote20.15.PressEnteruntilSASreachesthebottomoftheDATAstep.16.UsetheQuitcommandtohalttheDATAstep. 6.2UsingtheDEBUGOption6-2317.RemovetheDEBUGoptionfromtheDATAstepandusetheLEFTfunctiontoremovetheleadingspace.datawork.agents2(drop=CityCountryCountry);setprog2.agents;lengthCountry$20TrueLocation$40;Country=left(scan(CityCountry,2,','));ifCountry='USA'thenTrueLocation=scan(CityCountry,1,',')!!','!!State;else/*notUSA*/TrueLocation=CityCountry;run;procprintdata=work.agents2noobs;varTrueLocationCityCountryState;title'LocationsofTicketAgents';run;PROCPRINTOutputLocationsofTicketAgentsTrueLocationCityCountryStateAuckland,NewZealandAuckland,NewZealandKansasCity,MissouriKansasCity,USAMissouriCanberra,AustraliaCanberra,AustraliaAustralianCapitalAthens(Athinai),GreeceAthens(Athinai),GreeceAmsterdam,NetherlandsAmsterdam,NetherlandsAnchorage,AlaskaAnchorage,USAAlaskaBirmingham,AlabamaBirmingham,USAAlabamaBangkok,ThailandBangkok,ThailandNashville,TennesseeNashville,USATennesseeBoston,MassachusettsBoston,USAMassachusetts 6-24Chapter6DebuggingTechniques(Self-Study) Chapter7ProcessingDataIteratively7.1DOLoopProcessing......................................................................................................7-37.2SASArrayProcessing..................................................................................................7-277.3UsingSASArrays.........................................................................................................7-387.4SolutionstoExercises.................................................................................................7-53 7-2Chapter7ProcessingDataIteratively 7.1DOLoopProcessing7-37.1DOLoopProcessingObjectivesUnderstanditerativeDOloops.UseDOloopstogeneratedata.UseDOloopstoeliminateredundantcode.UseDOloopprocessingtoconditionallyexecutecode.3DOLoopProcessingStatementswithinaDOloopexecuteforaspecificnumberofiterationsoruntilaspecificconditionstopstheloop.DATAstatementSASstatementsDOstatementiteratedSASstatementsENDstatementSASstatementsRUNstatement4 7-4Chapter7ProcessingDataIterativelyDOLoopProcessingYoucanuseDOloopstoperformrepetitivecalculationsgeneratedataeliminateredundantcodeexecuteSAScodeconditionally.5RepetitiveCodingComparetheinterestforyearlyversusquarterlycompoundingona$50,000investmentmadeforoneyearat7.5percentinterest.Howmuchmoneywillapersonaccrueineachsituation?6 7.1DOLoopProcessing7-5RepetitiveCodingdatacompound;Amount=50000;Rate=.075;Yearly=Amount*Rate;Quarterly+((Quarterly+Amount)*Rate/4);Quarterly+((Quarterly+Amount)*Rate/4);Quarterly+((Quarterly+Amount)*Rate/4);Quarterly+((Quarterly+Amount)*Rate/4);run;7DATAstepsthatdonotreaddataexecuteonlyonce.RepetitiveCodingprocprintdata=compoundnoobs;run;PROCPRINTOutputAmountRateYearlyQuarterly500000.07537503856.79Whatifyouwantedtodeterminethequarterlycompoundedinterestafteraperiodof20years(80quarters)?8 7-6Chapter7ProcessingDataIterativelyDOLoopProcessingdatacompound(drop=Qtr);Amount=50000;Rate=.075;Yearly=Amount*Rate;doQtr=1to4;Quarterly+((Quarterly+Amount)*Rate/4);end;run;9Thenameoftheindexvariable,Qtr,waschosenforclarity.AnyvalidSASvariablenamecouldbeused.TheIterativeDOStatementTheiterativeDOstatementexecutesstatementsbetweenDOandENDstatementsrepetitively,basedonthevalueofanindexvariable.DODOindex-variableindex-variable==specification-1specification-1<,…<,…specification-nspecification-n>;>;<>END;END;specification-1…specification-ncanrepresentarangeofvaluesoralistofspecificvalues.10index-variablenamesavariablewhosevaluegovernsexecutionoftheDOloop.Theindex-variableargumentisrequired.specificationdenotesanexpressionoraseriesofexpressions.TheiterativeDOstatementrequiresatleastonespecificationargument.Theindexvariable,unlessdropped,isincludedinthedatasetthatisbeingcreated.AvoidchangingthevalueoftheindexvariablewithintheDOloop.IfyoumodifythevalueoftheindexvariablewithintheDOloop,youmaycauseinfinitelooping. 7.1DOLoopProcessing7-7TheIterativeDOStatementDODOindex-variableindex-variable==startstartTOTOstopstop;>;Thevaluesofstart,stop,andincrementmustbenumbersorexpressionsthatyieldnumbersareestablishedbeforeexecutingtheloop.AnychangestothevaluesofstoporincrementmadewithintheDOloopdonotaffectthenumberofiterations.11startspecifiestheinitialvalueoftheindexvariable.stopspecifiestheendingvalueoftheindexvariable.incrementoptionallyspecifiesapositiveornegativenumbertocontroltheincrementingofindex-variable.Ifincrementisnotspecified,theindexvariableisincreasedby1.Whenincrementispositive,startmustbethelowerboundandstop,ifpresent,mustbetheupperboundfortheloop.Ifincrementisnegative,startmustbetheupperboundandstop,ifpresent,mustbethelowerboundfortheloop.TheIterativeDOStatementWhatarethevaluesofeachofthefourindexvariables?doi=1to12;Outofrange12345678910111213doj=2to10by2;Outofrange24681012dok=14to2by–2;Outofrange14121086420dom=3.6to3.8by.05;Outofrange3.603.653.703.753.803.8512...... 7-8Chapter7ProcessingDataIterativelyTheIterativeDOStatementDODOindex-variableindex-variable==item-1…item-n>;;item-1throughitem-ncanbeeitherallnumericorallcharacterconstants,ortheycanbevariables.TheDOloopisexecutedonceforeachvalueinthelist.13Enclosecharacterconstantsinquotationmarks.TheIterativeDOStatementHowmanytimeswilleachDOloopexecute?doMonth='JAN','FEB','MAR';3times.doFib=1,2,3,5,8,13,21;7times.doi=Var1,Var2,Var3;3times.doj=BeginDatetoToday()by7;Unknown.ThenumberofiterationsdependsonthevaluesofBeginDateandToday().dok=Test1-Test50;1time.AsinglevalueofkisdeterminedbysubtractingTest50fromTest1.14...... 7.1DOLoopProcessing7-9DOLoopLogicPerformingRepetitiveCalculationsOnJanuary1ofeachyear,$5,000isinvestedinanaccount.Determinethevalueoftheaccountafterthreeyearsbasedonaconstantannualinterestrateof7.5percent.datainvest;doYear=2001to2003;Capital+5000;Capital+(Capital*.075);end;run;19 7-10Chapter7ProcessingDataIterativelyExecutedatainvest;doYear=2001to2003;Capital+5000;Capital+(Capital*.075);end;run;PDVDYEARCAPITAL_N_20...datainvest;doYear=2001to2003;Capital+5000;Capital+(Capital*.075);end;run;PDVDYEARCAPITAL_N_20010122...datainvest;doYear=2001to2003;Capital+5000;Capital+(Capital*.075);end;run;PDV0+5000DYEARCAPITAL_N_20015000123... 7.1DOLoopProcessing7-11datainvest;doYear=2001to2003;Capital+5000;Capital+(Capital*.075);end;run;PDV5000+(5000*.075)DYEARCAPITAL_N_20015375124......datainvest;doYear=2001to2003;Capital+5000;Capital+(Capital*.075);end;run;Year+1PDVDYEARCAPITAL_N_20025375125......datainvest;doYear=2001to2003;IsYearCapital+5000;outofCapital+(Capital*.075);end;range?run;PDVDYEARCAPITAL_N_20025375126...... 7-12Chapter7ProcessingDataIterativelydatainvest;doYear=2001to2003;Capital+5000;Capital+(Capital*.075);end;run;PDV5375+5000DYEARCAPITAL_N_200210375127...datainvest;doYear=2001to2003;Capital+5000;Capital+(Capital*.075);end;run;Year+1PDVDYEARCAPITAL_N_200417364.61133......datainvest;doYear=2001to2003;IsYearCapital+5000;outofCapital+(Capital*.075);end;range?run;PDVDYEARCAPITAL_N_200417364.61134...... 7.1DOLoopProcessing7-13datainvest;doYear=2001to2003;Capital+5000;Capital+(Capital*.075);end;run;PDVDYEARCAPITAL_N_200417364.61135......datainvest;doYear=2001to2003;Capital+5000;Capital+(Capital*.075);end;run;ImpliedoutputPDVDYEARCAPITAL_N_200417364.611Writeoutobservationtoinvest.36......PerformingRepetitiveCalculationsprocprintdata=investnoobs;run;PROCPRINTOutputYearCapital200417364.6137 7-14Chapter7ProcessingDataIterativelyPerformingRepetitiveCalculationsGenerateaseparateobservationforeachyear.datainvest;doYear=2001to2003;Capital+5000;Capital+(Capital*.075);output;end;run;procprintdata=investnoobs;run;c07s1d1.sas38PerformingRepetitiveCalculationsPROCPRINTOutputYearCapital20015375.00200211153.13200317364.61WhyisthevalueofYearnotequalto2004inthelastobservation?39TheexplicitOUTPUTstatementwithintheDOloopwritesoneobservationforeachofthethreeiterationsoftheDOloop.Inthepreviousexample,implicitoutputwroteonlyoneobservation.ThefinalvalueofCapitalisidenticalregardlessofhowmanyobservationsareoutput. 7.1DOLoopProcessing7-15ReducingRedundantCodeRecalltheexamplethatforecaststhegrowthofeachdivisionofanairline.PartialListingofprog2.growthNumDivisionEmpsIncreaseAPTOPS2050.075FINACE1980.040FLTOPS1870.08040AForecastingApplication(Review)dataforecast;setprog2.growth(rename=(NumEmps=NewTotal));Year=1;NewTotal=NewTotal*(1+Increase);output;Year=2;NewTotal=NewTotal*(1+Increase);output;Year=3;NewTotal=NewTotal*(1+Increase);output;run;Whatifyouwanttoforecastgrowthoverthenext30years?41ThisprogramdiffersslightlyfromtheprogramintroducedinChapter2.ARENAME=datasetoptionwasaddedintheSETstatement.Asaresult,thethreeassignmentstatementsthatassignvaluestoNewTotalareidentical.Therefore,aDROPstatementisnolongernecessary. 7-16Chapter7ProcessingDataIterativelyReducingRedundantCodeUseaDOlooptoeliminatetheredundantcodeinthepreviousexample.dataforecast;setprog2.growth(rename=(NumEmps=NewTotal));doYear=1to3;NewTotal=NewTotal*(1+Increase);output;end;run;c07s1d2.sas42Growthoverthenext30yearscouldbeforecastbychangingtheiterativeDOstatement:doYear=1to30;ReducingRedundantCodeprocprintdata=forecastnoobs;run;PartialPROCPRINTOutputNewDivisionTotalIncreaseYearAPTOPS220.380.0751APTOPS236.900.0752APTOPS254.670.0753FINACE205.920.0401WhatifyouwanttoforecastthenumberofyearsitwouldtakeforthesizeoftheAirportOperationsDivisiontoexceed300people?43 7.1DOLoopProcessing7-17ConditionalIterativeProcessingYoucanuseDOWHILEandDOUNTILstatementstostoptheloopwhenaconditionismetratherthanwhentheindexvariableexceedsaspecificvalue.Toavoidinfiniteloops,besurethattheconditionspecifiedwillbemet.44TheDOWHILEStatementTheDOWHILEstatementexecutesstatementsinaDOloopwhileaconditionistrue.DOWHILEDOWHILE((expressionexpression););<>ENDEND;;expressionisevaluatedatthetopoftheloop.Thestatementsintheloopneverexecuteiftheexpressionisinitiallyfalse.45TheDOUNTILStatementTheDOUNTILstatementexecutesstatementsinaDOloopuntilaconditionistrue.DOUNTILDOUNTIL((expressionexpression););<>ENDEND;;expressionisevaluatedatthebottomoftheloop.Thestatementsintheloopareexecutedatleastonce.46 7-18Chapter7ProcessingDataIterativelyConditionalIterativeProcessingDeterminethenumberofyearsitwouldtakeforanaccounttoexceed$1,000,000if$5,000isinvestedannuallyat7.5percent.47ConditionalIterativeProcessingdatainvest;dountil(Capital>1000000);Year+1;Capital+5000;Capital+(Capital*.075);end;run;procprintdata=investnoobs;run;48ConditionalIterativeProcessingPROCPRINTOutputCapitalYear1047355.9138HowcouldyougeneratethesameresultwithaDOWHILEstatement?49 7.1DOLoopProcessing7-19TheIterativeDOStatementwithaConditionalClauseYoucancombineDOWHILEandDOUNTILstatementswiththeiterativeDOstatement.DODOindex-variableindex-variable==startstartTOTOstopstop>WHILEWHILE||UNTILUNTIL((expressionexpression););<>ENDEND;;ThisisonemethodofavoidinganinfiniteloopinDOWHILEorDOUNTILstatements.50InaDOWHILEstatement,theconditionalclauseischeckedaftertheindexvariableisincremented.InaDOUNTILstatement,theconditionalclauseischeckedbeforetheindexvariableisincremented.TheIterativeDOStatementwithaConditionalClauseDeterminethereturnoftheaccountagain.Stoptheloopif25yearsisreachedormorethan$250,000isaccumulated.51 7-20Chapter7ProcessingDataIterativelyTheIterativeDOStatementwithaConditionalClausedatainvest;doYear=1to25until(Capital>250000);Capital+5000;Capital+(Capital*.075);end;run;procprintdata=investnoobs;run;52TheIterativeDOStatementwithaConditionalClausePROCPRINTOutputYearCapital21255594.8653NestedDOLoopsNestedDOloopsareloopswithinloops.WhenyounestDOloops,usedifferentindexvariablesforeachloopbecertainthateachDOstatementhasacorrespondingENDstatement.54 7.1DOLoopProcessing7-21NestedDOLoopsCreateoneobservationperyearforfiveyearsandshowtheearningsifyouinvest$5,000peryearwith7.5percentannualinterestcompoundedquarterly.55NestedDOLoopsdatainvest(drop=Quarter);doYear=1to5;Capital+5000;doQuarter=1to4;5x4xCapital+(Capital*(.075/4));end;output;end;run;procprintdata=investnoobs;run;56......NestedDOLoopsPROCPRINTOutputYearCapital15385.68211186.79317435.37424165.94531415.68Howcouldyougenerateoneobservationforeachquarterlyamount?57 7-22Chapter7ProcessingDataIterativelyNestedDOLoopsComparethefinalresultsofinvesting$5,000ayearforfiveyearsinthreedifferentbanksthatcompoundquarterly.Assumeeachbankhasafixedinterestrate.prog2.BanksNameRateCalhounBankandTrust0.0718StateSavingsBank0.0721NationalSavingsandTrust0.072858NestedDOLoopsdatainvest(drop=QuarterYear);setprog2.banks;Capital=0;doYear=1to5;Capital+5000;3xdoQuarter=1to4;5x4xCapital+(Capital*(Rate/4));end;end;run;Thisprogramissimilartothepreviousprogram.Thechangesarenoted.c07s1d3.sas59......NestedDOLoopsdatainvest(drop=QuarterYear);setprog2.banks;Capital=0;doYear=1to5;Capital+5000;doQuarter=1to4;Capital+(Capital*(Rate/4));(0.0718/4));end;end;run;PartialPDVDNAMERATE_N_CalhounBankandTrust0.0718160...... 7.1DOLoopProcessing7-23NestedDOLoopsdatainvest(drop=QuarterYear);setprog2.banks;Capital=0;doYear=1to5;Capital+5000;doQuarter=1to4;Capital+(Capital*(Rate/4));(0.0721/4));end;end;run;PartialPDVDNAMERATE_N_StateSavingsBank0.0721261......NestedDOLoopsdatainvest(drop=QuarterYear);setprog2.banks;Capital=0;doYear=1to5;Capital+5000;doQuarter=1to4;Capital+(Capital*(Rate/4));(0.0728/4));end;end;run;PartialPDVDNAMERATE_N_NationalSavingsandTrust0.0728362......NestedDOLoopsprocprintdata=investnoobs;run;PROCPRINTOutputNameRateCapitalCalhounBankandTrust0.071831106.73StateSavingsBank0.072131135.55NationalSavingsandTrust0.072831202.9163 7-24Chapter7ProcessingDataIterativelyExercises1.PerformingComputationswithDOLoopsThepayrolldepartmentmustprojecttotalemployeecosts(wages,retirement,benefits,andmedicalbenefits)throughfutureyearsbasedonassumedincreases.a.CreateaSASdatasetnamedfuturewithfourvariables:Yearandthethreevariablesshownbelow.Initializeeachofthevariablesbelowtotheircurrentvalues,anduseaDOlooptocalculatetheirestimatedvaluesforthenexttenyears.Forexample,nextyear'swageexpensewillbethisyear'swageexpenseplus6percentofthisyear'samount;intwoyears,thewageexpensewillbenextyear'samountplus6percent;andsoon.Createoneobservationforeachyear.EstimatedannualVariableCurrentvalueincreaseWages$12,874,0006.0%Retire1,765,0001.4%Medical649,0009.5%UseSASdatefunctionstoguaranteethatthevalueofYearinthefirstobservationistheupcomingyear,regardlessofthecurrentyear.(Ifthecurrentyearis2001,thevalueofYearinthefirstobservationwillbe2002.Iftheprogramisrunin2006withoutanymodifications,thevalueofYearinthefirstobservationwillbe2007.)Printthedatasettoverifyyourresults.ObsYearWagesRetireMedical1200313646440.001789710.00710655.002200414465226.401814765.94778167.233200515333139.981840172.66852093.114200616253128.381865935.08933041.965200717228316.091892058.171021680.946200818262015.051918546.991118740.637200919357735.951945406.641225020.998201020519200.111972642.341341397.999201121750352.122000259.331468830.8010201223055373.252028262.961608369.72Theresultsaboveweregeneratedon3May2002.YourvaluesofYearmaydiffer. 7.1DOLoopProcessing7-25b.ModifythepreviousprogramtocreateanewvariablenamedTotCostthatisthesumofthewage,retirement,andmedicalcostsforeachyear.Printthedataset.ObsYearWagesRetireMedicalTotCost1200313646440.001789710.00710655.0016146805.002200414465226.401814765.94778167.2317058159.573200515333139.981840172.66852093.1118025405.764200616253128.381865935.08933041.9619052105.425200717228316.091892058.171021680.9420142055.206200818262015.051918546.991118740.6321299302.677200919357735.951945406.641225020.9922528163.598201020519200.111972642.341341397.9923833240.449201121750352.122000259.331468830.8025219442.2410201223055373.252028262.961608369.7226692005.93Theresultsaboveweregeneratedon3May2002.YourvaluesofYearmaydiffer. 7-26Chapter7ProcessingDataIterativelyc.Corporateincomeforlastyearwas$50,000,000.Incomeisprojectedtoincreaseat1percentperyear.ModifythepreviousprogramsothattheDOloopstopswhentheyear'stotalcostsexceedtheyear'sincome.Printthedatasettoverifythattotalcostsexceedincomeafter26observations.ObsYearIncomeTotCost1200350500000.0016146805.002200451005000.0017058159.573200551515050.0018025405.764200652030200.5019052105.425200752550502.5120142055.206200853076007.5321299302.677200953606767.6122528163.598201054142835.2823833240.449201154684263.6325219442.2410201255231106.2726692005.9311201355783417.3328256519.1312201456341251.5129918944.7513201556904664.0231685647.2914201657473710.6633563421.1315201758048447.7735559520.9116201858628932.2537681694.1417201959215221.5739938216.3018202059807373.7842337928.4919202160405447.5244890278.0120202261009502.0047605361.8921202361619597.0250493973.8122202462235792.9953567654.5723202562858150.9256838746.3024202663486732.4360320451.0325202764121599.7564026893.5626202864762815.7567973189.29Theresultsaboveweregeneratedon3May2002.YourvaluesofYearmaydiffer. 7.2SASArrayProcessing7-277.2SASArrayProcessingObjectivesUnderstandtheconceptsofSASarrays.UseSASarraystoperformrepetitivecalculations.66PerformingRepetitiveCalculationsEmployeescontributeanamounttocharityeveryquarter.TheSASdatasetprog2.donatecontainscontributiondataforeachemployee.Theemployersupplementseachcontributionby25percent.Calculateeachemployee'squarterlycontributionincludingthecompanysupplement.PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4E00224123322.E003673548403067 7-28Chapter7ProcessingDataIterativelyPerformingRepetitiveCalculationsdatacharity;setprog2.donate;Qtr1=Qtr1*1.25;Qtr2=Qtr2*1.25;Qtr3=Qtr3*1.25;Qtr4=Qtr4*1.25;run;procprintdata=charitynoobs;run;68PerformingRepetitiveCalculationsPartialPROCPRINTOutputIDQtr1Qtr2Qtr3Qtr4E0022415.0041.2527.50.E0036743.7560.0050.0037.50E00441.78.75111.25112.50E0058720.0023.7537.5036.25E005985.0010.007.501.25Whatifyouwanttosimilarlymodify52weeksofdatastoredinWeek1throughWeek52?69ArrayProcessingYoucanusearraystosimplifyprogramsthatperformrepetitivecalculationscreatemanyvariableswiththesameattributesreaddatarotateSASdatasetsbymakingvariablesintoobservationsorobservationsintovariablescomparevariablesperformatablelookup.70 7.2SASArrayProcessing7-29WhatIsaSASArray?ASASarrayisatemporarygroupingofSASvariablesthatarearrangedinaparticularorderisidentifiedbyanarraynameexistsonlyforthedurationofthecurrentDATAstepisnotavariable.71SASarraysaredifferentfromarraysinmanyotherprogramminglanguages.IntheSASSystem,anarrayisnotadatastructure.Itissimplyaconvenientwayoftemporarilyidentifyingagroupofvariables.WhatIsaSASArray?Eachvalueinanarrayiscalledanelementidentifiedbyasubscriptthatrepresentsthepositionoftheelementinthearray.Whenyouuseanarrayreference,thecorrespondingvalueissubstitutedforthereference.72 7-30Chapter7ProcessingDataIterativelyWhatIsaSASArray?ArraynameCONTRIBCONTRIBIDDQTR1QTR2QTR3QTR4FirstSecondThirdFourthelementelementelementelementCONTRIB{1}CONTRIB{2}CONTRIB{3}CONTRIB{4}Arrayreferences73...... 7.2SASArrayProcessing7-31TheARRAYStatementTheARRAYstatementdefinestheelementsinanarray.Theseelementswillbeprocessedasagroup.Yourefertoelementsofthearraybythearraynameandsubscript.ARRAYARRAYarray-namearray-name{{subscriptsubscript}<$><}<$>><<(><(initial-value-listinitial-value-list)>;)>;74array-namespecifiesthenameofthearray.{subscript}describesthenumberandarrangementofelementsinthearraybyusinganasterisk,anumber,orarangeofnumbers.subscriptisenclosedinbraces({}).Brackets([])andparentheses(())arealsoallowed.subscriptoftenhastheform{dimension-size(s)}.{dimension-size(s)}isusedtoindicateanumericrepresentationofeitherthenumberofelementsinaone-dimensionalarrayorthenumberofelementsineachdimensionofamultidimensionalarray.$indicatesthattheelementsinthearrayarecharacterelements.Thedollarsignisnotnecessaryiftheelementsinthearraywerepreviouslydefinedascharacterelements.lengthspecifiesthelengthofelementsinthearraythatwerenotpreviouslyassignedalength.array-elementsnamestheelementsthatmakeupthearray.Arrayelementscanbelistedinanyorder.(initial-value-list)givesinitialvaluesforthecorrespondingelementsinthearray.Thevaluesforelementscanbenumbersorcharacterstrings.Youmustencloseallcharacterstringsinquotationmarks.ArraynamescannotbeusedinLABEL,FORMAT,DROP,KEEP,orLENGTHstatements.Ifyouuseafunctionnameasthenameofthearray,SAStreatsparentheticalreferencesthatinvolvethenameasarrayreferences,notfunctionreferences,forthedurationoftheDATAstep. 7-32Chapter7ProcessingDataIterativelyTheARRAYStatementTheARRAYstatementmustcontainallnumericorallcharacterelementsmustbeusedtodefineanarraybeforethearraynamecanbereferencedcreatesvariablesiftheydonotalreadyexistinthePDVisacompile-timestatement.75YoucanusespecialSASnameliststoreferencevariablesthatwerepreviouslydefinedinthesameDATAstep.The_CHARACTER_variablelistscharactervaluesonly.The_NUMERIC_variablelistsnumericvaluesonly.Avoidusingthe_ALL_specialSASnamelisttoreferencevariables,becausetheelementsinanarraymustbeeitherallcharacterorallnumericvalues.DefininganArrayWriteanARRAYstatementthatdefinesthefourquarterlycontributionvariablesaselementsofanarray.arrayContrib{4}Qtr1Qtr2Qtr3Qtr4;CONTRIBCONTRIBIDQTR1QTR2QTR3QTR4FirstSecondThirdFourthelementelementelementelement76......Thefourvariables,Qtr1,Qtr2,Qtr3,andQtr4,cannowbereferencedviathearraynameContrib. 7.2SASArrayProcessing7-33DefininganArrayVariablesthatareelementsofanarrayneednothavesimilar,related,ornumberednames.arrayContrib2{4}Q1Qrtr2ThrdQQtr4;CONTRIB2CONTRIB2IDQ1QRTR2THRDQQTR4FirstSecondThirdFourthelementelementelementelement77......ProcessinganArrayArrayprocessingoftenoccurswithinDOloops.AniterativeDOloopthatprocessesanarrayhasthefollowingform:DODOindex-variableindex-variable=1=1TOTOnumber-of-elements-in-arraynumber-of-elements-in-array;;additionalSASstatementsadditionalSASstatementsusingarray-nameusingarray-name{{index-variableindex-variable}}……ENDEND;;Toexecutetheloopasmanytimesasthereareelementsinthearray,specifythatthevaluesofindex-variablerangefrom1tonumber-of-elements-in-array.78YoumusttellSASwhichvariableinthearraytouseineachiterationoftheloop.YoucanwriteprogrammingstatementssothattheindexvariableoftheDOloopisthesubscriptofthearrayreference(forexample,array-name{index-variable}).Whenthevalueoftheindexvariablechanges,thesubscriptofthearrayreference(andthereforethevariablethatisreferenced)alsochanges.Toprocessparticularelementsofanarray,specifythoseelementsastherangeoftheiterativeDOstatement.Bydefault,SASincludesindex-variableintheoutputdataset.UseaDROPstatementortheDROP=datasetoptiontopreventtheindexvariablefrombeingwrittentoyouroutputdataset. 7-34Chapter7ProcessingDataIterativelyProcessinganArrayarrayContrib{4}Qtr1Qtr2Qtr3Qtr4;doQtr=1to4;Contrib{Qtr}=Contrib{Qtr}*1.25;end;ValueofarrayindexreferenceCONTRIB{QTR}CONTRIB{QTR}variableQtr1234CONTRIB{1}CONTRIB{2}CONTRIB{3}CONTRIB{4}QTR1QTR2QTR3QTR4FirstSecondThirdFourth79elementelementelementelement...Thenameoftheindexvariable,Qtr,waschosenforclarity.AnyvalidSASvariablenamecouldbeused.PerformingRepetitiveCalculationsdatacharity(drop=Qtr);setprog2.donate;arrayContrib{4}Qtr1Qtr2Qtr3Qtr4;doQtr=1to4;Contrib{Qtr}=Contrib{Qtr}*1.25;end;run;c07s2d1.sas80...... 7.2SASArrayProcessing7-35PerformingRepetitiveCalculationsdatacharity(drop=Qtr);setprog2.donate;arrayContrib{4}Qtr1Qtr2Qtr3Qtr4;doQtr=1to4;Contrib{1}=Contrib{1}*1.25;Contrib{Qtr}=Contrib{Qtr}*1.25;end;run;WhenQtr=1Qtr1=Qtr1*1.25;81......PerformingRepetitiveCalculationsprocprintdata=charitynoobs;run;PartialPROCPRINTOutputIDQtr1Qtr2Qtr3Qtr4E0022415.0041.2527.50.E0036743.7560.0050.0037.50E00441.78.75111.25112.50E0058720.0023.7537.5036.25E005985.0010.007.501.2585 7-36Chapter7ProcessingDataIterativelyExercises2.UsingArraysforRepetitiveComputationsAskiresorthasaweather-recordingdevicethatwritesanobservationtoaSASdataseteveryday.Eachobservationinthedatasetprog2.skicontainsthedateand24hourlyreadingsofthetemperatureindegreesFahrenheitstartingat1:00a.m.PartialListingofprog2.skiDateT1T2T3T4T5T6T7T8T9T10T11T1218FEB200023222020212426282829313119FEB200025252630313333353637394020FEB200031313029292829303031303021FEB200013151617192020212324262722FEB2000202223252627293133353636T13T14T15T16T17T18T19T20T21T22T23T24323231323133323129272625404142424342414038363432302928262523222119171513293031303031302723222120373837343231302624252120Createadatasetnamedcelsiusbyreadingtheprog2.skidataset.ConvertallofthetemperaturesstoredinT1throughT24toCelsiusbyusingthisformula:Celsiustemperature=5*(Fahrenheittemperature-32)/9TheseCelsiustemperatureswillbestoredinT1throughT24.(Youdonotneedtocreate24newvariablesfortheCelsiustemperatures.)CreateavariableCostthatcontainsthedailycostofrunningasnowmakingmachineifthemachineautomaticallyrunsforonehourwhenthedetectedtemperatureislowerthan2degreesCelsius.Itcosts$125.00perhourtorunthemachine.Printthedataset.Roundthetemperaturevaluestothefirstdecimalplace. 7.2SASArrayProcessing7-37PartialPROCPRINTOutputObsDateT1T2T3T4T5T6T7118FEB2000-5.0-5.6-6.7-6.7-6.1-4.4-3.3219FEB2000-3.9-3.9-3.3-1.1-0.60.60.6320FEB2000-0.6-0.6-1.1-1.7-1.7-2.2-1.7421FEB2000-10.6-9.4-8.9-8.3-7.2-6.7-6.7522FEB2000-6.7-5.6-5.0-3.9-3.3-2.8-1.7ObsT8T9T10T11T12T13T14T15T16T171-2.2-2.2-1.7-0.6-0.60.00.0-0.60.0-0.621.72.22.83.94.44.45.05.65.66.13-1.1-1.1-0.6-1.1-1.1-1.1-1.7-2.2-3.3-3.94-6.1-5.0-4.4-3.3-2.8-1.7-1.1-0.6-1.1-1.15-0.60.61.72.22.22.83.32.81.10.0ObsT18T19T20T21T22T23T24Cost10.60.0-0.6-1.7-2.8-3.3-3.9300025.65.04.43.32.21.10.012503-5.0-5.6-6.1-7.2-8.3-9.4-10.630004-0.6-1.1-2.8-5.0-5.6-6.1-6.730005-0.6-1.1-3.3-4.4-3.9-6.1-6.72375 7-38Chapter7ProcessingDataIteratively7.3UsingSASArraysObjectivesUseSASarraystocreatenewvariables.UseSASarraystoperformatablelookup.UseSASarraystorotateaSASdataset.88CreatingVariableswithArraysCalculatethepercentagethateachquarter'scontributionrepresentsoftheemployee'stotalannualcontribution.Basethepercentageonlyontheemployee'sactualcontributionandignorethecompanycontributions.PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4E00224123322.E003673548403089 7.3UsingSASArrays7-39CreatingVariableswithArraysdatapercent(drop=Qtr);setprog2.donate;Total=sum(ofQtr1-Qtr4);arrayContrib{4}Qtr1-Qtr4;arrayPercent{4};doQtr=1to4;Percent{Qtr}=Contrib{Qtr}/Total;end;run;ThesecondARRAYstatementcreatesfournumericvariables:Percent1,Percent2,Percent3,andPercent4.c07s3d1.sas90ThefirstARRAYstatementusestheexistingvariablesQtr1,Qtr2,Qtr3,andQtr4.InthatARRAYstatement,anumberedrangeSASvariablelistisused.CreatingVariableswithArraysprocprintdata=percentnoobs;varIDPercent1-Percent4;formatPercent1-Percent4percent6.;run;PartialPROCPRINTOutputIDPercent1Percent2Percent3Percent4E0022418%49%33%.E0036723%31%26%20%E00441.26%37%37%E0058717%20%32%31%E0059821%42%32%5%91ThePERCENTw.dformatmultipliesvaluesby100,formatstheminthesamewayastheBESTw.dformat,andaddsapercentsign(%)totheendoftheformattedvalue.Negativevaluesareenclosedinparentheses.ThePERCENTw.dformatallowsroomforapercentsignandparentheses,evenifthevalueisnotnegative. 7-40Chapter7ProcessingDataIterativelyCreatingVariableswithArraysCalculatethedifferenceineachemployee'sactualcontributionfromonequartertothenext.SeconddifferencePartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4E00224123322.FirstThirdE0036735484030differencedifference92......CreatingVariableswithArraysdatachange(drop=i);setprog2.donate;arrayContrib{4}Qtr1-Qtr4;arrayDiff{3};doi=1to3;Diff{i}=Contrib{i+1}-Contrib{i};end;run;c07s3d2.sas93CreatingVariableswithArraysdatachange(drop=i);setprog2.donate;arrayContrib{4}Qtr1-Qtr4;arrayDiff{3};doi=1to3;Diff{1}=Contrib{2}-Contrib{1};Diff{i}=Contrib{i+1}-Contrib{i};end;run;Wheni=1Diff1=Qtr2-Qtr1;94...... 7.3UsingSASArrays7-41CreatingVariableswithArraysdatachange(drop=i);setprog2.donate;arrayContrib{4}Qtr1-Qtr4;arrayDiff{3};doi=1to3;Diff{3}=Contrib{4}-Contrib{3};Diff{i}=Contrib{i+1}-Contrib{i};end;run;Wheni=3Diff3=Qtr4-Qtr3;96......CreatingVariableswithArraysprocprintdata=changenoobs;varIDDiff1-Diff3;run;PartialPROCPRINTOutputIDDiff1Diff2Diff3E0022421-11.E0036713-8-10E00441.261E00587311-1E005984-2-597 7-42Chapter7ProcessingDataIterativelyAssigningInitialValuesDeterminethedifferencebetweenemployeecontributionsandlastyear'saveragequarterlygoalsof$10,$15,$5,and$10peremployee.datacompare(drop=QtrGoal1-Goal4);setprog2.donate;arrayContrib{4}Qtr1-Qtr4;arrayDiff{4};arrayGoal{4}Goal1-Goal4(10,15,5,10);doQtr=1to4;Diff{Qtr}=Contrib{Qtr}-Goal{Qtr};end;run;98Elementsandvaluesarematchedbyposition.Iftherearemorearrayelementsthaninitialvalues,theremainingarrayelementsareassignedmissingvaluesandSASissuesawarning.Youcanseparatethevaluesintheinitialvaluelistwitheitheracommaorablankspace.Initialvaluesareretaineduntilanewvalueisassignedtothearrayelement.Thisisanexampleofasimpletablelookupprogram.AssigningInitialValuesprocprintdata=comparenoobs;varIDDiff1Diff2Diff3Diff4;run;PartialPROCPRINTOutputIDDiff1Diff2Diff3Diff4E0022421817.E0036725333520E00441.488480E00587642519E00598-6-71-999 7.3UsingSASArrays7-43PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4Compiledatacompare(drop=QtrGoal1-Goal4);setprog2.donate;E00224123322.arrayContrib{4}Qtr1-Qtr4;E0036735484030arrayDiff{4};arrayGoal{4}Goal1-Goal4(10,15,5,10);doQtr=1to4;Diff{Qtr}=Contrib{Qtr}-Goal{Qtr};end;run;PDV100......PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4datacompare(drop=QtrGoal1-Goal4);setprog2.donate;E00224123322.arrayContrib{4}Qtr1-Qtr4;E0036735484030arrayDiff{4};arrayGoal{4}Goal1-Goal4(10,15,5,10);doQtr=1to4;Diff{Qtr}=Contrib{Qtr}-Goal{Qtr};end;run;PDVIDQTR1QTR2QTR3QTR4DIFF1DIFF2DIFF3DIFF4GOAL1GOAL2GOAL3GOAL4103...... 7-44Chapter7ProcessingDataIterativelyPartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4datacompare(drop=QtrGoal1-Goal4);setprog2.donate;E00224123322.arrayContrib{4}Qtr1-Qtr4;E0036735484030arrayDiff{4};arrayGoal{4}Goal1-Goal4(10,15,5,10);doQtr=1to4;Diff{Qtr}=Contrib{Qtr}-Goal{Qtr};end;run;PDVIDQTR1QTR2QTR3QTR4DIFF1DIFF2DDDDDDIFF3DIFF4GOAL1GOAL2GOAL3GOAL4QTR105......TheelementsintheGoalarray,Goal1,Goal2,Goal3,andGoal4,arecreatedinthePDV.ThesevariablesareusedtocalculatethevaluesofDiff1,Diff2,Diff3,andDiff4.ThevaluesaresubsequentlyexcludedfromtheoutputdatasetcompareusingtheDROP=datasetoptionintheDATAstatement.PerformingaTableLookupYoucanusethekeyword_TEMPORARY_insteadofspecifyingvariablenameswhenyoucreateanarraytodefinetemporaryarrayelements.datacompare(drop=Qtr);setprog2.donate;arrayContrib{4}Qtr1-Qtr4;arrayDiff{4};arrayGoal{4}_temporary_(10,15,5,10);doQtr=1to4;Diff{Qtr}=Contrib{Qtr}-Goal{Qtr};end;run;c07s3d3.sas106Arraysoftemporaryelementsareusefulwhentheonlypurposeforcreatinganarrayistoperformacalculation.Topreservetheresultofthecalculation,assignittoavariable.Temporarydataelementsdonotappearintheoutputdataset.Temporarydataelementvaluesarealwaysautomaticallyretained. 7.3UsingSASArrays7-45PerformingaTableLookupprocprintdata=comparenoobs;varIDDiff1Diff2Diff3Diff4;run;PartialPROCPRINTOutputIDDiff1Diff2Diff3Diff4E0022421817.E0036725333520E00441.488480E00587642519E00598-6-71-9107RotatingaSASDataSetRotating,ortransposing,aSASdatasetcanbeaccomplishedbyusingarrayprocessing.Whenadatasetisrotated,thevaluesofanobservationintheinputdatasetbecomevaluesofavariableintheoutputdataset.PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4E00224123322.E0036735484030108TheTRANSPOSEprocedureisalsousedtocreateanoutputdatasetbyrestructuringthevaluesinaSASdataset,transposingselectedvariablesintoobservations. 7-46Chapter7ProcessingDataIterativelyRotatingaSASDataSetPartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4E00224123322.E0036735484030PartialListingofrotateIDQtrAmountE00224112E00224233E00224322E002244.E00367135E00367248E00367340E00367430109......RotatingaSASDataSetdatarotate(drop=Qtr1-Qtr4);setprog2.donate;arrayContrib{4}Qtr1-Qtr4;doQtr=1to4;Amount=Contrib{Qtr};output;end;run;c07s3d4.sas110PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4Executedatarotate(drop=Qtr1-Qtr4);setprog2.donate;E00224123322.arrayContrib{4}Qtr1-Qtr4;E0036735484030doQtr=1to4;Amount=Contrib{Qtr};PartialListingofrotateoutput;IDQtrAmountend;run;PDVDDDDIDQTR1QTR2QTR3QTR4QTRAMOUNT111... 7.3UsingSASArrays7-47PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4datarotate(drop=Qtr1-Qtr4);setprog2.donate;E00224123322.arrayContrib{4}Qtr1-Qtr4;E0036735484030doQtr=1to4;Amount=Contrib{Qtr};PartialListingofrotateoutput;IDQtrAmountend;run;PDVDDDDIDQTR1QTR2QTR3QTR4QTRAMOUNTE00224123322.1.115......PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4datarotate(drop=Qtr1-Qtr4);setprog2.donate;E00224123322.arrayContrib{4}Qtr1-Qtr4;E0036735484030doQtr=1to4;Amount=Contrib{Qtr};PartialListingofrotateoutput;IDQtrAmountend;run;Amount=Contrib{1};PDVDDDDIDQTR1QTR2QTR3QTR4QTRAMOUNTE00224123322.112.116......PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4datarotate(drop=Qtr1-Qtr4);setprog2.donate;E00224123322.arrayContrib{4}Qtr1-Qtr4;E0036735484030doQtr=1to4;Amount=Contrib{Qtr};PartialListingofrotateoutput;IDQtrAmountend;run;E00224112PDVDDDDIDQTR1QTR2QTR3QTR4QTRAMOUNTE00224123322.112Writeoutobservationtorotate.117...... 7-48Chapter7ProcessingDataIterativelyPartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4datarotate(drop=Qtr1-Qtr4);setprog2.donate;E00224123322.arrayContrib{4}Qtr1-Qtr4;E0036735484030doQtr=1to4;Amount=Contrib{Qtr};PartialListingofrotateoutput;IDQtrAmountend;run;E00224112Amount=Contrib{2};PDVDDDDIDQTR1QTR2QTR3QTR4QTRAMOUNTE00224123322.21233118......PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4datarotate(drop=Qtr1-Qtr4);setprog2.donate;E00224123322.arrayContrib{4}Qtr1-Qtr4;E0036735484030doQtr=1to4;Amount=Contrib{Qtr};PartialListingofrotateoutput;IDQtrAmountend;run;E00224112E00224233E00224322PDVDDDDIDQTR1QTR2QTR3QTR4QTRAMOUNTE00224123322.322Writeoutobservationtorotate.121......PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4datarotate(drop=Qtr1-Qtr4);setprog2.donate;E00224123322.arrayContrib{4}Qtr1-Qtr4;E0036735484030doQtr=1to4;Amount=Contrib{Qtr};PartialListingofrotateoutput;IDQtrAmountend;run;E00224112E00224233E00224322E002244.PDVDDDDIDQTR1QTR2QTR3QTR4QTRAMOUNTE00224123322.4.Writeoutobservationtorotate.123... 7.3UsingSASArrays7-49PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4datarotate(drop=Qtr1-Qtr4);setprog2.donate;E00224123322.arrayContrib{4}Qtr1-Qtr4;E0036735484030doQtr=1to4;Amount=Contrib{Qtr};PartialListingofrotateoutput;IDQtrAmountend;run;E00224112E00224233Implicitreturn.ContinueE00224322processingobservationsE002244.fromprog2.donate.PDVDDDDIDQTR1QTR2QTR3QTR4QTRAMOUNTE00224123322.5.124......RotatingaSASDataSetprocprintdata=rotatenoobs;run;PartialPROCPRINTOutputIDQtrAmountE00224112E00224233E00224322E002244.E00367135E00367248E00367340E00367430125 7-50Chapter7ProcessingDataIterativelyExercises3.UsingArraystoCreateVariablesWriteaDATAstepthatreadstheSASdatasetprog2.donateandcreatesaSASdatasetnamedquarter.Calculatetheaveragecontributionforanemployeeacrossallfourquarters,andthencalculatethedifferencebetweeneachquarterlycontributionandtheaverage.Usearraystohelpperformthecalculation.PartialListingofprog2.donateIDQtr1Qtr2Qtr3Qtr4E00224123322.E0036735484030E00441.638990E0058716193029E005984861Printthedataset.Thedesiredreportisshownbelow.PartialPROCPRINTOutputObsIDAverageDiff1Diff2Diff3Diff41E0022422.3333-10.333310.6667-0.3333.2E0036738.2500-3.25009.75001.7500-8.25003E0044180.6667.-17.66678.33339.33334E0058723.5000-7.5000-4.50006.50005.50005E005984.7500-0.75003.25001.2500-3.7500 7.3UsingSASArrays7-514.UsingArraysforTableLookup(Optional)Adriver'slicenserenewaltestconsistsoftenmultiple-choicequestions.Eachquestionhasfivechoices(A-E).Eachday,alltestresultsareenteredintotheSASdatasetprog2.testansshownbelow.Eachobservationinprog2.testanscontainsasingleperson'sanswers.Listingofprog2.testansIDQ1Q2Q3Q4Q5Q6Q7Q8Q9Q10291192ACCBDEDBBA593137BCCEEDBAA721311ACCBDDEBBC345221BCCADBBCAD193920ACCBEEDBBA257672BCCBDDDBBA357899CCCBEEEBBA564332ACCBEEDBBA111033ACBDDDBBA445732CCCCEEDBBB824610BBEBEEDBBA774235ACCBEEDBBA943244CCCBEEDBBA647893ACCBEEEBBA432118ACCBEEDBBAThecorrectanswersforthequestionsareshownbelow:Question:12345678910Answer:ACCBEEDBBAReadprog2.testansanddeterminewhethereachpersonpassedorfailedthetest.Computeavariablescorethatcontainsthetotalcorrectanswersforeachperson.Createatemporaryarrayfortheanswerkey. 7-52Chapter7ProcessingDataIterativelyIfapersonscores7orhigher,writetheobservationtoadatasetnamedpassed.Printthedatasettoverifythatthereare12observationsinpassed.PROCPRINTOutputObsIDQ1Q2Q3Q4Q5Q6Q7Q8Q9Q10Score1291192ACCBDEDBBA92593137BCCEEDBAA73193920ACCBEEDBBA104257672BCCBDDDBBA75357899CCCBEEEBBA86564332ACCBEEDBBA107445732CCCCEEDBBB78824610BBEBEEDBBA79774235ACCBEEDBBA1010943244CCCBEEDBBA911647893ACCBEEEBBA912432118ACCBEEDBBA10Ifapersonscoreslessthan7,writetheobservationtoadatasetnamedfailed.Printthedatasettoverifythattherearethreeobservationsinfailed.PROCPRINTOutputObsIDQ1Q2Q3Q4Q5Q6Q7Q8Q9Q10Score1721311ACCBDDEBBC62345221BCCADBBCAD23111033ACBDDDBBA6 7.4SolutionstoExercises7-537.4SolutionstoExercises1.PerformingComputationswithDOLoopsa.datafuture;Wages=12874000;Retire=1765000;Medical=649000;Year=year(today());dountil(Year=year(today())+10);Year+1;/*IfaDOUNTILstatementisused,youmustremembertoincrementthevalueofYear.*/Wages+(Wages*.06);Retire+(Retire*.014);Medical+(Medical*.095);output;end;run;procprintdata=future;varYearWagesRetireMedical;run;Alternatesolution:datafuture;Wages=12874000;Retire=1765000;Medical=649000;doYear=year(today())+1toyear(today())+10;Wages+(Wages*.06);Retire+(Retire*.014);Medical+(Medical*.095);output;end;run;procprintdata=future;varYearWagesRetireMedical;run; 7-54Chapter7ProcessingDataIterativelyb.datafuture;Wages=12874000;Retire=1765000;Medical=649000;Year=year(today());dountil(Year=year(today())+10);Year+1;/*IfaDOUNTILstatementisused,youmustremembertoincrementthevalueofYear.*/Wages+(Wages*.06);Retire+(Retire*.014);Medical+(Medical*.095);TotCost=sum(Wages,Retire,Medical);output;end;run;procprintdata=future;varYearWagesRetireMedicalTotCost;run;Alternatesolution:datafuture;Wages=12874000;Retire=1765000;Medical=649000;doYear=year(today())+1toyear(today())+10;Wages+(Wages*.06);Retire+(Retire*.014);Medical+(Medical*.095);TotCost=sum(Wages,Retire,Medical);output;end;run;procprintdata=future;varYearWagesRetireMedicalTotCost;run; 7.4SolutionstoExercises7-55c.datafuture;Year=year(today());Wages=12874000;Retire=1765000;Medical=649000;Income=50000000;dountil(TotCostgtIncome);Wages+(Wages*.06);Retire+(Retire*.014);Medical+(Medical*.095);TotCost=sum(Wages,Retire,Medical);Income+(Income*.01);Year+1;output;end;run;procprintdata=future;varYearIncomeTotCost;run;2.UsingArraysforRepetitiveComputationsdatacelsius(drop=i);setprog2.ski;/*Youmustresetcosttozeroeverytimeanobservationfromprog2.skiisread.*/Cost=0;arrayTemps{24}T1-T24;doi=1to24;Temps{i}=round(5*(Temps{i}-32)/9,.1);ifTemps{i}lt2thenCost+125;end;run;procprintdata=celsius;run; 7-56Chapter7ProcessingDataIteratively3.UsingArraystoCreateVariablesdataquarter(drop=Qtr);setprog2.donate;/*TwoARRAYstatementsarenecessary.ThefirstARRAYstatementcreatesaSASarraythatcontainsthefourquarterlycontributions.ThesecondARRAYstatementcreatesaSASarraythatcontainsthefourdifferencesthatwillbecalculatedduringtheDATAstep.*/arrayContrib{4}Qtr1-Qtr4;arrayDiff{4};Average=mean(ofQtr1-Qtr4);doQtr=1to4;Diff{Qtr}=Contrib{Qtr}-Average;end;run;procprintdata=quarter;varIDAverageDiff1-Diff4;run; 7.4SolutionstoExercises7-574.UsingArraysforTableLookup(Optional)datapassed(drop=i)failed(drop=i);setprog2.testans;/*TwoARRAYstatementsarenecessary.ThefirstARRAYstatementcreatesaSASarraythatcontainsthetenresponseseachtest-takerselected.ThesecondARRAYstatementcreatesaSASarraythatcontainsthetencorrectanswersforeachofthetenquestions.*/arrayResponse{10}Q1-Q10;arrayAnswer{10}$1_temporary_('A','C','C','B','E','E','D','B','B','A');Score=0;doi=1to10;ifAnswer{i}=Response{i}thenScore+1;end;ifScorege7thenoutputpassed;elseoutputfailed;run;procprintdata=passed;run;procprintdata=failed;run; 7-58Chapter7ProcessingDataIteratively ®Chapter8CombiningSASDataSets8.1Match-MergingTwoorMoreSASDataSets................................................................8-38.2SimpleJoinsUsingtheSQLProcedure(Self-Study)................................................8-198.3SolutionstoExercises.................................................................................................8-31 8-2CombiningSAS®DataSets 8.1Match-MergingTwoorMoreSASDataSets8-38.1Match-MergingTwoorMoreSASDataSetsObjectivesPerformamatch-merge.Performexplicitoutputformatchingandnon-matchingobservations.3MergingDataSetsAmergecombinestwoormoreexistingdatasetsbyjoiningobservationsside-by-side.DATAStep4 8-4CombiningSAS®DataSetsMatch-MergeThemostcommontypeofmergeisamatch-merge,whichusesacommonvariabletojoinobservations.DATAStep5Match-MergingWhenyoumatch-mergetwoormoredatasets,itiscommontohaverepeatedBYvaluesnon-matches.6Match-MergingThedatasetprog2.transactcontainsanaccountnumberandinformationontransactionsforaweek.Thedatasetprog2.branchescontainsanaccountnumberandthebranchlocationforthataccount.ActActNumTransAmntNumBranch56891D126.3256891N.Lincoln56891C56056900S.Cicero57900C23558876W.Argyle58876D14.5659900N.Damen59987C371.6959987E.Wacker7 8.1Match-MergingTwoorMoreSASDataSets8-5DesiredOutputThebankmanagerwantstoseereportsbasedonthreedatasets.Goal:Adatasetnamednewtransthatshowsthisweek’stransactions.ActNumTransAmntBranch56891D126.32N.Lincoln56891C560N.Lincoln58876D14.56W.Argyle59987C371.69E.Wacker8DesiredOutputGoal:Adatasetnamednoactivthatshowsaccountswithnotransactionsthisweek.ActNumBranch56900S.Cicero59900N.Damen9DesiredOutputGoal:Adatasetnamednoacctthatshowstransactionswithnomatchingaccountnumber.ActNumTransAmnt57900C23510 8-6CombiningSAS®DataSetsTheMERGEStatementYoucanusetheMERGEstatementtocombineobservationsfromtwoormoreSASdatasets.GeneralformoftheMERGEstatementwithaBYstatement:DATADATASAS-data-setSAS-data-set……;;MERGEMERGESAS-data-set-1SAS-data-set-2SAS-data-set-1SAS-data-set-2…;…;BYBYBY-variable-1...BY-variable-1...;;<>RUN;RUN;11ThereisnolimitonthenumberofdatasetsthatcanbemergedinoneDATAstep.TheSORTProcedure(Review)WhenyouusetheBYstatementwithaMERGEstatement,thedatasetmustbesortedorindexedaccordingtotheBYvariable(s).YoucanusetheSORTproceduretosortthedata.GeneralformofaPROCSORTstep:PROCSORTPROCSORTDATA=DATA=SAS-data-set1SAS-data-set1SAS-data-set2>;;BYBYBY-variable...BY-variable...;;RUNRUN;;12IfyoumergeaSASdatasetwithaDBMStablefromanotherdatabase,theDBMStabledoesnothavetobesorted. 8.1Match-MergingTwoorMoreSASDataSets8-7DATAStepMergeABIDNamedatanew;IDAge1SAMmergeab;1313SUEbyID;2424BOBrun;421NEWIDNameAge1SAM312423SUE4BOB2113IdentifyingDataSetContributorsWhenyoureadmultipleSASdatasetsinoneDATAstep,youcanusetheIN=datasetoptiontodetectwhichdatasetcontributedtoanobservation.GeneralformoftheIN=datasetoption:SAS-data-setSAS-data-set(IN=(IN=variablevariable))wherevariableisanyvalidSASvariablename.14Thevariablenamecanbeanyunique,validvariablename.Theprogrammermustsupplythevariablename.TheSASSystemsuppliesthevalue. 8-8CombiningSAS®DataSetsTheIN=DataSetOptionvariableisatemporarynumericvariablewithavalueof0toindicatefalse;thedatasetdidnotcontributetothecurrentobservation1toindicatetrue;thedatasetdidcontributetothecurrentobservation.15VariablescreatedwithIN=areautomaticallydroppedfromtheoutputdataset.UsingtheIN=DataSetOptiondatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);mergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;additionalSASstatementsrun;16 8.1Match-MergingTwoorMoreSASDataSets8-9IftheObservationIsaMatchprog2.transactandprog2.branchesbothcontributedtotheobservation.InTrans=1andInBanks=1prog2.transactprog2.branchesCurrentObservation17......IftheObservationIsNotaMatchprog2.branchescontributedtotheobservation.prog2.transactdidnot.(Theaccounthadnotransactionsthisweek.)InTrans=0andInBanks=1prog2.transactprog2.branchesCurrentObservation18......IftheObservationIsNotaMatchprog2.transactcontributedtotheobservation.prog2.branchesdidnot.(Atransactionoccurred,buttheaccountnumberwasinvalid.)InTrans=1andInBanks=0prog2.transactprog2.branchesCurrentObservation19...... 8-10CombiningSAS®DataSetsUsingIN=toIdentifyMatchesandNon-Matchesdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);mergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;ifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifintransandnotInBanksthenoutputnoacct;run;c08s1d1.sas20ActActNumTransAmntNumBranch56891D126.3256891N.Lincoln56891C56056900S.Cicero57900C23558876W.Argyle58876D14.5659900N.Damen59987C371.6959987E.Wackerdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);Compilemergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;ifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifInTransandnotInBanksthenoutputnoacct;run;INTRANSACTNUMTRANSAMNTINBANKSBRANCH21......ActActNumTransAmntNumBranch56891D126.3256891N.Lincoln56891C56056900S.Cicero57900C23558876W.Argyle58876D14.5659900N.Damen59987C371.6959987E.Wackerdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);Executemergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;ifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifInTransandnotInBanksthenoutputnoacct;run;INTRANSACTNUMTRANSAMNTINBANKSBRANCH0.022...... 8.1Match-MergingTwoorMoreSASDataSets8-11ActActNumTransAmntNumBranch56891D126.32Match56891N.Lincoln56891C56056900S.Cicero57900C23558876W.Argyle58876D14.5659900N.Damen59987C371.6959987E.Wackerdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);mergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;ifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifInTransandnotInBanksthenoutputnoacct;run;INTRANSACTNUMTRANSAMNTINBANKSBRANCH1056891D126.32.023......ActActNumTransAmntNumBranch56891D126.3256891N.Lincoln56891C56056900S.Cicero57900C23558876W.Argyle58876D14.5659900N.Damen59987C371.6959987E.Wackerdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);mergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;TrueifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifInTransandnotInBanksthenoutputnoacct;Explicitrun;OutputINTRANSACTNUMTRANSAMNTINBANKSBRANCH1056891D126.32.10N.Lincoln27Writeoutobservationtonewtrans.......ActActNumTransAmntNumBranch56891D126.32NoMatch56891N.Lincoln56891C56056900S.Cicero57900C23558876W.Argyle58876D14.5659900N.Damen59987C371.6959987E.Wackerdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);mergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;ifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifInTransandnotInBanksthenoutputnoacct;run;INTRANSACTNUMTRANSAMNTINBANKSBRANCH1056891D126.32.10N.Lincoln29...... 8-12CombiningSAS®DataSetsActActNumTransAmntNumBranch56891D126.3256891N.Lincoln56891C56056900S.Cicero57900C235Yes58876W.Argyle58876D14.5659900N.Damen59987C371.6959987E.Wackerdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);mergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;ifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifInTransandnotInBanksthenoutputnoacct;run;DonotreinitializePDV.INTRANSACTNUMTRANSAMNTINBANKSBRANCH1056891D126.32.10N.Lincoln31......WhenSASperformsamatch-merge,itonlyreinitializesvaluesreadfromthemergeddatasetsiftheBYvaluesmatch,oriftheBYvaluesinalldatasetschange.ActActNumTransAmntNumBranch56891D126.3256891N.Lincoln56891C56056900S.Cicero57900C23558876W.Argyle58876D14.5659900N.Damen59987C371.6959987E.Wackerdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);mergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;ifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifInTransandnotInBanksthenoutputnoacct;run;INTRANSACTNUMTRANSAMNTINBANKSBRANCH1056891CD126.32560.10N.Lincoln32...... 8.1Match-MergingTwoorMoreSASDataSets8-13ActActNumTransAmntNumBranch56891D126.3256891N.Lincoln56891C56056900S.Cicero57900C23558876W.Argyle58876D14.5659900N.Damen59987C371.6959987E.Wackerdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);mergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;TrueifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifInTransandnotInBanksthenoutputnoacct;Explicitrun;OutputINTRANSACTNUMTRANSAMNTINBANKSBRANCH1056891CD126.32560.10N.Lincoln34Writeoutobservationtonewtrans.......ActActNumTransAmntNumBranch56891D126.32NoMatch56891N.Lincoln56891C56056900S.Cicero57900C23558876W.Argyle58876D14.5659900N.DamenMatchPDV?59987C371.6959987E.Wackerdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);mergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;ifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifInTransandnotInBanksthenoutputnoacct;run;INTRANSACTNUMTRANSAMNTINBANKSBRANCH1056891C560.10N.Lincoln36......ActActNumTransAmntNumBranch56891D126.3256891N.Lincoln56891C56056900S.Cicero57900C235MatchPD58876W.ArgyleNoV?58876D14.56No59900N.Damen59987C371.6959987E.Wackerdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);mergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;ifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifInTransandnotInBanksthenoutputnoacct;run;ReinitializePDV.INTRANSACTNUMTRANSAMNTINBANKSBRANCH0156891C560.01N.Lincoln37...... 8-14CombiningSAS®DataSetsActActNumTransAmntNumBranchReadfrom56891D126.32datasetwith56891N.Lincoln56891C560lowerBY56900S.Cicero57900C235value.58876W.Argyle58876D14.5659900N.Damen59987C371.6959987E.Wackerdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);mergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;ifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifInTransandnotInBanksthenoutputnoacct;run;INTRANSACTNUMTRANSAMNTINBANKSBRANCH056900.10S.Cicero38......WhentheBYvaluesdonotmatcheachotheranddonotmatchtheBYvalueinthePDV,SASreadstheobservationwiththelowestBYvalue.Becausethedataissorted,thisensuresthatSASdoesnotencounter,laterinthedataset,aBYvaluethatisalreadyread.ActActNumTransAmntNumBranch56891D126.3256891N.Lincoln56891C56056900S.Cicero57900C23558876W.Argyle58876D14.5659900N.Damen59987C371.6959987E.Wackerdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);mergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);byActNum;TrueifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifInTransandnotInBanksthenoutputnoacct;run;ExplicitOutputINTRANSACTNUMTRANSAMNTINBANKSBRANCH056900.10S.Cicero41Writeoutobservationtonoactiv....... 8.1Match-MergingTwoorMoreSASDataSets8-15ActActNumTransAmntNumBranch56891D126.3256891N.Lincoln56891C56056900S.Cicero57900C23558876W.Argyle58876D14.5659900N.Damen59987C371.6959987E.Wackerdatanewtransnoactiv(drop=TransAmnt)noacct(drop=Branch);mergeprog2.transact(in=InTrans)prog2.branches(in=InBanks);ContinueprocessingbyActNum;untilendofbothdatasets.ifInTransandInBanksthenoutputnewtrans;elseifInBanksandnotInTransthenoutputnoactiv;elseifInTransandnotInBanksthenoutputnoacct;run;INTRANSACTNUMTRANSAMNTINBANKSBRANCH056900.10S.Cicero43......ViewingOnlytheMatchesprocprintdata=newtransnoobs;run;PROCPRINTOutputActNumTransAmntBranch56891D126.32N.Lincoln56891C560N.Lincoln58876D14.56W.Argyle59987C371.69E.Wacker44Non-Matchesfromprog2.branchesprocprintdata=noactivnoobs;run;PROCPRINTOutputActNumBranch56900S.Cicero59900N.Damen45 8-16CombiningSAS®DataSetsNon-MatchesFromprog2.transactprocprintdata=noacctnoobs;run;PROCPRINTOutputActNumTransAmnt57900C23546c08s1d1.sas 8.1Match-MergingTwoorMoreSASDataSets8-17Exercises1.Match-MergingTwoDataSetsThedatasetprog2.pricesisamasterdatasetcontainingaproductcode(ProdCode)andaunitprice(Price)foreachproductsoldbyaparticularcompany.TheSASdatasetprog2.todaysalescontainsaproductcodeandquantitysoldforeachsalemadeonagivenday.PartialListingofprog2.pricesProdCodePrice1723789.6429978114.4710496128.990884912.2333060162.9905846107.7427731140.753096738.7316344181.5111220160.49PartialListingofprog2.todaysalesProdCodeQty17237515078231049615330601330602333060163306030058461305846130584610Thetwodatasetsarenotsorted. 8-18CombiningSAS®DataSetsCreatethreedatasets:•Adatasetnamedrevenuethatcontainstheproductcode(ProdCode),theprice(Price),thequantitysold(Qty),andtherevenuegeneratedfromeachsale(Revenue).RevenueisanewvariablethatisequaltoPrice*Qty.•Adatasetnamednotsoldthatcontainstheproductcode(ProdCode)andprice(Price)foreachproductthatwasnotsold.•Adatasetnamedinvalidcodethatcontainstheproductcode(ProdCode)andquantity(Qty)foreachobservationinthetodaysalesdatasetthatdoesnothaveacorrespondingproductcodeinthepricesdataset.Thedatasetsshouldcontain39,7,and4observations,respectively.PartialListingofrevenueDataSet(shouldhave39observations)ProdObsCodePriceQtyRevenue10528853.2616852.1620528853.26191011.94305846107.74131400.62405846107.74131400.62505846107.74101077.4060876640.9613532.48710496128.99151934.85811220160.49132086.37ListingofnotsoldDataSet(shouldhave7observations)ProdObsCodePrice104333114.3620884912.2331121169.16417183164.82529978114.4763033931.7473096738.73ListingofinvalidcodeDataSet(shouldhave4observations)ProdObsCodeQty1114651321255673150782342627810 8.2SimpleJoinsUsingtheSQLProcedure(Self-Study)8-198.2SimpleJoinsUsingtheSQLProcedure(Self-Study)ObjectivesPerformaninnerjoinusingtheSQLprocedure.49TheSQLProcedureTheSQLprocedureenablesyoutowriteANSIstandardSQLcodewithintheSASSystemanduseittoprocessSAStables.50ThissectioncoversbasicSQLsyntaxforaninnerjoin.TolearnmoreabouttheSQLprocedure,seetheSASdocumentation.SASEducationalsooffers®anSQLcoursetitledSQLProcessingwiththeSASSystem(http://www.sas.com/apps/wtraining2/coursedetails.jsp?course_code=sql&ctry=us). 8-20CombiningSAS®DataSetsPROCSQLversustheDATAStep:BenefitsTheSQLprocedureenablesyoutojointablesandproduceareportinonestepwithoutcreatingaSASdatasetjointableswithoutsorteddatausecomplexmatchingcriteria.Bydefault,PROCSQLreturnsareport,notaSASdataset.51PROCSQLversusDATAStep:CostsIngeneral,theSQLprocedurerequiresmoreCPUtimeandmemorythanaDATAstepmerge.52JoiningTwoTableswithPROCSQLActActNumTransAmntNumBranch56891D126.3256891N.Lincoln56891C56056900S.Cicero57900C23558876W.Argyle58876D14.5659900N.Damen59987C371.6959987E.WackerThetableprog2.transactcontainsanaccountnumberandinformationontransactionsforaweek.Thetableprog2.branchescontainsanaccountnumberandthebranchlocationforthataccount.53 8.2SimpleJoinsUsingtheSQLProcedure(Self-Study)8-21DesiredOutputThebankmanagerwantstoseeonlytheaccountsthathavevalidtransactions(onlyrowswithmatchingvaluesofActNum).ActNumTransAmntBranchƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ56891D126.32N.Lincoln56891C560N.Lincoln58876D14.56W.Argyle59987C371.69E.Wacker54TheSQLProcedure:SyntaxOverviewThePROCSQLstatementsignalsthestartoftheSQLprocedure.PROCSQL;PROCSQL;55 8-22CombiningSAS®DataSetsTheSQLProcedure:SyntaxOverviewTheQUITstatementendstheSQLstep.QUIT;QUIT;56InPROCSQLsyntax,SASexecutesastatementassoonasitencountersasemicolon.NoRUNstatementisrequired.TheSQLProcedure:SyntaxOverviewStatementswithintheSQLstep(alsocalledqueries)aremadeofsmallerbuildingblockscalledclauses.Thefollowingclausesarediscussedinthissection:SELECTFROMWHERE.Thereisonesemicolonattheendofeachquery;notattheendofeachclause.57 8.2SimpleJoinsUsingtheSQLProcedure(Self-Study)8-23TheSELECTClauseTheSELECTclauseidentifiescolumnstoincludeinthequeryresultortable.SELECTSELECTvar-1var-1,,var-2...var-2...ColumnslistedintheSELECTclauseareseparatedbycommas.Thereisnocommafollowingthelastvariableinthelist.SELECTSELECT**Toselectallcolumnsread,useanasteriskinplaceofthecolumnnames.58TheFROMClauseTheFROMclauseidentifiestheSAStable(s)fromwhichtoread.FROMFROMSAS-data-set...SAS-data-set...59 8-24CombiningSAS®DataSetsUsingPROCSQLtoJoinTablesTojointwoormoreSAStables,listthemintheFROMclauseseparatedbycommas.GeneralformofanSQLjoin:PROCSQL;PROCSQL;SELECTSELECTvar-1var-1,,var-2var-2……FROMFROMSAS-data-set-1SAS-data-set-1,,SAS-data-set-2...SAS-data-set-2...;;60YoucanusePROCSQLtojoinasmanyas32datasets.SQLJoinswithoutaWHEREClauseAnSQLjoinspecifiedwithoutaWHEREclauseresultsinaCartesianproduct.Allpossiblecombinationsareoutput.procsql;select*fromprog2.transact,prog2.branches;quit;61 8.2SimpleJoinsUsingtheSQLProcedure(Self-Study)8-25SQLJoinwithoutaWHEREClausePartialOutputActActNumTransAmntNumBranch56891D126.3256891N.Lincoln56891D126.3256900S.Cicero56891D126.3258876W.Argyle56891D126.3259900N.Damen56891D126.3259987E.Wacker56891C56056891N.Lincoln56891C56056900S.Cicero56891C56058876W.Argyle56891C56059900N.Damen62Intheaboveexample,eachtablecontainsfiverows.Therefore,theresultingCartesianproductcontains5*5,or25,rows.TheWHEREClauseInajoin,theWHEREclausespecifiesthejoincriteria,WHEREWHEREexpressionexpressionwhereexpressionisanyvalidSAScondition.63 8-26CombiningSAS®DataSetsJoiningonaCommonVariableThejoininthescenariorequiresonlymatchingvaluesofActNum.ActNumfromprog2.branches=ActNumfromprog2.transact64IdentifyingVariableswiththeSameNamesprocsql;selectTransact.ActNum,Trans,Amnt,Branchfromprog2.transact,prog2.brancheswhereTransact.ActNum=Branches.ActNum;quit;Youdonotneedtousethetablenameasaprefixifthecolumnnameappearsinonlyonetable.65Conceptually,SASselectsmatchingrowsfromtheCartesianproduct.However,whenthecodeisactuallyprocessed,SASusestheWHEREcriteriatooptimizethejoin.BecausethejoinoutputsonlyrowswherethevaluesofActNummatch,youcanselectActNumfromeithertable.procsql;selectBranches.ActNum,Trans,Amnt,Branchfromprog2.transact,prog2.brancheswhereTransact.ActNum=Branches.ActNum;quit; 8.2SimpleJoinsUsingtheSQLProcedure(Self-Study)8-27AssigninganAliasforaSASTableYoucanalsospecifyanaliasforaSAStable.Thealiasreplacesthetablenameasthecolumnprefix.FROMFROMSAS-data-set-1AS>alias-1,alias-1,SAS-data-set-2AS>alias-2...alias-2...AnaliascanbeanyvalidSASname.66AssigninganAliasforaSASTableprocsql;selectT.ActNum,Trans,Amnt,Branchfromprog2.transactasT,prog2.branchesasBwhereT.ActNum=B.ActNum;quit;c08s2d1.sas67Usually,thetablealiasisusedasaconvenience.Ifyoujointwotableswiththesametablenamebutdifferentlibraryreferences,youmustspecifyanalias. 8-28CombiningSAS®DataSetsInnerJoinwithPROCSQLActNumTransAmntBranchƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ56891D126.32N.Lincoln56891C560N.Lincoln58876D14.56W.Argyle59987C371.69E.Wackerc08s2d1.sas68 8.2SimpleJoinsUsingtheSQLProcedure(Self-Study)8-29Exercises2.PerformingSimpleJoinsUsingPROCSQL(Optional)TheSAStableprog2.rduschedulehasonerowrepresentingeachtimeaflightattendantorpilotisscheduledtoflyintoRDUairport.Itcontainstheflightnumber,thedateoftheflight,andtheemployee’sidentificationnumber.PartialListingofprog2.rduscheduleFltIDSchDateEmpIDIA0360003JAN2000E00075IA0360003JAN2000E00434IA0360003JAN2000E00481IA0240016JAN2000E00082IA0200320JAN2000E00082IA0200320JAN2000E00485IA0200523JAN2000E00481IA0240207FEB2000E00364TheSAStableprog2.fltsptsisamastertableofalltheflightattendantsandpilotsinthecompany.Itcontainseachemployee’sfirstname,lastname,identificationnumber,andjobcode.PartialListingofprog2.fltsptsJobFirstNameLastNameIDCodeDOROTHYEMILLSE00001FLTAT3J.KEVINCOCKERHAME00024FLTAT3DESIREEGOLDENBERGE00031PILOT3ALECFISHERE00033FLTAT2NORMAJEANWIELENGAE00043PILOT3GREGORYJ.GOODYEARE00046FLTAT1HANSECKHAUSENE00047FLTAT3JOHNK.MELTONE00052FLTAT2ANNEWHITEJR.E00055PILOT3 8-30CombiningSAS®DataSetsUsePROCSQLtoproduceareportshowingalltheinformationfortheflightattendantsandpilotsscheduledtoflyintoRDU.PartialOutputJobEmpIDFirstNameLastNameCodeFltIDSchDateƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒE00434KATESMITHPILOT2IA0360003JAN2000E00481BETTYA.YANGFLTAT2IA0360003JAN2000E00481BETTYA.YANGFLTAT2IA0200523JAN2000E00377DONALDT.SZCZEPANSKIPILOT1IA0200016FEB2000E00207ANNEH.YANGFLTAT2IA0240517FEB2000E00432SANDRASCHOBERFLTAT2IA0240517FEB2000E00052JOHNK.MELTONFLTAT2IA0340003APR2000E00247CARRIED.DODGEPILOT2IA0340003APR2000E00120PEGGYH.DUNLAPFLTAT2IA0200005APR2000E00248DAWNB.EDWARDSFLTAT3IA0200005APR2000Hint:SQLjoinsdonotrequirekeycolumnstohavethesamename. 8.3SolutionstoExercises8-318.3SolutionstoExercises1.Match-MergingTwoDataSets/*EachdatasetmustbesortedbyProdCodebeforemerging*/procsortdata=prog2.pricesout=pricesort;byProdCode;run;procsortdata=prog2.todaysalesout=salesort;byProdCode;run;datarevenuenotsold(keep=PriceProdCode)invalidcode(Keep=ProdCodeQty);mergepricesort(in=InPrice)salesort(in=InSales);byProdCode;ifInPriceandInSalesthendo;/*MatchingProdCodes*/Revenue=Qty*Price;/*Onlynecessarytocalculaterevenueformatches*/outputRevenue;end;elseifInPriceandnotInSalesthenoutputnotsold;/*Productnotintodaysalesdataset.*//*Ithasnotsoldthisweek*/elseifInSalesandnotInPricethenoutputinvalidcode;/*Productintodaysalesthatisnotinthemasterpricelist.*/run;procprintdata=revenue;run;procprintdata=notsold;run;procprintdata=invalidcode;run; 8-32CombiningSAS®DataSets2.PerformingSimpleJoinsUsingPROCSQL(Optional)procsql;selectEmpID,FirstName,LastName,JobCode,FltID,SchDatefromprog2.rduschedule,prog2.fltsptswhereEmpID=ID/*SQLdoesnotrequirekeyvariablestohavethesamename.*/;quit; Chapter9LearningMore9.1WhereDoIGoFromHere?............................................................................................9-39.2SASResources...............................................................................................................9-6 9-2Chapter9LearningMore 9.1WhereDoIGoFromHere?9-39.1WhereDoIGoFromHere?ObjectivesExplorewhichSAStrainingcoursesareappropriateafteryoucompleteSAS®ProgrammingII:ManipulatingDatawiththeDATAStep.3 9-4Chapter9LearningMoreAdditionalSASTrainingCoursesSAS®ProgrammingII:ManipulatingDatawiththeDATAStepispartofthefollowinglearningpathsoftheSAScurriculum:AccessingandManipulatingAccessingandManipulatingDataCurriculumDataCurriculumApplicationsDevelopmentApplicationsDevelopmentSASSAS®®ProgrammingII:ProgrammingII:CurriculumCurriculumManipulatingDataManipulatingDatawiththeDATAStepwiththeDATAStepDataWarehousingDataWarehousingCurriculumCurriculumWebEnablementWebEnablementCurriculumCurriculum4Additionallearningpathsinclude•DataPresentation•DataMining•SASITResourceManagement•SASHumanCapitalManagement•StatisticalAnalysis•JMP•StatView.®TheSASTrainingcatalogispublishedbiannuallyandcontainsinformationontrainingservicesavailablefromSAS.Includedinthecatalogaredetailedcoursedescriptions,coursefees,andsuggestedlearningpaths,aswellasinformationondiscountsandspecialoffers.SpecificSASTrainingCoursesSAS®ProgrammingIII:AdvancedTechniquesincludestopicsthatyoucanusetobroadenyourprogrammingskills.SAS®MacroLanguageincludestopicsonbuildingcompletemacro-basedsystemsusingtheSASMacroFacility.5 9.1WhereDoIGoFromHere?9-5SASTrainingForadditionalinformationaboutothertrainingopportunitiesavailablefromSAS,refertotheSASTrainingWebsiteathttp://support.sas.com/training.6SASCertifiedProfessionalProgramConsidertakingacertificationexamtoassessyourknowledgeofSASsoftware.Foracurrentlistingofcertificationexamsandregistrationinformation,visithttp://support.sas.com/certify.7 9-6Chapter9LearningMore9.2SASResourcesObjectivesExploreotherservicesandresourcesavailabletoallSASusers.9SASServicesSASisafull-servicecompanythatprovidesConsultingshort-orlong-termconsultingservicestomeetbusinessneedsTraininginstructor-basedandonlinetrainingoptionsCertificationglobalcertificationprogramtoassessknowledgeofSASsoftwareandearnindustry-recognizedcredentials.10 9.2SASResources9-7SASServicesSASisafull-servicecompanythatprovidesOnlineHelpacomprehensiveonlineHelpsystemtoaddressmanyinformationneedsDocumentationextensiveonlineandhardcopyreferenceinformationTechnicalSupportspecialistsforallSASsoftwareproductsandsupportedoperatingsystems.11SASServicesAccesstheSASWebsiteatwww.sas.comtolearnmoreaboutavailablesoftware,support,andservicesandtotakeadvantageoftheseofferings.12YoucanusetheSASWebsiteto•readaboutsoftware,eitherbyapplicationorbyindustry•learnaboutupcomingworldwideevents,suchasindustrytradeshows•reportproblemstotheTechnicalSupportDivision•learnaboutconsultingservices•identifythemostappropriatelearningpathandregisterforcoursesonline•reviewthelistofcertificationexamsdesignedtoassessknowledgeofSASsoftware;identifytestpreparationoptions;andregisteronlineforacertificationexam®•browseandorderfromtheonlineversionoftheSASPublicationscatalog•accessonlineversionsofSASpublications. 9-8Chapter9LearningMoreConsultingServicesSASoffersflexibleconsultingoptionstomeetshort-orlong-termbusinessneeds.Servicessuchasinstallation,needsassessment,projectscoping,prototyping,orshort-termtechnicalassistancehelpyoutoreapthebenefitsofSASsoftwareasquicklyaspossible.Consultantsprovideexpertiseinareassuchas•datawarehousing•datamining•businessintelligence•Web-enablementtasks•analyticalsolutions•businesssolutions•customapplications•client/servertechnology•systems-relatedissues. 9.2SASResources9-9TrainingServicesSASofferstrainingservicesandacertificationprogramtohelpyouachievebusinessandprofessionalgoals.WhetheryouareabeginningoranaccomplishedSASsoftwareuser,trainingservicesareavailabletohelpyouincreaseyourskillsandexpandyourknowledge.Instructor-basedTrainingoffersbothpublicandon-sitecoursesthatencompassthebreadthofSASsoftwareincluding•theSASprogramminglanguage•reportwriting•applicationsdevelopment•datawarehousing•client/serverstrategies•structuredquerylanguage(SQL)•financialconsolidationandreporting•databaseaccess•statisticalanalysis.SeminarsledbyindustryexpertsarealsoavailablethroughtheBusinessKnowledgeSeriestoprovideyouwithexpertiseinthelatestbusinessdevelopments.e-Learningisanoptimalchoicewhentimeanddistanceareanissue.SASoffersLiveWebclassesandself-pacede-learningtohelpyougetthetrainingyouneedwhileaccommodatingyourbusyschedule.Thebenefitsofe-learningincludethefollowing:•BringSASsoftwareorJMPtrainingdirectlytoyourdesktopandlearnatyourownpaceanytime,anywhere.•Learnatyourconvenience.•Personalizeyourtraining.•PracticeinyourownSASsession.•Enhancewhatyoulearnintheclassroom.Formoreinformationabouttrainingservices,visittheWebat®http://support.sas.com/trainingandorderthecomplimentarySASTrainingcatalog,publishedbiannually(http://support.sas.com/training/us/catalog.html). 9-10Chapter9LearningMoreOnlineHelpandDocumentationSASfeaturesanextensiveonlineHelpsystem.13OnlineHelpandDocumentationYoucanalsoaccessSASOnlineDoc,whichprovidesyouwithSASSystemreferencedocumentation.14SASDocumentationSomeSASdocumentationisalsoavailableinhardcopy.Foradditionalinformation,visittheSASPublishingWebsitesathttp://www.sas.com/apps/pubscat/welcome.jspandhttp://support.sas.com/publishing/.15 9.2SASResources9-11PublicationsServicesForacompletelistofdocumentationavailableinonlineandhardcopyform,accesstheSASPublicationsWebsiteathttp://support.sas.com/publishing.YoucanorderdocumentationusingthePublicationsCatalogthroughtheSASPublicationsWebsiteorbycalling1-800-727-3228.SASalsopublishesanumberofmagazinesandnewsletters.Toviewtheseperiodicals,accesstheSASPublicationsWebsite.SASalsooffersSelecText,aserviceforU.S.collegesanduniversities.TheSelecTextserviceallowsinstructorstocreatecustomcoursetextbooksforteachingstudentstouseSASsoftware.AccesstheSelecTextWebsiteathttp://support.sas.com/selectextorsende-mailtoselectext@sas.com. 9-12Chapter9LearningMoreTechnicalSupportServicesTechnicalSupportprovidesyouwiththeresourcestoanswerquestionsorsolveproblemsthatyouencounterwhenyouuseSASsoftware.YouhaveaccesstoavarietyoftoolstosolveproblemsonyourownandavarietyofwaystocontactTechnicalSupportwhenyouneedhelp.•Free,UnlimitedSupportFreetechnicalsupportisavailabletoallsitesthatlicensesoftwarefromSAS.ThisincludesunlimitedtelephonesupportforcustomersinNorthAmericabycalling1-919-677-8008.CustomersoutsideNorthAmericacancontacttheirlocalSASInstituteoffice.Thereisalsoane-mailinterfaceandFTPsite.•ReportedProblemsAlthoughSASsoftwareisrecognizedasaleaderinreliability,SASrealizesthatnosoftwareisproblemfree.WedoourbesttoletyouknowaboutbugsorproblemsthathavebeenreportedtoTechnicalSupport.InformationaboutreportedproblemsisavailableintheSASNotesandSAS/CCompilerUsageNotes,whicharedistributedwiththesoftware,andcanalsobesearchedviatheWebinterface.WealsoinformyouaboutmoreseriousproblemsthroughAlertNotesandtheTSNEWS-Llistserver.•LocalSupportatYourSiteToprovidethemosteffectiveresponsetoyourquestionsandproblems,oneormorepersonsatyoursitearedesignatedaslocalSASsupportpersonnel.TheseareknowledgeableSASuserswhoareprovidedwithadditionalresourcestoassistallSASusersatyoursite.YoucanoftengetaquickanswertoyourSASquestionsbycontactingyourlocalSASconsultantbeforecallingSASTechnicalSupport.TouseSASTechnicalSupport,youmustknowyourSASSystemsitenumber.Yoursitenumbercanbefoundatthetopofthelog.ThesitenumbercanalsobeeasilyobtainedusingtheSETINITprocedure,whichdisplaysinformationaboutyourSASinstallationinthelog.PROCSETINITNOALIAS;RUN;SASUsersGroupsSASUsersGroupsoffertheopportunityto•enhanceyourunderstandingofSASsoftwareandservices•exchangeideasaboutusingyoursoftwareandhardwaremostproductively•learnofnewSASproductsandservicesassoonastheybecomeavailable•havemoreinfluenceoverthedirectionofSASsoftwareandservices.Additionalinformation,includingalistofSASUsersGroupsworldwide,isavailableattheSASUsersGroupsWebsite,http://support.sas.com/usergroups. AppendixAIndexDROP=datasetoption,5-62$explicitcharactertonumeric,5-59,5-68INPUTfunction,5-57–5-58,5-62$w.informat,1-13RENAME=datasetoption,5-61_D_N_automaticvariable,2-48data_NULL_keywordaccumulatingtotalsforgroups,3-13–3-24DATAstatement,2-42freeformat,4-4nonstandard,4-6Asorting,3-13–3-14absolutelinepointercontrols,4-32standard,4-6accumulatingvariablesDATAstatement,1-11,1-44creatingwithaRETAINstatement,3-5–_NULL_keyword,2-423-9creatingmultipleSASdatasets,2-16creatingwithaSUMstatement,3-9–3-10DATAstepmissingvalues,3-8–3-10accumulatingtotalsforagroupofdata,3-arrayprocessing,7-27–7-35,7-38–7-4812–3-24ARRAYstatement,7-31–7-32controllingvariableinput,2-30–2-31syntax,7-31controllingvariableoutput,2-23–2-29assignmentstatements,1-13,3-4controllingwhichobservationsareread,SUMfunction,3-92-32–2-37syntax,1-44creatingaccumulatingtotalvariables,3-3–3-10Bexplicitoutput,2-5–2-11BYstatementimplicitoutput,2-4–2-5MERGEstatement,8-6INFILEstatement,1-12,1-44usingwithaSETstatement,3-15INPUTstatement,1-12,1-17,1-19,1-44BY-groupprocessing,3-15–3-24OUTPUTstatement,2-5–2-11BYvariables,3-15overview,1-3–1-6multipleBYvariables,3-22–3-24processing,1-16–1-20PUTLOGstatement,6-8Creadingdelimitedrawdatafiles,4-3–4-25readingfromexternalfiles,2-42CEILfunction,5-41readinghierarchicalrawdatafiles,4-56–colonformatmodifier,4-84-84PUTstatement,2-45readingmixedrecordtypes,4-34–4-39COMMAw.dformat,1-25,1-45readingmultiplerecordsperobservation,concatenationoperators,5-164-30–4-33CONTENTSprocedure,1-22–1-23,1-45SETstatement,1-37,1-44convertingdata,5-53–5-69subsettingfromarawdatafiles,4-40–4-automaticcharactertonumeric,5-53–5-4470writingtoexternalfiles,2-42–2-55automaticnumerictocharacter,5-63–5-DATEfunction,5-4866DATE9.format,1-45characterdatestoSASdates,5-63 A-2IndexDATE9.informat,1-13EXAMINEcommandDATEDIFfunction,5-51DEBUGoption,6-17DAYfunction,5-49EXPORTprocedure,2-41DD=option,2-47externalfilesDDNAME=option,2-47readingfrom,2-47,2-55DEBUGoption,6-16–6-17writingto,2-42–2-55commands,6-17extractingcharacters,5-10,5-35–5-36delimiteddatafilesreading,4-3–4-25FDELIMITER=option,2-54FILEstatement,2-42–2-43delimiters,4-4DLM=option,2-54specifying,4-14filespecifications,2-43specifyingalternate,2-54FILE=option,2-47DLM=optionFILENAMEstatement,2-44FILEstatement,2-54FILEREF=optionINFILEstatement,4-14,4-26filespecifications,2-47DOloops,7-3–7-23First.variable,3-15–3-24conditionaliterativeprocessing,7-17–7-FIRSTOBS=datasetoption,2-32,2-3420inPROCsteps,2-36DOUNTILstatement,7-17–7-20SETstatement,2-35DOWHILEstatement,7-17–7-20FIRSTOBS=systemoption,2-34explicitOUTPUTstatement,7-14–7-15FLOORfunction,5-41nested,7-20–7-23FORMATstatement,1-25–1-26,1-45overview,7-4formats,1-25–1-26,1-45performingrepetitivecalculations,7-9–7-formattedinput,1-1214free-formatdata,4-4reducingredundantcode,7-15–7-16FSLISTprocedure,2-47DOstatementsiterative,7-6–7-8HDOUNTILstatement,7-17–7-20syntax,7-17hierarchicalfilesDOWHILEstatement,7-17–7-20creatingoneobservationperdetailrecord,syntax,7-174-57–4-66DOLLARw.dformat,1-25,1-45creatingoneobservationperheaderdoubletrailing@optionrecord,4-66–4-81comparedtosingletrailing@option,4-48processing,4-56–4-57INPUTstatement,4-46–4-47DROPstatement,1-38,1-44,2-23–2-25,2-I30–2-31IF-THENELSEstatements,1-37,1-44DROP=datasetoption,2-25–2-32,5-62IN=datasetoption,8-7–8-11inPROCsteps,2-36INDEXfunction,5-27–5-28DSDoptionindexvariablesINFILEstatement,4-24–4-26iterativeDOstatements,7-6–7-7INFILEstatement,1-12,1-44,2-42EDLM=option,4-14,4-26END=optionDSDoption,4-24–4-26INFILEstatement,4-70END=option,4-70SETstatement,2-49–2-51MISSOVERoption,4-26end-of-fileindicatorPADoption,4-20creating,2-49–2-51TRUNCOVERoption,4-20informats,1-13,4-7 IndexA-3settingvariablelengths,4-10INFILEstatement,4-26specifyinginanINPUTstatement,4-8MMDDYY10.format,1-25,1-45INPUTfunction,5-54,5-57–5-58,5-62modifiedlistoutput,2-45INPUTstatement,1-12–1-13,1-17,1-19,1-MONTHfunction,5-4944,2-42,4-35–4-39,5-54colonformatmodifier,4-8Ndoubletrailing@option,4-46–4-47nameprefixlists,5-6listinput,4-5namerangelists,5-6singletrailing@option,4-37–4-39nestedDOloops,7-20–7-23specifyinginformats,4-8nonstandarddata,4-6–4-8usingmultiple,4-31reading,4-6–4-8insertingcharacters,5-10,5-35–5-36NOOBSoptionINTfunction,5-42PROCPRINTstatement,1-24iteration,2-4numberedrangelists,5-6iterativeDOstatements,7-6–7-8conditionalclauses,7-19OJOBS=datasetoption,2-32–2-33inPROCsteps,2-36joiningtablesSETstatement,2-35usingtheSQLprocedure,8-19–8-27ODSCSVALLstatement,2-40–2-41OFkeywordKSASvariablelists,5-7KEEPstatement,1-38,1-44,2-23–2-25,2-OUTPUTstatement30–2-31DATAstep,2-5–2-11KEEP=datasetoption,2-25–2-32specifyingmultipledatasets,2-17inPROCsteps,2-36PLPADoptionLast.variable,3-15–3-24INFILEstatement,4-20LENGTHstatement,1-38–1-39,1-44,4-10PDV,1-16LIBNAMEstatement,1-29–1-31,1-46performingrepetitivecalculationslinepointercontrols,4-32DOloops,7-9–7-14listinput,4-4SASarrays,7-27–7-28,7-34–7-35LISTWATCHcommandPRINTprocedure,1-22–1-23,1-45DEBUGoption,6-17VARstatement,1-26,1-42,1-45LOGspecificationPRINTspecificationFILEstatement,2-43FILEstatement,2-43logicerrors,6-5PROCPRINTstatementLOWCASEfunction,5-35–5-36NOOBSoption,1-24ProgramDataVector(PDV),1-16MProgramEditorlinenumbercommands,1-51–1-53match-mergingSASdatasets,8-3–8-15PUTfunction,5-54,5-67–5-68MDYfunction,5-48PUTstatement,2-42–2-46,6-6–6-8MEANfunction,5-46colonformatmodifier,2-45MERGEstatementPUTLOGstatement,6-8BYstatement,8-6missingvaluesQaccumulatingtotals,3-8–3-10MISSOVERoption,4-18–4-20QTRfunction,5-49 A-4IndexQUITcommandviewingdataportion,1-22–1-24DEBUGoption,6-17viewingdescriptorportion,1-22–1-23writingtomultiple,2-15–2-18RSASdatevalues,1-10convertingcharacterdates,5-63rawdatafilescreatingusingfunctions,5-48hierarchical,4-56–4-84extractinginformationfrom,5-49missingdataatendofarow,4-15–4-19SASformats,1-25–1-26,1-45missingdatarepresentedbyconsecutiveSASfunctionsdelimiters,4-21–4-25categories,5-3mixedrecordtypes,4-34–4-39CEIL,5-41multipleobservationsperrecord,4-45–4-computingstatistics,5-4447dataconversion,5-53–5-69multiplerecordsperobservation,4-31–4-DATE,5-4833DATEDIF,5-51readingcomma-delimited,4-13–4-14DAY,5-49readingspace-delimited,4-3–4-13FLOOR,5-41readingwithlistinput,4-3–4-25INDEX,5-27–5-28subsettingfrom,4-40–4-44INPUT,5-54,5-57–5-58,5-62relativelinepointercontrols,4-32INT,5-42removingtrailingblanks,5-17LOWCASE,5-35–5-36RENAME=datasetoption,5-61MDY,5-48RETAINstatement,3-4–3-6,4-62–4-66,4-MEAN,5-4667MONTH,5-49RIGHTfunction,5-11QTR,5-49rotatingSASdatasets,7-45–7-48RIGHT,5-11ROUNDfunction,5-40ROUND,5-40SCAN,5-13–5-14SSUBSTR,5-10,5-35–5-36SASarrays,7-27–7-35SUM,5-46creatingvariables,7-38–7-44syntax,5-4overview,7-29TODAY,5-48performingrepetitivecalculations,7-27–TRANWRD,5-33–5-347-28,7-34–7-35TRIM,5-17performingtablelookups,7-44truncation,5-39–5-43rotatingaSASdataset,7-45–7-48UPCASE,5-30SASdatalibraries,1-29WEEKDAY,5-49SASdatasetsYEAR,5-49accessing,1-31YRDIF,5-50–5-51creatingfromrawdatafiles,1-10–1-21SASinformats,1-13creatingmultiple,2-16SASlogcreatingpermanent,1-35writinginformationto,6-6–6-8creatingwithSASdata,1-36–1-42SASprogramsmatch-merging,8-3–8-16editing,1-52permanent,1-31–1-35including,1-51reading,1-37opening,1-48rotatingusingSASarrays,7-45–7-48saving,1-48,1-50,1-52specifyingmultipleinanOUTPUTsubmitting,1-48,1-49,1-51statement,2-17SASservices,9-6–9-12structure,1-8consulting,9-8viewing,1-32–1-34publications,9-10–9-11 IndexA-5technicalsupport,9-12syntaxerrors,6-5SAStablesassigningaliases,8-27Tjoining,8-19–8-27tablelookupsSASvariablelists,5-7performingwithSASarrays,7-44OFkeyword,5-7TITLEstatement,1-46SASvariablesTODAYfunction,5-48naming,1-9TRANWRDfunction,5-33–5-34requiredattributes,1-8–1-9TRIMfunction,5-17SASwindowingenvironmenttruncationfunctions,5-39–5-43onOS/390,1-51–1-53TRUNCOVERoptiononUNIX,1-49–1-50INFILEstatement,4-20onWindows,1-47–1-49SCANfunction,5-13–5-14USELECTgroups,2-18SETstatement,1-37,1-44UPCASEfunction,5-30END=option,2-49–2-51FIRSTOBS=datasetoption,2-35VOBS=datasetoption,2-35VARstatementusingwithaBYstatement,3-15PRINTprocedure,1-26,1-42,1-44simplejoinsvariablesperformingusingtheSQLprocedure,8-accumulatingtotal,3-5–3-919–8-27BY,3-15–3-24singletrailing@optioncontrollinginput,2-30–2-31comparedtodoubletrailing@,4-48controllingoutput,2-23–2-29INPUTstatement,4-37–4-39creatingwithSASarrays,7-38–7-44SORTprocedure,3-13–3-14,8-6First.,3-15–3-24sortingdata,3-13–3-14Last.,3-15–3-24specialSASnamelists,5-6multipleBY,3-22–3-24SQLprocedurenaming,1-9FROMclause,8-23requiredattributes,1-8–1-9performingsimplejoins,8-19–8-27retainingvalues,3-5–3-6SELECTclause,8-23settinglengths,4-10WHEREclause,8-25standarddataWcharacter,4-6numeric,4-6w.informat,1-13STEPcommandWATCHcommandDEBUGoption,6-17DEBUGoption,6-17subsettingIFstatements,3-18–3-19,4-40–WEEKDAYfunction,5-494-44WORKlibrary,1-31SUBSTRfunction,5-10,5-35–5-36YSUMfunctionassignmentstatements,3-9YEARfunction,5-49sumstatementsYRDIFfunction,5-50–5-51creatingaccumulatingvariables,3-9–3-10

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。
大家都在看
近期热门
关闭