Você está na página 1de 6

6/5/2017 ExamplesofSASPrograms

ReturntoTopDocument

Overview
ThisdocumentprovidesthreedifferentexamplesofSASprogramsandexplainsthedifferentcharacteristicsof
eachprogram.

Alinktoeachsampleprogram,withabriefdescriptionofitscharacteristics,isprovidedbelow.Additionallinks
totheprogramtext,theaccompanyingexplanation,andanyadditionalinformation,areprovidedinthe
Overviewsectionofeachsampleprogram.

SampleProgram#1:Readsdataorganizedinfixedcolumns,fromaninlinesourceandusestwoPROCs.
SampleProgram#2:Readsdataorganizedincolumnsseparatedbyspaces,fromanexternalfileanduses
twoPROCs.
SampleProgram#3:Readsdataorganizedincolumnsseparatedbyspaces,fromwithintheprogram
commandfileitselfandusesseveralPROCstochoosetworandomsamples.

SampleProgram#1

Overview

FollowingisthefirstofthreeexamplesofSASprograms.Theprogramiswrittenoutfirst,andanexplanationof
theprogramisprovidedinthesubsequentsection.

Program

DATACLASS;
INPUTNAME$18SEX$10AGE1213HEIGHT1516WEIGHT1822;
CARDS;
JOHNM125999.5
JAMESM125783.0
ALFREDM1469112.5
ALICEF135684.0

PROCMEANS;
VARAGEHEIGHTWEIGHT;
PROCPLOT;
PLOTWEIGHT*HEIGHT;
ENDSAS;
;

Explanation

TheDataStep

TheDATAstatementtellsthecomputerthatthedataiscomingfromaninlinesource,SAScreatesa
temporarydatafilecalledWORK.CLASS.
TheINPUTstatementformatsthevariablesforthecomputer.

http://www2.gsu.edu/~dscbms/dsc8450/sas/examples.html 1/6
6/5/2017 ExamplesofSASPrograms

1.NAME:thisisanalphanumericvariable,asindicatedbythe$.ThevariableNAMEhasbeen
assignedcolumns18.
2.SEX:thisisalsoanalphanumericvariable,andhasbeenassignedcolumn10.
3.AGE:thisisanumericvariable,andhasbeenassignedcolumns1213.
4.HEIGHT:numericvariable,columns1516.
5.WEIGHT:numericvariable,columns1822.
TheCARDSstatementinformsthecomputerthatthedataislocatedinthenextlines.

ThePROCS

1.Thefirstprocedure,PROCMEANS,calculatesthemeanforeveryvariable.
2.Thesecond,PROCPLOT,plotsthevaluesforWEIGHTagainstHEIGHT.

SampleProgram#2

Overview

FollowingisthesecondofthreeexamplesofSASprograms.Theprogramiswrittenoutfirst,andanexplanation
oftheprogramisprovidedinthesubsequentsection.

Ifyourunthissampleprogram,youwillseethecontrastbetweentheoutputlayoutanddetailofthedata
summarizingSASproceduresnamedPROCMEANSandPROCUNIVARIATE.

SampleProgram

DATAhatco;
optionsls=79;
optionsps=60;

INFILE'~dscbms/class/dsc8450/files/hatco';
INPUTX1X2X3X4X5X6X7X8X9X10X11X12X13X14;
LABELX1='DELIVERYSPEED'
X2='PRICELEVEL'
X3='PRICEFLEXIBILITY'
X4='MANUFACTURERIMAGE'
X5='OVERALLSERVICE'
X6='SALESFORCEIMAGE'
X7='PRODUCTQUALITY'
X8='SIZEOFFIRM'
X9='USAGELEVEL'
X10='SATISFACTIONLEVEL'
X11='SPECIFICATIONBUYING'
X12='STRUCTUREOFPROCUREMENT'
X13='TYPEOFINDUSTRY'
X14='TYPEOFBUYINGSITUATION';
Procmeans;
varx1x14;
PROCUNIVARIATEPLOTNORMAL;
varx1x7x9;

Explanation

TheDataStep

http://www2.gsu.edu/~dscbms/dsc8450/sas/examples.html 2/6
6/5/2017 ExamplesofSASPrograms

