📄 m20 - combining sas data sets - 47 of 49.mht
字号:
observations have been processed. The new data set contains all =
the=20
variables and the total number of observations from all input data =
sets. <PRE> <FONT color=3D#003399>data interlv;</FONT>
<FONT color=3D#003399>set a b;</FONT>
<FONT color=3D#003399>by num;</FONT>
<FONT =
color=3D#003399>run;</FONT></PRE><STRONG>Match-Merging</STRONG><BR>Someti=
mes=20
you need to combine observations from two or more data sets into a =
single=20
observation in a new data set according to the values of a =
same-named=20
variable. This is <A=20
onmouseover=3D"window.status=3D'Display details on =
match-merging.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D'';"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m20/m20_14.htm">match-merging=
</A>,=20
which uses a MERGE statement rather than a SET statement to =
combine data=20
sets. Each input data set must be sorted or indexed in ascending =
order=20
based on the BY variable(s). During match-merging, SAS =
sequentially checks=20
each observation of each data set to see whether the BY values =
match, then=20
writes the combined observation to the new data set. <PRE> =
<FONT color=3D#003399>data merged;</FONT>
<FONT color=3D#003399>merge a b;</FONT>
<FONT color=3D#003399>by num;</FONT>
<FONT color=3D#003399>run;</FONT></PRE>
<P><STRONG>Match-Merge Processing</STRONG><BR>To predict the =
results of=20
match-merging correctly, you need to understand how the DATA step=20
processes data in match-merges.=20
<BLOCKQUOTE><A=20
onmouseover=3D"window.status=3D'Display details on the =
compilation phase of match-merging.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D''"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m20/m20_22.htm"><STRONG>Compi=
ling</STRONG></A><BR>To=20
prepare to merge data sets, SAS=20
<OL>
<LI>reads the descriptor portions of the data sets that are =
listed in=20
the MERGE statement=20
<LI>reads the rest of the DATA step program=20
<LI>creates the program data vector (PDV), an area of memory =
where SAS=20
builds your data set one observation at a time=20
<LI>assigns a tracking pointer to each data set that is listed =
in the=20
MERGE statement. </LI></OL>
<P>If variables with the same name appear in more than one data =
set, the=20
variable from the first data set that contains the variable (in =
the=20
order listed in the MERGE statement) determines the length of =
the=20
variable.=20
<P><A=20
onmouseover=3D"window.status=3D'Display details on the execution =
phase of match-merging.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D'';"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m20/m20_24.htm"><STRONG>Execu=
ting</STRONG></A><BR>After=20
compiling the DATA step, SAS sequentially match-merges =
observations by=20
moving the pointers down each observation of each data set and =
checking=20
to see <STRONG>whether the BY values match</STRONG>.=20
<UL>
<LI>If <STRONG>Yes</STRONG>, the observations are written to =
the PDV=20
in the order in which the data sets appear in the MERGE =
statement.=20
Values of any same-named variable are overwritten by values of =
the=20
same-named variable in subsequent data sets. SAS writes the =
combined=20
observation to the new data set and retains the values in the =
PDV=20
until the BY value changes in all the data sets. <BR><BR>
<LI>If <STRONG>No</STRONG>, SAS determines which of the values =
comes=20
first and writes the observation that contains this value to =
the PDV.=20
Then the observation is written to the new data set. =
</LI></UL>When the=20
BY value changes in all the input data sets, the PDV is =
initialized to=20
missing. The DATA step merge continues to process every =
observation in=20
each data set until it has processed all observations in all =
data sets.=20
<P><A=20
onmouseover=3D"window.status=3D'Display details on unmatched =
observations and missing values.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D''"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m20/m20_28.htm"><STRONG>Handl=
ing=20
Unmatched Observations and Missing Values</STRONG></A><BR>All=20
observations that are written to the PDV, including observations =
that=20
have missing data and no matching BY values, are written to the =
output=20
data set.=20
<UL>
<LI>If an observation contains missing values for a variable, =
then the=20
observation in the output data set contains the missing values =
as=20
well. Observations that have missing values for the BY =
variable appear=20
at the top of the output data set.<BR><BR>
<LI>If an input data set doesn't have a matching BY value, =
then the=20
observation in the output data set contains missing values for =
the=20
variables that are unique to that input data set.=20
</LI></UL></BLOCKQUOTE><STRONG>Renaming =
Variables</STRONG><BR>Sometimes=20
you might have same-named variables in more than one input data =
set. In=20
this case, match-merging <A=20
onmouseover=3D"window.status=3D'Display details on renaming =
variables.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D'';"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m20/m20_31.htm">overwrites=20
values</A> of the same-named variable in the first data set with =
values of=20
the same-named variable in subsequent data sets. To prevent =
overwriting,=20
use the RENAME=3D data set option in the MERGE statement to rename =
variables.=20
<P><STRONG>Excluding Unmatched Observations</STRONG><BR>By =
default,=20
match-merging combines all observations in all input data sets. =
However,=20
you might want to select only observations that match for two or =
more=20
input data sets. To exclude <A=20
onmouseover=3D"window.status=3D'Display details on excluding =
unmatched observations.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D'';"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m20/m20_36.htm">unmatched=20
observations</A>, use the IN=3D data set option and the subsetting =
IF=20
statement in your DATA step. The IN=3D data set option creates a =
variable to=20
indicate whether the data set contributed data to the current =
observation.=20
The subsetting IF statement then checks the IN=3D values and =
writes to the=20
merged data set only observations that appear in the data sets for =
which=20
IN=3D is specified.=20
<P><STRONG>Selecting Variables</STRONG><BR>You can specify the =
variables=20
you want to drop or keep by using the <A=20
onmouseover=3D"window.status=3D'Display details on selecting =
variables.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D'';"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m20/m20_42.htm">DROP=3D and =
KEEP=3D=20
data set options</A>. When match-merging, you can specify these =
options in=20
either the DATA statement or the MERGE statement, depending on =
whether or=20
not you want to process values of the variables in that DATA step. =
When=20
used in the DATA statement, the DROP=3D option simply drops the =
variables=20
from the new data set. However, they are still read from the =
original data=20
set and are available within the DATA step. =
</P></TD></TR></TBODY></TABLE><!-- InstanceEndEditable --><BR>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"85%" align=3Dcenter =
border=3D0>
<TBODY>
<TR>
<TD align=3Dleft>
<HR>
</TD></TR></TBODY></TABLE>
<P><!-- InstanceBeginEditable name=3D"syntax" -->
<CENTER>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"85%" border=3D0>
<TBODY>
<TR>
<TD><STRONG><A name=3DSyntax>Syntax</A></STRONG>=20
<P><EM>To go to the page where a statement or option was =
presented, select=20
a link.</EM> </P></TD></TR></TBODY></TABLE>
<P align=3Dcenter>
<CENTER>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"85%" border=3D0>
<TBODY>
<TR>
<TD>
<BLOCKQUOTE>
<P><STRONG>One-to-One Reading</STRONG> </P>
<DL>
<DT><FONT color=3D#003399><STRONG>LIBNAME</STRONG> =
<EM>libref</EM>=20
'<EM>SAS-data-library</EM>'<STRONG>;</STRONG></FONT>=20
<DT><FONT color=3D#003399><STRONG>DATA</STRONG>=20
<EM>output-SAS-data-set</EM><STRONG>;</STRONG></FONT>=20
<DD><FONT color=3D#003399><A=20
onmouseover=3D"window.status=3D'Display details on one-to-one =
reading.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D'';"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m20/m20_2.htm"><STRONG>SET</S=
TRONG>=20
<EM>SAS-data-set-1</EM><STRONG>;</STRONG></A></FONT>=20
<DD><FONT color=3D#003399><A=20
onmouseover=3D"window.status=3D'Display details on one-to-one =
reading.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D'';"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m20/m20_2.htm"><STRONG>SET</S=
TRONG>=20
<EM>SAS-data-set-2</EM><STRONG>;</STRONG></A></FONT>=20
<DT><FONT color=3D#003399><STRONG>RUN;</STRONG></FONT> =
</DT></DL>
<P><STRONG>Concatenating</STRONG> </P>
<DL>
<DT><FONT color=3D#003399><STRONG>DATA</STRONG>=20
<EM>output-SAS-data-set</EM><STRONG>;</STRONG></FONT>=20
<DD><FONT color=3D#003399><A=20
onmouseover=3D"window.status=3D'Display details on =
concatenating.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D'';"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m20/m20_7.htm"><STRONG>SET</S=
TRONG>=20
<EM>SAS-data-set-1 =
SAS-data-set-2</EM><STRONG>;</STRONG></A></FONT>=20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -