Date: Fri, 12 Apr 1996 10:04:58 -0400
Reply-To: Ronald Cody EdD <cody@UMDNJ.EDU>
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: Ronald Cody EdD <cody@UMDNJ.EDU>
Subject: Re: HELP: "Merging" datasets
In-Reply-To: <no.id> from "Anders Brabaek" at Apr 12, 96 01:59:03 pm
Anders Brabaek writes:
>
> I have a small problem "merging" 2 datasets.
>
> One dataset, dataset1,consists of one observation and 2 variables: y1, y2.
> The other dataset, dataset2, consists of 7600 observations, each observation has 3 variables: x1, x2, x3.
> Now I want to put these two datasets together,
> so that I will get 7600 observations,.each consisting of 2+3=5 variables.
>
> Like this:
> Dataset1
> Obs y1 y2
> 1 1 0
>
> Dataset2:
> Obs x1 x2 x3
> 1 m 24 33
> 2 f 45 98
> etc
>
> Final
> Obs x1 x2 x3 y1 y2
> 1 m 24 33 1 0
> 2 f 45 98 1 0
> ...
> 7600 m 18 70 1 0
>
>
> When I run the following code
>
> data Final;
> merge dataset1 dataset2;
> run;
>
> I only get the observation from dataset1
> added to the first observation in dataset2,
> but I want to add it to all of the observations in dataset2.
>
> I suppose this should be a fairly easy operation,
> but being a novice with SAS I need help.
>
> Please, I need help.
> Thanks in advance.
> Anders Brabaek
>
Actually, it is not so easy or obvious. You need to employ a "trick" if
you use a data step approach. If you do a conditional SET statement on
the data set with one observation, the values (Y1 and Y2) will be
RETAINED (variables coming in with a SET are automatically retained) and
will be added to all obs in the other data set. The code (not tested)
would look like this:
DATA FINAL;
SET DATASET2;
IF _N_ = 1 THEN SET DATASET1;
RUN;
Hope this helps. You can see an example of this (and lots of other
examples) in SAS Programming by Example (Cody and Pass) available from
the SAS Institute. Adding a single observation to each obs in another
data set is covered on page 190. Ron
--
-----------------------------------------------------
| Ronald Cody, Ed.D. |
| Robert Wood Johnson Medical School |
| Department of Environmental and Community Medicine |
| 675 Hoes Lane |
| Piscataway, NJ 08854 |
| -------------------------------------------------- |
| Voice Phone (908)235-4490 |
| Fax (908)235-4569 |
| E-Mail CODY@UMDNJ.EDU |
-----------------------------------------------------
|