超经典的SAS BASE的笔记4.docx
《超经典的SAS BASE的笔记4.docx》由会员分享,可在线阅读,更多相关《超经典的SAS BASE的笔记4.docx(17页珍藏版)》请在冰豆网上搜索。
超经典的SASBASE的笔记4
Topic:
OverviewofMultipleDataSets
1.ConcatenatingdatasetusingSetstatement
2.Interleavingseveraldatasets
3.Modifying(creating)dataset
4.ConcatenatingdatasetusingPROCAPPEND
5.Mergingdatasets
6.Updatingdataset
7.PROCEXPORT/IMPORT
8.PROCCONTENTS
Overview
OneofSAS’sgreateststrengthsisitsabilitytocombineandprocessmorethanonedatasetatatime.ThemaintoolsusedtodothisaretheSET,MERGEandUPDATEstatements.
1.ConcatenatingdatasetusingSetstatement
ItreadsanobservationfromoneormoreSASdatasets.TheSETstatementisflexibleandhasavarietyofusesinSASprogramming.TheseusesaredeterminedbytheoptionsandstatementsthatyouusewiththeSETstatement:
dataone;
inputyearpop$@@;
datalines;
1991500K1992501K1993502K
;
datatwo;
inputyearpop$@@;
datalines;
1991400K1992401K1993402K
;
datathree;
inputyearpop$@@;
datalines;
1991300K1992301K1993302K1994303K
;
datacombine_1;
setonetwothree;
run;
datacon1;
inputcustom_id$product$12.;
cards;
28901pentiumIV
36815pentiumIII
21224pentiumIV
;
datacon2;
inputcustom_id$product$12.;
cards;
18601pentiumIV
24683pentiumIII
851921pentiumIV
61831pentiumIV
;
datacon3;
setcon1;
setcon2;
run;
2.Interleavingseveraldatasets
Ifyouwanttocombineseveraldatasetssothatobservationssharingacommonvaluearealladjacenttoeachother,youcanlistthedatasetsonSETstatement,andspecifythevariabletobeusedonaBYstatement.Note:
Thedatasetstobeinterleavedmustalreadybesortedbythevariable(s)listedintheBYstatement.
/*Creatinganewdatasetfrommultipledatasetsbaseduponsortedorder*/;
dataanimal;
inputcommon$animal$;
datalines;
aAnt
aApe
bBird
cCat
dDog
;
dataplant;
inputcommon$plant$;
datalines;
aApple
bBanana
cCoconut
dDewberry
eEggplant
fFig
;
datainterleaving;
setanimalplant;
bycommon;
run;
procprintdata=interleaving;
run;
3.TheSETstatementisusedtomodifyanexistingSASdataset
andReadingObservationsUsingDirectAccess
dataold;
inputx1-x3;
datalines;
123
345
;
datanew;
setold;
xtot=sum(ofx1-x3);
run;
dataa;
inputxy@@;
cards;
90119022903390449055
;
datab;
x=2;/*selectagivenobs*/
setapoint=x;
output;
stop;
run;
Tocreateatemporarynumericvariablewhosevalueisusedtodetectthelastobservation,youcanusetheEND=optionintheSETstatement.
dataen;
inputaccntbalanceday@@;
cards;
9014861901985490349829034982
;
dataen2;
setenend=last;
iflast;
run;
4.ConcatenatingdatasetusingPROCAPPEND
ConcatenatingSASdatasetsistheprocessofstoringobservationsoneafteranotheruntilallthedatasetsandtheirobservationshavebeencombinedintoonedataset.
ManyusersperformtheconcatenationprocessusingaDATAstep(asshowninthepreviousexamples),buttherearegoodreasonsforusingtheAPPENDprocedure.IfyouusetheSETstatementinaDATAsteptoconcatenatetwodatasets,theSASSystemmustprocessalltheobservationsinbothdatasetstocreateanewone.TheAPPENDprocedurebypassestheprocessingofdataintheoriginaldatasetandaddsnewobservationsdirectlytotheendoftheoriginaldataset.Itdoesthisbypositioningtherecordpointerattheendoftheoriginaldataset,andstartingtheprocessingdirectlywiththenewobservations.
PROCAPPEND
Syntax:
BASE=SAS-data-set
SincePROCAPPENDreadsonlytheseconddataset,setBASE=tothelargerdataset.However,theorderofthedatasetsdoesnotmatter.
datamaster;
inputcity$1-11month$10.temp;
cards;
HonoluluAugust80.7
HonoluluJanuary72.3
BostonJuly73.3
BostonJanuary29.2
DuluthJuly65.6
DuluthJanuary8.5
NewYorkAugust82.7
NewYorkJanuary22.3
;
dataadd;
inputcity$1-11month$10.temp;
cards;
RaleighJuly77.5
RaleighJanuary40.5
MiamiAugust82.9
MiamiJanuary67.2
LosAngelesAugust69.5
LosAngelesJanuary54.5
;
run;
procappendbase=masterdata=add;
run;
dataweather;
inputdatemmddyy8.tempsunhrs;
cards;
01-01-90359.3
01-02-90349.33
01-03-90379.35
01-04-90389.39
;
datadaily;
inputdatemmddyy8.tempsunhrsprecip;
cards;
01-05-90359.401.1
;
run;
procappendbase=weatherdata=dailyforce;
run;
Whataretheadvantagesor/anddisadvantagesofSETstatementandPROCAPPEND?
1.PROCAPPENDismoreefficientforappendingtwodatasets.BecausePROCAPPENDperformsanupdateinplaceontheBASE=dataset;therefore,itjustaddstheobservationsfromtheDATA=datasettotheendoftheBASE=dataset.TheobservationsintheBASE=datasetarenotreadorprocessed.
2.TheDATAwithSETdoesnotperformanupdateinplace