sasadvancedbook2.docx
《sasadvancedbook2.docx》由会员分享,可在线阅读,更多相关《sasadvancedbook2.docx(57页珍藏版)》请在冰点文库上搜索。
sasadvancedbook2
TopicAdvancedProgrammingTechniquesII
1.Array
2.PROCDATASETS
3.PROCCOMPARE
4.PROCFORMAT
5.PROCSORT
6.CreatetheFirst.andLast.TemporaryVariables
7.UsingSASDATAStepViewToConserveDataStorageSpace
8.HowtoControlWhichVariablesandObservationsYou
WanttoReadandWritebyUsingSAS
9.CreatingIntegrityConstraints
10.TheEfficiencyofSASProgramming
1.ReviewofARRAY
1.1Definition
ArrayisatemporarygroupingofSASvariablesthatarearrangedinaparticularorderandidentifiedbyanarray-name.ThearrayexistsonlyforthedurationofthecurrentDATAstep.Thearray-nameitisnotavariable.InSAS,anarrayisnotadatastructurebutisjustaconvenientwayoftemporarilyidentifyingagroupofvariables.
1.2BasicConcepts
Arrayprocessing:
isamethodthatenablesyoutoperformthesametasksforaseriesofrelatedvariablesoragroupofvariables.
Arrayreference:
isamethodtoreferencetheelementsofanarray.
One-dimensionalarrayisasimplegroupingofvariablesthat,whenprocessed,results
inoutputthatcanberepresentedinsimplerowformat.
Multiple-dimensionalarrayisamorecomplexgroupingofvariablesthat,whenprocessed,resultsinoutputthatcouldhavetwoormoredimensions,suchascolumnsandrows.
1.3WhyuseSASArray?
∙readdata
∙repeatanactionorsetofactionsoneachofagroupofvariables
∙createseveralrelatedvariables
∙writeshorterprograms
∙restructureaSASdatasettochangetheunitofobservation
1.4THESYNTAX
Thegeneralsyntaxfordefininganarrayisasfollows:
ARRAYarray-name{dimension}$lengthelements(initialvalues);
-Array-name–isthenamewecreateforthearray.ItmustbeavalidSASnameandisrecommendedtonotbethesameasaSASFunctionname.InVersion7andbeyondthearraynamecanbeupto32charactersinlength.
-{Dimension}–indicatesthenumberofelementsinthisarray.Arraysubscriptmustbeenclosedwithin:
braces{},squarebrackets[],andparentheses().Whenthearraysubscriptisanasterisk(*)itisnotnecessarytoknowhowmanyelementsarewithinthearray.
-$-includedontheARRAYstatementonlyifthearrayischaracter,thatis,ifthearraywillbereferencingnewcharactervariables.Thedollarsignisnotnecessaryiftheelementsinthearraywerepreviouslydefinedascharacterelements.
-Length–canbeusedtodefinethelengthofthenewcharactervariablesreferencedbythearrayorspecifiesthelengthofelementsinthearraythatwerenotpreviouslyassignedalength.
-Elements–canbeusedtodefinethevariablesthatthearraywillreference,eitherexistingvariablesornewvariables.Theycanbelistedinanyorderandmustbeallnumericorallcharacter.
Specialvariablesmaybeusedtoselectallvariablesorallvariablesofaselecttype:
_numeric_,character_,and_all_.
-InitialValues–canbeincludedtogivetheelementsofthearrayinitialvalues.Thisalsocausesthesevariablestoberetainedduringthedatastep(i.e.notreinitializedtomissingattheexecutionoftheDATAstatement).
-Dimfunctioncanbeusedtoreturnthecountofelements
Advice:
arraystatementisnotanexecutablestatement.
1.5TemporaryArrays
WhenelementsareconstantsneededonlyfordurationofDATAstep,youcanomitvariablesfromanarrayandinsteadusetemporaryarrayelements.
-temporaryarrayelementsbehavelikevariables
-temporaryarrayelementsdonothavenames
-arrayelementsdonotappearintheoutputdataset
-theyareautomaticallyretained
Basicexamples:
Example1:
DefiningArrays
∙arrayrain{5}janrfebrmarraprrmayr;
∙arraydays{7}d1-d7;
Example2:
AssigningInitialNumericValues
∙arraytest{4}t1t2t3t4(90807070);
Example3:
DefiningInitialCharacterValues
∙arraytest2{*}a1a2a3('a','b','c');
Example4:
DefiningMoreAdvancedArrays
∙arrayx{5,3}score1-score15;
Example5:
DefiningMoreAdvancedArrayslowerbound/highbound
arrayyr[00:
06]yr00-yr06;
/*UnderstandingArraystructure*/
dataarry;
arraysm{*}x1-x5;
inputx1-x5;
doi=1todim(sm);
/*doi=1to5*/
/*doi=1,2,3,4,5;*/
/*doi=1dim(*);*/
new=sm(i)+10;
output;
end;
datalines;
12345
;
procprintdata=arry;
run;
/*UsingCharacterVariablesinanarray;*/
optionsnodatepageno=1linesize=80pagesize=60;
dataarry_01;
arraynames{*}$n1-n10;
arraycapitals{*}$c1-c10;
inputnames{*};
doi=1to10;
capitals{i}=upcase(names{i});
end;
datalines;
smithersmichaelsgonzalezhurthfrankbleigh
rounderjosephpeterssam
;
procprintdata=arry_01;
title'NamesChangedfromLowercasetoUppercase';
run;
/*CreateNewVariables*/
dataa;
inputx1-x3@@;
cards;
102030204060306090
;
dataaa;
seta;
arrayxs[3]x1-x3;
arrayrate[3];
doi=1to3;
rate[i]=xs[i];
end;
run;
procprintdata=aa;
run;
*-----------------------------------------------------------*
|Example:
Usingamulti-dimensionalarraytorestructure|
|adataset|
*-----------------------------------------------------------*;
DataWT_ONE;
inputIDWT1WT2WT3WT4WT5WT6;
datalines;
01155158162149148147
02110112114107108109
;
DATAWT_MANY;
SETWT_ONE;
ARRAYWTS[2,3]WT1-WT6;
DOCOND=1TO2;
DOTIME=1TO3;
WEIGHT=WTS[COND,TIME];
OUTPUT;
End;
END;
DROPWT1-WT6;
RUN;
Quiz:
BasedontheARRAYstatementbelow,selectthearrayreferenceforthearrayelementq50.
arrayqs{4,20}q1-q80;
datapop;
inputmale0_9male10_19male20_29male30_39male40_49
female0_9female10_19female20_29female30_39female40_49;
datalines;
856570110205907085105220
7585801002251008095125240
7055651052151058590125210
8065801202008575100100250
;
procformat;
valuesexpop1='male'
2='female'
;
valueagegrppop1='0-9'
2='10-19'
3='20-29'
4='30-39'
5='40-49'
;
run;
datanewpop(keep=sexagegrpsize_pop);
setpop;
arrayx(2,5)_numeric_;
dosex=1to2;
doagegrp=1to5;
size_pop=x(sex,agegrp);
output;
end;
end;
run;
2.PROCDATASETS
2.1Introduction
IlovetheDATAstep.It'spowerful.It'sflexible.AlmosteveryoneneedstheDATAstepfromtimetotime,butsomeprogrammerslikeitsomuch,theyuseitforeverything--creatingdatasets,copyingdatasets,movingdatasets.Ifyouaremanipulatingyourdata,theDATAstepistheperfecttool.However,ifyouareperformingdatamanagementtaskssuchascopyingaDATAsetoraddingFORMATandLABELS,oryouwanttoavoidrunningaDATAstep,PROCDATASETSisapowerfultool.
2.2WhyweneedtousePROCDATASETS?
Therearetwomainreasonsforthis.
-First,thereisalwaysthechanceyouwillmakeaprogrammingmistakeanddestroyyourdataset.
-Second,usingaDATAstepfordatamanagementisofteninefficient.
AdvantageofPROCDATASETS
-ifallyouneedistocopyorchangethedescriptorportionofthedataset,itisusuallyeasierandmoreefficienttousePROCDATASETS.
-theymaynotevenkeepSASdatasetsaround.
-PROCDATASETSwouldreallyhelpwithefficiencyduringtheexecutionoftheprogram,eitherbyavoidingaDATASTEPorbyfreeingupworkingstorage.
-thereareanumberofneatthingsthatonlyPROCDATSETSdoes,suchasrepairingandmodification.
PROCDATASETSisadatamanagementprocedurethatallowsyoutodothesetaskswithoutcompletelyre-writingthedataset.Theprocedureisautilitythatallowsyouto
efficientlymanageyourSASfiles.
PROCDATASETS;
Age/Append/Audit/Change/Copy/Exclude/Select/Delete/Modify/Format
ICCreate/ICDelete/ICReactivate/IndexCreate/IndexDelete/Informat
Label/Rename/Repair/Save
Examples
Age-renamesagroup
libnameee"c:
\";
dataee.current;
inputbrkdnt:
date7.vehicle$3.@@;
cards;
2mar94AAA20may94AAA19jun94AAA29nov94AAA
4jul94BBB29may94CCC24dec94CCC
;
dataee.bkup;
inputbrkdndt:
date7.vehicle$3.@@;
cards;
2mar95AAA20may95AAA19jun96AAA19nov94AAA
5jul96BBB30may98CCC14dec95CCC
;
procdatasetslibrary=eenowarn;
agecurrentbkup;
exchangebkup=current;
run;
quit;
Append–addsobservationsfromonedatasettoanother.
procdatasets;
appendbase=currentdata=bkup;/*similartosetorprocappend*/;
run;
quit;
Change–changesthennameofaSASfileintheinputdatalibrary(orwithinaSASdirectory).
procdatasets;
changebkup=new;
run;
quit;
Copy–copiessomeorallmembersofoneSASlibrarytoanother.Thisisprimarilyusedtomovedatasetsfromonesystemorversiontoanother.
Tolimitcopyingtospecificmembers,useeithertheSELECTorEXCLUDEoptions.TospecifyadifferentlibrarytocopyfromuseeithertheDATASETSLIBRARYoptiontospecifyadefaultlibraryorusetheIN=option.Tomoveamemberfromonelibrarytoanotherandthendeletetheoriginalmember,usetheMOVEoption
Thefollowingexamplemovestwomembersfromlib1tolib2:
LIBNAMEdest1‘SAS-data-library’;
LIBNAMEdest2‘SAS-data-library’;
procdatasets;
copyin=dest1out=dest2movememtype=data;
selectcurrent;
*excludeaa;
run;
quit;
Delete–getsridofunneededfiles.Ithelpstofreeupmemorybygettingridofanydatasetyouhavefinishedusing.
procdatasetslibrary=dest1;/*workdir*/
deleteaa;
run;
quit;
KilldeleteSASfiles
procdatasetslibrary=workkill;
run;
quit;
Save–willdeleteallfilesexceptthoselistedontheSAVEstatement.
Modify–tochangespecificdatasetorvariableattributes.Worksonlyononedatasetatatime.Allowsyoutochangeorspecifyformats,informats,andlabels,renamevariablesandcreateanddeleteindexes.
ForanexistingdatasettheMODIFYcommandisthebestwaytomakechangesbecausenoobservationsarereadinorwrittenoutdurin