DATA Step, Macro, Functions and more

Using non-indexed merge and RETAIN to propagate a string literal down all rows

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Using non-indexed merge and RETAIN to propagate a string literal down all rows

SAS-Ler's

I'm trying to use a non-indexed merge (no BY statement) to pull together two data sets.  One is a one-observation data set with 5 variables that are string literals (constants); the other is a more traditional data set

with several observations and variables.  All variables are character.

Here is what I have so far :


--------------------------------------------------------------------------------------------------------------------------------------

                                                            Have1 data set                           14:46 Wednesday, July 2, 2014   1

                                                    Obs    desc1    desc2    desc3

                                                     1       one          cold       wet
                                                     2       two          warm      wet
                                                     3       three       cold        dry
                                                     4       four         warm      dry

--------------------------------------------------------------------------------------------------------------------------------------

                                                            Have2 data set                           14:46 Wednesday, July 2, 2014   2

                                 Obs   lit1                lit2               lit3                lit4                 lit5

                                  1     category1    category2    category3    category4    category5

I'm using this statement to combine the data sets, using a MERGE statement with RETAIN :

data all;

length lit1$ 32 lit2$ 32 lit3$ 32 lit4$ 32 lit5$ 32;

retain lit1--lit5;  /* <--- This should keep the lit varables from being reset to missing */

merge have1 have2;

run;

This is what I get for the merged data set :

--------------------------------------------------------------------------------------------------------------------------------------

                                                           Merged data set                           14:46 Wednesday, July 2, 2014   3

                   Obs      lit1         lit2         lit3         lit4         lit5       desc1    desc2    desc3

                    1     category1    category2    category3    category4    category5    one      cold      wet
                    2                                                                                                                two      warm      wet
                    3                                                                                                                three    cold      dry
                    4                                                                                                                four     warm      dry


What I want is for all lthe 'lit' variables to be non-missing in the merged data set :

                   Obs   lit1               lit2                lit3                 lit4                lit5               desc1  desc2  desc3

                    1     category1    category2    category3    category4    category5    one      cold      wet

                    2     category1    category2    category3    category4    category5    two      warm     wet

                    3     category1    category2    category3    category4    category5    three    cold      dry

                    4     category1    category2    category3    category4    category5     four     warm    dry

I tried altering the merge statement by adding a set statement :

data all;

length lit1$ 32 lit2$ 32 lit3$ 32 lit4$ 32 lit5$ 32;

retain lit1--lit5;

merge have1(in=a) have2(in=b);

if not b then set have2;

run;

But this created a data set with only two observations (although the 'lit' variables were propogated from the first obs to the second).

Any ideas ?  I have a feeling the solution may require an 'IF _N_=1 THEN...' statement or use of an array.

Thanks in advance.

Barry Walton


Accepted Solutions
Solution
‎07-02-2014 03:35 PM
Trusted Advisor
Posts: 1,204

Re: Using non-indexed merge and RETAIN to propagate a string literal down all rows

Try this.

data all;

if _n_=1 then set have2;

set have1;

run;

View solution in original post


All Replies
Solution
‎07-02-2014 03:35 PM
Trusted Advisor
Posts: 1,204

Re: Using non-indexed merge and RETAIN to propagate a string literal down all rows

Try this.

data all;

if _n_=1 then set have2;

set have1;

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 180 views
  • 0 likes
  • 2 in conversation