Anda di halaman 1dari 2

Print

SummaryofLesson2:WorkingwithSASPrograms
Thissummarycontainstopicsummaries,syntax,andsampleprograms.

TopicSummaries
Togotothemoviewhereyoulearnedataskorconcept,selectalink.
ExploringSASPrograms
ASASprogramconsistsofDATAstepsandPROCsteps.ASASprogrammingstepiscomprisedofa
sequenceofstatements.Everystephasabeginningandendingstepboundary.SAScompilesandexecutes
eachstepindependently,basedonthestepboundaries.
ASASprogramcanalsocontainglobalstatements,whichareoutsideDATAandPROCsteps,andtypically
affecttheSASsession.ATITLEstatementisaglobalstatement.Afteritisdefined,atitleisdisplayedon
everyreport,unlessthetitleisclearedorcanceled.
SASstatementsusuallybeginwithanidentifyingkeyword,andalwaysendwithasemicolon.SAS
statementsarefreeformatandcanbeginandendinanycolumn.Asinglestatementcanspanmultiplelines,
andtherecanbemorethanonestatementperline.Unquotedvaluescanbelowercase,uppercase,ormixed
case.Thisflexibilitycanresultinprogramsthataredifficulttoread.
Conventionalformatting,alsocalledstructuredformatting,usesconsistentspacingtomakeaSASprogram
easytoread.Tofollowbestpractices,begineachstatementonanewline,indentstatementswithineach
step,andindentsubsequentlinesinamultilinestatement.
CommentsareusedtodocumentaprogramandtomarkSAScodeasnonexecutingtext.Therearetwo
typesofcomments:blockcommentsandcommentstatements.

/*comment*/
*commentstatement

DiagnosingandCorrectingSyntaxErrors
SyntaxerrorsoccurwhenprogramstatementsdonotconformtotherulesoftheSASlanguage.Common
syntaxerrorsincludemisspelledkeywords,missingsemicolons,andinvalidoptions.SASfindssyntaxerrors
duringthecompilationphase,beforeitexecutestheprogram.WhenSASencountersasyntaxerror,itwrites
thefollowingtothelog:thewordERRORorWARNING,thelocationoftheerror,andanexplanationofthe
error.Youshouldalwayscheckthelog,eveniftheprogramproducesoutput.
Mismatchedorunbalancedquotationmarksareconsideredasyntaxerror.Insomeprogramming
environments,thisresultsinasimpleerrormessage.Inotherenvironments,itismoredifficulttoidentifythis
typeoferror.

SamplePrograms
SubmittingaSASProgram
datawork.newsalesemps
setorion.sales
whereCountry='AU'
run
title'NewSalesEmployees'
procprintdata=work.newsalesemps
run

procmeansdata=work.newsalesemps
classJob_Title
varSalary
run
title
AddingCommentstoYourSASPrograms
*Thisprogramusesthedatasetorion.salestocreatework.newsalesemps.
datawork.newsalesemps
setorion.sales
whereCountry='US'
run
/*
procprintdata=work.newsalesemps
run*/
procmeansdata=work.newsalesemps
classGender
varSalary/*numericvariable*/
run
ViewingandCorrectingSyntaxErrors
daatwork.newsalesemps
lengthFirst_Name$12
Last_Name$18Job_Title$25
infile"&path/newemps.csv"dlm=','
inputFirst_Name$Last_Name$
Job_Title$Salary
run
procprintdata=work.newsalesemps
run
procmeansdata=work.newsalesempsaveragemax
classJob_Title
varSalary
run

SASProgramming1:Essentials

Copyright2016SASInstituteInc.,Cary,NC,USA.Allrightsreserved.

Close

Anda mungkin juga menyukai