Lesson Summary
SummaryofLesson10:CombiningSASDataSets
Thissummarycontainstopicsummaries,syntax,andsampleprograms.
TopicSummaries
Togotothemoviewhereyoulearnedataskorconcept,selectalink.
ConcatenatingDataSets
Youcanconcatenatetwoormoredatasetsbycombiningthemverticallytocreateanewdataset.It
isimportanttoknowthestructureandcontentsoftheinputdatasets.
YouuseaDATAsteptoconcatenatemultipledatasetsintoasingle,newdataset.IntheSET
statement,youcanspecifyanynumberofinputdatasetstoconcatenate.Duringcompilation,SAS
usesthedescriptorportionofthefirstdatasettocreatevariablesinthePDV,andthencontinues
witheachsubsequentdataset,creatingadditionalvariablesinthePDVasneeded.During
execution,SASprocessesthedatasetsintheorderinwhichtheyarelistedintheSETstatement.
DATASASdataset
SETSASdataset1SASdataset2...
RUN
Ifthedatasetshavedifferentlynamedvariables,everyvariableiscreatedinthenewdataset,and
someobservationshavemissingvaluesforthedifferentlynamedvariables.Youcanusethe
RENAME=datasetoptiontochangevariablenamesinoneormoredatasets.Aftertheyare
renamed,theyaretreatedasthesamevariableduringcompilationandexecution,andinthenew
dataset.
SASdataset(RENAME=(oldname1=newname1
oldname2=newname2
...
oldnamen=newnamen))
MergingSASDataSetsOnetoOne
MergingcombinesobservationsfromtwoormoreSASdatasetsintoasingleobservationinanew
dataset.Asimplemergecombinesobservationsbasedontheirpositionsintheoriginaldatasets.A
matchmergecombinesthembasedonthevaluesofoneormorecommonvariables.Theresultofa
matchmergeisdependantontherelationshipbetweenobservationsintheinputdatasets.
YouuseaDATAstepwithaMERGEstatementtomergemultipledatasetsintoasingledataset.
TheBYstatementindicatesamatchmergeandspecifiesthecommonvariableorvariablesto
match.ThecommonvariablesarereferredtoasBYvariables.TheBYvariablesmustexistinevery
dataset,andeachdatasetmustbesortedbythevalueoftheBYvariables.
DATASASdataset
MERGESASdataset1SASdataset2...
BY<DESCENDING>BYvariable(s)
<additionalSASstatements>
RUN
MergingSASDataSetsOnetoMany
Inaonetomanymerge,asingleobservationinonedatasetmatchesmorethanoneobservationin
anotherdataset.TheDATAstepisthesame,regardlessoftherelationshipbetweenthedatasets
https://support.sas.com/edu/OLTRN/ECPRG193/m421/m421_5_a_sum.htm
1/4
1/16/2015
Lesson Summary
beingmerged.SASprocesseseachBYgroupbeforereinitializingthePDV.
MergingSASDataSetsThatHaveNonMatches
Whenyoumergedatasets,observationsinonedatasetmightnothaveamatchingobservationin
anotherdataset.Thesearecallednonmatches.Bydefault,bothmatchesandnonmatchesare
includedinamergeddataset.Theobservationsthatarematchescontaindatafromeveryinput
dataset.Thenonmatchingobservationsdonotcontaindatafromeveryinputdataset.
YoucanusetheIN=datasetoptioninaMERGEstatementtocreateatemporaryvariablethat
indicateswhetheradatasetcontributedinformationtotheobservationinthePDV.TheIN=
variableshavetwopossiblevalues:0and1.Youcantestthevalueofthisvariableusingsubsetting
IFstatementstooutputonlythematchesoronlythenonmatchestothemergeddataset.
MERGESASdataset1<(IN=variable)>...
SamplePrograms
ConcatenatingDataSetswithDifferentVariables
**********CreateData**********
dataempscn
inputFirst$Gender$Country$
datalines
ChangMChina
LiMChina
MingFChina
run
dataempsjp
inputFirst$Gender$Region$
datalines
ChoFJapan
TomiMJapan
run
**********UnlikeStructuredDataSets**********
dataempsall2
setempscnempsjp
run
procprintdata=empsall2
run
MergingDataSetsOnetoOne
**********CreateData**********
dataempsau
inputFirst$Gender$EmpID
datalines
TogarM121150
KylieF121151
BirinM121152
run
dataphoneh
inputEmpIDPhone$15.
datalines
121150+61(2)55551793
https://support.sas.com/edu/OLTRN/ECPRG193/m421/m421_5_a_sum.htm
2/4
1/16/2015
Lesson Summary
121151+61(2)55551849
121152+61(2)55551665
run
**********MatchMergeOnetoOne**********
dataempsauh
mergeempsauphoneh
byEmpID
run
procprintdata=empsauh
run
MatchMergingDataSetswithNonMatches
**********CreateData**********
dataempsau
inputFirst$Gender$EmpID
datalines
TogarM121150
KylieF121151
BirinM121152
run
dataphonec
inputEmpIDPhone$15.
datalines
121150+61(2)55551795
121152+61(2)55551667
121153+61(2)55551348
run
**********MatchMergewithNonMatches**********
dataempsauc
mergeempsauphonec
byEmpID
run
procprintdata=empsauc
run
SelectingNonMatches
**********CreateData**********
dataempsau
inputFirst$Gender$EmpID
datalines
TogarM121150
KylieF121151
BirinM121152
run
dataphonec
inputEmpIDPhone$15.
datalines
121150+61(2)55551795
121152+61(2)55551667
121153+61(2)55551348
run
https://support.sas.com/edu/OLTRN/ECPRG193/m421/m421_5_a_sum.htm
3/4
1/16/2015
Lesson Summary
**********NonMatchesfromempsauOnly**********
dataempsauc2
mergeempsau(in=Emps)
phonec(in=Cell)
byEmpID
ifEmps=1andCell=0
run
procprintdata=empsauc2
run
SASProgramming1:Essentials
Copyright2014SASInstituteInc.,Cary,NC,USA.Allrightsreserved.
Close
https://support.sas.com/edu/OLTRN/ECPRG193/m421/m421_5_a_sum.htm
4/4