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
Try this.
data all;
if _n_=1 then set have2;
set have1;
run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.