SAS Data Step 2 Programming

SAS Data Step 2 Programming

ID:21248739

大小:930.50 KB

页数:72页

时间:2018-10-20

上传者:U-23001
SAS Data Step 2 Programming_第1页
SAS Data Step 2 Programming_第2页
SAS Data Step 2 Programming_第3页
SAS Data Step 2 Programming_第4页
SAS Data Step 2 Programming_第5页
资源描述:

《SAS Data Step 2 Programming》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

DATAStepProgramming 1ContentVariableAttributesReadingSASDataSetsCreating,DroppingandKeepingVariablesConditionalProcessingAdvance:HowtoWriteLinestoanExternalFile(Self-study)Exercise DefaultVariableAttributesWhenavariableiscreatedinaDATAstep,thename,type,andlengthofthevariableareautomaticallyassignedremainingattributessuchaslabelandformatarenotautomaticallyassignedexceptthattheyaredefinedwhencreatingvariables.Example0 QuestionInwhichstage(compileorexecution),name,type,andlengthofthevariableareautomaticallyassigned?WhatisthelengthofvariableAafterrunningfollowingcode?dataa;if0=1thenA='abc';elseif1=1thenA='uvwxyz';run;Ifwesetormergemultipledatasetsandavariableexistinginallofthesedatasets,whatvariableattributeswillbe?Example1 SpecifyingVariableAttributesUseLABELandFORMATstatementsinthePROCsteptotemporarilyassigntheattributes(forthedurationofthesteponly)UseLABEL,FORMAT,LENGTH,INFORMAT,FORMATorATTRIBstatementsintheDATAsteptopermanentlyassigntheattributes(storedinthedatasetdescriptorportion). 5AssignVariableAttributesLABELstatementAssociatesformatswithvariablessyntaxFORMATstatementAssignsdescriptivelabelstovariablessyntaxLABELvariable-1='label-1'...;FORMATvariable(s); 6Exampledatademog;infile"C:ABCraw_data4.txt";inputsubjid$dosedate:MMDDYY10.dosetime:time5.drugname$;formatdosedatedate9.dosetimetime5.;labeldosedate="Thedatewhenreceivingtreatment"dosetime="Thetimewhenreceivingtreatment";run; 7AssignInformatsInformatStatementSyntaxinformatspecifiestheinformatforreadingthevaluesofthevariablesthatarelistedintheINFORMATstatement.INFORMATvariable(s) ; 8Exampledatatest;informattime1time2time5.sex$char4.;inputsex$time1time2;formattime2time5.;putsex$time1time2;datalines;Female11:1112:12;run;ThePUTstatementproducesthisresult:----+----1----+----2----+Fema4026012:12 9ATTRIBstatementAssociatesaformat,informat,label,and/orlengthwithoneormorevariablesSyntaxATTRIBvariable-list(s)attribute-list(s); 10Exampledatademog;infile"C:ABCraw_data4.txt";inputsubjid$dosedate:MMDDYY10.dosetime:time5.drugname$;attribdosedateformat=date9.label="Thedatewhenreceivingtreatment"dosetimeformat=time5.label="Thetimewhenreceivingtreatment";run; 11UsePROCCONTENTStoexaminetheattributesofvariables.ExampleSASOutputproccontentsdata=work.demog;run; 12QuestionIftheformatofDOSEDATEisassignedasdate9.indatastepWhatformatwillbepresentedinSASoutputafterrunningfollowingprocstep?WillfollowingprocstepchangethepermanentDOSEDATEformat?procprintdata=demog;formatdosedateMMDDYY10.;run; 1313ContentVariableAttributesReadingSASDataSetsCreating,DroppingandKeepingVariablesConditionalProcessingAdvance:HowtoWriteLinestoanExternalFile(Self-study)Exercise 1414ReadingSASDataSetsSETstatementReadsanobservationfromoneormoreSASdatasetsSyntaxSET>;Bydefault,theSETstatementreadsalloftheobservationsfromtheinputSASdatasetvariablesfromtheinputSASdataset. 15SAS-data-setspecifiesaone-levelname,atwo-levelname,oroneofthespecialSASdatasetnames.data-set-options(s)Keep=SpecifiesvariablesforprocessingDrop=ExcludesvariablesfromprocessingRename=Changesthenameofavariablewhere=SelectsobservationsthatmeetthespecifiedconditionExample2 16DataSetOptionsFIRSTOBS=nCausesprocessingtobeginataspecifiedobservationOBS=n|MAXCausesprocessingtoendwiththenthobservationExample3 17PROCSORTstepIfyouwanttousebystatement,pleaseusePROCSORTstepfirstly.PROCSORTstepsorttheSASdatasetaccordingtovariableslistedinBYstatement.Syntax:PROCSORTDATA=OUT=;BYvariable-1<...variable-n>;run;Note:ifyouomittheOUT=,theinputdatasetisorderedbydefault.Example3a 18BY-groupprocessingIsamethodofprocessingobservationsfromoneormoreSASdatasetsthataregroupedororderedbyvaluesofoneormorecommonvariables.GeneralformDESCENDINGindicatesthatthedatasetsaresortedindescendingorder(largesttosmallest)bythevariablethatisspecified.IfyouhavemorethatonevariableintheBYgroup,DESCENDINGappliesonlytothevariablethatimmediatelyfollowsit.BYvariable(s); 19First.variableandlast.variableIntheDATAstep,SASidentifiesthebeginningandendofeachBYgroupbycreatingtwotemporaryvariablesforeachBYvariable:FIRST.variableandLAST.variable.ThesetemporaryvariablesareavailableforDATAstepprogrammingbutarenotaddedtotheoutputdataset.WhenanobservationisthefirstinaBYgroup,SASsetsthevalueoftheFIRST.variableto1.ForallotherobservationsintheBYgroup,thevalueoftheFIRST.variableis0.Likewise,ifanobservationisthelastinaBYgroup,SASsetsthevalueofLAST.variableto1.ForallotherobservationsintheBYgroup,thevalueofLAST.variableis0. 20ThisexampleshowshowSASusestheFIRST.variableandLAST.variabletoflagthebeginningandendoffourBYgroups.Libnametmp"C:ABC";procsortdata=tmp.demogout=demog;byracessmokesubjid;run;dataexample;setdemog;byracessmokesubjid;iffirst.smokethenoutput;run;RawdataSortbySUBJID 21FIRST.LAST.FIRST.LAST.FIRST.LAST.RacesRacesSmokeSmokeSubjidSubjid101011000011000011000011000011000011000011000011000111001011000011010111outputoutputiffirst.smokethenoutput; 2222ContentVariableAttributesReadingSASDataSetsCreating,DroppingandKeepingVariablesConditionalProcessingAdvance:HowtoWriteLinestoanExternalFile(Self-study)Exercise 23AssignmentStatementsEvaluatesanexpressionandstorestheresultinavariableSyntaxexpressionOperands:variablenames/constantsOperators:symbolsthatrequestarithmeticcalculations/SASfunctionsvariable=expression; 24CommonOperatorExample4 25DropStatementSyntaxDROPvariable-list;Libnametmp"C:ABC";dataexample;settmp.demog;dropsubjid;run; 26KeepStatementSyntaxKEEPvariable-list;Libnametmp"C:ABC";dataexample;settmp.demog;keepsubjid;run; 27RenameStatementSyntaxRENAMEold-name-1=new-name-1...;Libnametmp"C:ABC";dataexample;settmp.demog;renamesubjid=subject;run; 28DROP=/KEEP=/RENAME=datasetoptionassociatedwithanoutputdatasetassociatedwithaninputdatasetdataexample2(keep=subjidpidtreattxt);settmp.testdrgj;run;procsortdata=tmp.testdrgjout=example2(keep=subjidpidtreattxt);bypid;run;dataexample2;settest.testdrgj(keep=subjidpidtreattxt);run;procsortdata=test.testdrgj(keep=subjidpidtreattxt)out=example2;bysubjid;run; 29What’sthedifference?Associatedwithaninputdataset,itisavailableforprocessing.Associatedwithanoutputdataset,itisonlyavailableforwritingtooutputdataset.Example5 30dataexample3;settmp.demog(rename=(subjid=subject));keepsubjid;run;dataexample3(rename=(subjid=subject));settmp.demog;keepsubjid;run;ExecuteExecutePDVPDV 31Questiondatatemp1;subjid='Peter';number1=1001;run;datatemp2;settemp1(rename=(number1=number2)keep=subjidnumber1);run;TheRENAMEandKEEPstatementareusedtogetherasdatasetoption,isitcorrect?Shouldkeepvariable“number1”or“number2”? 32RETAINstatementCausesavariablethatiscreatedbyanINPUTorassignmentstatementtoretainitsvaluefromoneiterationoftheDATAsteptothenext.Syntaxelement-listspecifiesvariablenames,variablelists,orarraynameswhosevaluesyouwantretained.initial-valuespecifiesaninitialvalue,numericorcharacter,foroneormoreoftheprecedingelements.RETAIN <...element-list-n>>; 33Exampledataexample4temp1;outputtemp1;settmp.demog;bmi=wt/(ht*.01)**2;outputexample4;run;procprintdata=temp1;varsubjidwthtbmi;run;ObsSUBJIDWTHTBMI12100110017416731001100264170410011003631755100110046217161001100563170...PROCPRINTOutputWhyvariableBMIhasn’tbeencalculated? 34LookingBehindtheScenesdataexample4temp1;outputtemp1;settmp.demog;bmi=wt/(ht*.01)**2;outputexample4;run;CompilePDV 35dataexample4temp1;outputtemp1;settmp.demog;bmi=wt/(ht*.01)**2;outputexample4;run;PDVExecuteWriteoutobservationtotemp1. 36dataexample4temp1;outputtemp1;settmp.demog;bmi=wt/(ht*.01)**2;outputexample4;run;ExecutePDV 37dataexample4temp1;outputtemp1;settmp.demog;bmi=wt/(ht*.01)**2;outputexample4;run;PDVExecute 38dataexample4temp1;outputtemp1;settmp.demog;bmi=wt/(ht*.01)**2;outputexample4;run;PDVExecuteWriteoutobservationtoexample4.Implicitreturn 39dataexample4temp1;outputtemp1;settmp.demog;bmi=wt/(ht*.01)**2;outputexample4;run;PDVExecuteinitializedtomissingIfthevariablesarereadwithaSETstatement,theirvalueswillbeautomaticallyretainedfromoneiterationoftheDATAsteptothenext. 40dataexample4temp1;outputtemp1;settmp.demog;bmi=wt/(ht*.01)**2;outputexample4;run;PDVExecuteWriteoutobservationtotemp1. 41IfusingRETAINstatementinthisexample...dataexample4temp1;retainbmi;outputtemp1;settmp.demog;bmi=wt/(ht*.01)**2;outputexample4;run; 42dataexample4temp1;retainbmi;outputtemp1;settmp.demog;bmi=wt/(ht*.01)**2;outputexample4;run;PDVwillnotbeinitializedtomissingExecuteWriteoutobservationtotemp1.Implicitreturn 43ObsSUBJIDWTHTBMI12100110017416726.53383100110026417022.14534100110036317520.57145100110046217121.20316100110056317021.79937100110067218022.22228100110077617624.53519100110087517424.7721...PROCPRINTOutputprocprintdata=temp1;varsubjidwthtbmi;run; 44Retainstatementcouldchangetheorderofvariablesdatatemp;a=1;b=1;c=1;run;proccontentsdata=temp;run;datatemp;retaincba;settemp;run;proccontentsdata=temp;run;Question1:Whyretainstatementcouldchangetheorderofvariables?Question2:IfRETAINstatementisputafterSETstatement,couldtheorderofvariablesbechanged?Why? 4545ContentVariableAttributesReadingSASDataSetsCreating,DroppingandKeepingVariablesConditionalProcessingAdvance:HowtoWriteLinestoanExternalFile(Self-study)Exercise 46IF-THENlogicExecutesaSASstatementforobservationsthatmeetspecificconditionsGeneralformexpressionOperands:variablenames/constants.Operators:symbolsthatrequestacomparisonalogicaloperationanarithmeticcalculationandSASfunctions.IFexpressionTHENstatement; 47BasiccomparisonoperatorsstatementcanbeanyexecutableSASstatement.Note:UseDOandENDstatementstoexecuteagroupofstatementsbasedonacondition.Example6SymbolicMnemonicMeaning=EQequals~=NEnotequal>GTgreaterthan=GEgreaterthanorequal<=LElessthanorequal 48SelectstatementExecutesoneofseveralstatementsorgroupsofstatementsSyntaxExample7SELECT<(select-expression)>;WHEN-1(when-expression-1<...,when-expression-n>)statement;<...WHEN-n(when-expression-1<...,when-expression-n>)statement;>END; 49SubsettingobservationsInaDATAstep,youcansubsettheobservationswithaWHEREstatementsubsettingIFstatement.DELETEstatement 50WHEREstatementSyntaxTheWHEREstatementappliestoalldatasetsintheSET,MERGE,MODIFY,orUPDATEstatement.WHEREexpression; 5151WHEREandIFcondition 52Exampledataexample6;settmp.demog;subject=subjid;wheresubject='10011001';run;dataexample6;settmp.demog;wheresubjid='10011001';run;VariablesthatareusedintheWHEREstatementmustappearintheinputdatasets 53SubsettingIFstatementContinuesprocessingonlythoseobservationsthatmeettheconditionSyntaxIFexpression;IFexpressionDATAStatementReadObservation orRecordTrueContinueProcessingObservationOutputObservationtoSASDataSetFalse 54What’sthedifferencebetweenWHEREandSubsettingIFUseanexampletoillustratedataexample7;settmp.demog;subject=subjid;ifsubject='10011001';run;dataexample7;settmp.demog;subject=subjid;wheresubject='10011001';run;Reason:ThevariableusedintheWHEREstatementshouldexistintheinputdatasets.NosuchrestrictionsareforsubsettingIFstatement. 55QuestionIfyouneedtoSET/MERGEmultipledatasets,avariableinwherestatementdoesnotexistinalldatasets,whatwillhappen?Why?dataa;doa=1to4;value=a;output;end;run;datab;dob=1to4;value=b;output;end;run;datac;setab;where2<=a<=4;run; 56DELETEstatementSyntaxWhenDELETEexecutes,thecurrentobservationisnotwrittentoadataset,andSASreturnsimmediatelytothebeginningoftheDATAstepforthenextiteration.Example7DELETE;SyntaxWhenDELETEexecutes,thecurrentobservationisnotwrittentoadataset,andSASreturnsimmediatelytothebeginningoftheDATAstepforthenextiteration.Example8 5757ContentVariableAttributesReadingSASDataSetsCreating,DroppingandKeepingVariablesConditionalProcessingAdvance:HowtoWriteLinestoanExternalFile(Self-study)Exercise 58PUTStatementWriteslinestotheSASlog,totheSASprocedureoutputfile,ortoanexternalfilethatisspecifiedinthemostrecentFILEstatementSyntaxcolumnpointercontrols(@n,@numeric-variable,@(expression),+n,+numeric-variable,+(expression))specification(s)variable/(variable-list)'character-string‘/n*’character-string’n*:specifiestorepeatntimesthesubsequentcharacterstring.eg:put132*'_';_all_:writesthevaluesofallvariables,whichincludesautomaticvariablesput<@|@@>; 59@n:movesthepointertothestartingpositionofthefieldExampledata_null_;put@20"============Note============";put@22"Thisisjustanexample";put@20"============================";run;LOGInfo.Thelinestartsfromthe22ndcolumn. 60+n:movesthepointerncolumns.Exampledatatmp;settmp.demog;putsubjid+2sex+(-1)'*'races+(-1)'*'age;run;LOGInfo.Makethepointermoveforwardonecolumn 61PutStylesTherearefourwaystowritevariablevalueswiththePUTstatement:ListputColumnputFormattedputNamedput 62ColumnPUTSyntax:FormattedPUTSyntax:NamedPUTStatementSyntax:PUTvariablestart-column<--end-column><@|@@>;PUTvariableformat.<@|@@>;PUTvariable=<@|@@>; 63FILEStatementSpecifiesthecurrentoutputfileforPUTstatementsSyntaxfile-specification:'external-file‘:specifiesthephysicalnameofanexternalfilefileref:specifiesthefilerefofanexternalfile.log:areservedfilerefthatdirectstheoutputthatisproducedbyanyPUTstatementstotheSASlogprintfileref(file)FILEfile-specification 64OptionsDELIMITER='quotedstring‘:specifiesanalternatedelimiter(otherthanblank)tobeusedforLISToutput.Itisignoredforothertypesofoutput(formatted,column,named).Exampledata_null_;settmp.demog;file"c:ABCdemog.txt"delimiter='|';putsubjidracesagesexsmokehtwt;run; 65MOD:writestheoutputlinesafteranyexistinglinesinthefile.data_null_;file"c:ABCdemog.txt"delimiter='|';subjid='10011001';races='WHITE';age=50;putsubjidracesage;run;data_null_;file"c:ABCdemog.txt"moddelimiter='|';subjid='10011001';races='BLACK';age=75;putsubjidracesage;run; 6666ContentVariableAttributesReadingSASDataSetsCreating,DroppingandKeepingVariablesConditionalProcessingAdvance:HowtoWriteLinestoanExternalFile(Self-study)Exercise 67Exercise1–LOCF(Vertical)LOCF(LastObservationCarryForward)isagenerallyusedmethodtoimputemissingvaluesinclinicaltrials.WehavearawdatafileLOCF.txtunderthedirectoryC:ABCcase_study.AfterreadinginasSASdataset,itlooklikesbelow.Weneedtoreplacethemissingvalueswithpreviousnon-missingvaluewithineachSUBJIDandVSTEST.Exercise1 68 69Exercise2WehaveandatasetunderthedirectoryC:ABCcase_study.Followingisthesnapshotofit.Wewanttocalculatethebmivalueforeachsubjectandthenoutputthesubjectswithbiggestbmivalueofdifferentsextypesinanotherdataset.Thenwritetheirheight,weight,bmiinformationtoanexternalfile.Exercise2 70libnameworkload"c:ABCcase_study";procsortdata=workload.demogout=demog;bysubjidtest;run;datademog2;setdemog;bysubjidtest;retainheight;iffirst.subjidthenheight=value;elsedo;bmi=value/(height*.01)**2;weight=value;end;ifbmi~=.;run;procsortdata=demog2;bysexdescendingbmi;run;datademog3;setdemog2;bysexdescendingbmi;iffirst.sexthenoutput;run;...Toretaintheheightvaluetonextreading.Dosubsetting.Wherestatementisnotapplicablehere.Why?Ouputthesubjectswithbiggestbmivalueindemog3. 71...data_null_;setdemog3;file"c:ABCcase_studydemog.txt"titlesdelimiter='|';putsubjidweightheightbmi;run;

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

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

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