1.INFILEreadsadataset,specifiedas~dscbms/class/dsc8450/files/hatco.SAScreatesatemporarydata
filecalledWORK.HATCO.
2.Thevariables(X1,X2,X3,etc.)arereadinas"listinput,"becausethedata(allnumericalvalues,inthis
example)arestoredinthefileseparatedbyspaces.
3.TheprogramLABELstheoutputsothatthedisplaywillbeeasiertounderstand.

ThePROCS

1.PROCMEANS:computesthedefaultsummarystatisticsofallthevariablesx1throughx14.
2.PROCUNIVARIATE:computesmoredetailedsummarystatisticsforthemetricvariablesx1throughx7and
x9.TheoptionsPLOTandNORMALproducespecialsummaries.

SampleProgram#3

Overview

FollowingisthethirdofthreeexamplesofSASprograms.Theprogramiswrittenoutfirst,andanexplanationof
theprogramisprovidedinthesubsequentsection.ActuallytheLOGfileisdisplayed,insteadofacopyofthe
inputprogramcommandfile.TheLOGfilenumberseachoftheinputprogramcommandlinesandechosa
descriptionoftheintermediatetemporaryfilescreatedandusedbySAS.(SomeoutputfromtheactualLOGfile
hasbeenalteredslightly.)

Thissampleprogramdemonstratesdatasetsbeingcreated,variablesbeingadded,andtheresultsfurtheraltered
andhandedofftootherDATAstepsandPROCsduringaSASprogram.Thisprogramdemonstratesavery
effectivemannerofgeneratingtwosubsampleswhenthemultivariateanalysisrequiresananalysissample
(calledfirstsubbelow)andaholdoutsample(calledsecndsubbelow).Theoriginalfullsampleissortedaccording
toarbitrarilyassignedrandomnumbers.Eachsamplevalueinthesorteddataisassignedasequencenumber
whichiskeptinaspecialSASvariablenamed_n_.Thedatavaluesarethenassignedtothetwosamples
accordingtothesequencenumber.

SampleProgram

1TheSASSystem
15:14Wednesday,December17,1997

NOTE:Copyright(c)19891996bySASInstituteInc.,Cary,NC,USA.
NOTE:SAS(r)ProprietarySoftwareRelease6.11TS040
LicensedtoGEORGIASTATEUNIVERSITY,Site0008840004.

NOTE:SASinitializationused:
realtime0.57seconds
cputime0.16seconds

1datain;
2item+1;
3inputx1$@@;
4cards;

Nextarelines59fromtheprogramcommandfile.TheyareNOTnormallyshownintheLOGfile.
12345678910
a1a2a3a4a5a6a7a8a9a10
12345678910
b1b2b3b4b5b6b7b8b9b10
12345678910

http://www2.gsu.edu/~dscbms/dsc8450/sas/examples.html 3/6
6/5/2017 ExamplesofSASPrograms

NOTE:SASwenttoanewlinewhenINPUTstatementreachedpasttheendofaline.
NOTE:ThedatasetWORK.INhas50observationsand2variables.

10;
11
12procprintdata=in;
13title'Totalsampleavailable';
14

NOTE:ThePROCEDUREPRINTprintedpage1.

15datawithRan;
16setin;
17fraction=ranuni(74380);
18

NOTE:ThedatasetWORK.WITHRANhas50observationsand3variables.

2TheSASSystem
15:14Wednesday,December17,1997

19procsortdata=withRanout=sorted;
20byfraction;
21

NOTE:ThedatasetWORK.SORTEDhas50observationsand3variables.
NOTE:PROCEDURESORTused:
realtime0.31seconds
cputime0.03seconds

22datafirstsub;
23setsorted;
24if_n_lt15;
25

NOTE:ThedatasetWORK.FIRSTSUBhas14observationsand3variables.

26datasecndsub;
27setsorted;
28if_n_ge15and_n_lt29;
29
30

NOTE:ThedatasetWORK.SECNDSUBhas14observationsand3variables.

31procprintdata=firstsub;
32title'Estimationsample';

NOTE:ThePROCEDUREPRINTprintedpage2.

33procprintdata=secndsub;
34title'Holdoutsample';

