资源描述:
《sas programming ii manipulating data with the data step》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
®SASProgrammingII:ManipulatingDatawiththeDATAStepCourseNotes ®SASProgrammingII:ManipulatingDatawiththeDATAStepCourseNoteswasdevelopedbyJemshaidCheemaandMelindaThielbar.AdditionalcontributionsweremadebyMartyHultgren,KentReeve,WarrenRepole,andRogerStaum.EditingandproductionsupportwasprovidedbytheCurriculumDevelopmentandSupportDepartment.SASandallotherSASInstituteInc.productorservicenamesareregisteredtrademarksortrademarksofSASInstituteInc.intheUSAandothercountries.®indicatesUSAregistration.Otherbrandandproductnamesaretrademarksoftheirrespectivecompanies.®SASProgrammingII:ManipulatingDatawiththeDATAStepCourseNotesCopyright2004bySASInstituteInc.,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<