NOTE:ThePROCEDUREPRINTprintedpage3.

NOTE:TheSASSystemused:
realtime2.15seconds
cputime0.45seconds

NOTE:SASInstituteInc.,SASCampusDrive,Cary,NCUSA275132414

Explanation
http://www2.gsu.edu/~dscbms/dsc8450/sas/examples.html 4/6
6/5/2017 ExamplesofSASPrograms

ADataStep

1.SAScreatesatemporarydatafilecalledWORK.IN.
2.SAScreatesanewvariablenamedITEMwhichhasthevalue1forthefirstobservation,andthen
incrementsby1foreachadditionalobservation.Thisvariablewillbeusedlatertoidentifytheoriginal
values.
3.Thevariablevaluesofonevariable,X1,arereadinas"listinput,"becausethedataareseparatedby
spaces.The$makesthevaluesalphanumeric.The@@tellsSAStocontinuetothesameinputlinefor
readvalueofthevariable,nottogotothenextinputlineuntilthelineisended.
4.CARDSreadsthedatasetfromlineswithintheinlinesource(theprogramcommandfile).
5.Thedatavaluesareshownhere,tentoacard.
6.Thedatavaluesareshownhere,tentoacard.
7.Thedatavaluesareshownhere,tentoacard.
8.Thedatavaluesareshownhere,tentoacard.
9.Thedatavaluesareshownhere,tentoacard.

AProcPrint
10.Asemicolontoendthedatavalues.
11.[Blankline]
12.Producesaprintedversionoftheoriginaldata.
13.Producesatitle.
14.[Blankline]NextSASdeclaresthepagenumberfortheprintedoutput.

AnotherDataStep
15.SAScreatesatemporarydatafilecalledWORK.WITHRAN.
16.TheinputdataareWORK.IN.
17.Thevariablefractioniscreatedasarandomfraction.TheargumentofRANUNIsetstherandomnumber
seed.Usingthesameseedallowsustorecreatethesamesequenceofrandomfractions.
18.[Blankline]NextSASreportsthedatadescriptionforWORK.WITHRAN.

ProcSort
19.WORK.WITHRANissortedandWORK.SORTEDiscreated.
20.Theobservationsaresortedaccordingtothevalueofthevariablefraction.
21.[Blankline]NextSASreportsthedatadescriptionforWORK.SORTED.

TwomoreDataSteps
22.SAScreatesatemporarydatafilecalledWORK.FIRSTSUB.
23.TheinputdataareWORK.SORTED.
24.Thevariable_n_isaspecialvariablewhichcontainstheobservation'ssequencenumber.TheIFstatement
onlyoutputsadatarecordifitsconditioniscorrect.Onlythefirst14observationswillbeoutputto
WORK.FIRSTSUB.
25.[Blankline]NextSASreportsthedatadescriptionforWORK.FIRSTSUB.
26.SAScreatesatemporarydatafilecalledWORK.SECNDSUB.
27.TheinputdataareWORK.SORTED.
28.Thevariable_n_isaspecialvariablewhichcontainstheobservation'ssequencenumber.TheIFstatement
onlyoutputsadatarecordifitsconditioniscorrect.Thenext14observationswillbeoutputto
WORK.SECNDSUB.
29.[Blankline]NextSASreportsthedatadescriptionforWORK.SECNDSUB.
30.[Blankline]

TwomorePrintProcs
31.ProducesaprintedversionofWORK.FIRSTSUB.
32.Producesatitle.NextSASdeclaresthepagenumberfortheprintedoutput.
http://www2.gsu.edu/~dscbms/dsc8450/sas/examples.html 5/6
6/5/2017 ExamplesofSASPrograms

33.ProducesaprintedversionofWORK.SECNDSUB.
34.Producesatitle.NextSASdeclaresthepagenumberfortheprintedoutput.

Tocheckyourunderstandingofsortinganddatahandling,continuethisexamplebysortingthetwosamples
databackintotheiroriginalsequence.Theprogramcommandfileissavedasthefilenamerandom.

ReturntoTopofDocument

http://www2.gsu.edu/~dscbms/dsc8450/sas/examples.html 6/6

Você também pode gostar