I am trying to merge two datasets:
It's a combination of two factors. First, it's likely you tested and re-tested, trying to get this to work. And second, you are reusing the name DATASET1 as both the input and the output.
All of that is legal. However, it likely created the situation where DATASET1 already contains MYTEXT (possibly with all missing values at some point along the way). Get rid of it.
Order of sets on the MERGE statement makes a difference:
data combined;
merge types dataset1;
by texttype;
run;
But Many to many is not going to work as desired many types.
What you are doing is a JOIN. Merge is doing exactly what the documentation says it will.
Another way that is more flexible is something like this. Also the JOIN using SQL does not require sorting the data first.
proc sql;
create table combined as
select dataset1.*, types.mytext
from dataset1 left join types
on dataset1.texttype = types.texttype;
quit;
BTW you dataset code for TYPES has an error as MYTEXT is considered numeric as you haven't defined it as character and will generate missing valures for MYTEXT and error messages of invalid data.
data dataset1;
merge dataset1 types;
BY textType something;
RUN;
You need to specify the key of the somethingX field.
Why!!??
Your code is fine and should work...
data dataSet1;
input textType $ 1-5 textSome $ 7-17 ;
cards;
type1 something1
type1 something2
type1 something3
type2 something1
type2 something2
type2 something3
type3 something1
type3 something2
type3 something3
;run;
data types;
input myText $ textType $3-7;
datalines;
a type1
b type2
c type3
;
run;
proc sort data=dataset1; by textType; run;
proc sort data=types; by textType; run;
data dataset1;
merge dataset1 types;
BY textType;
drop textType;
RUN;
Try run the code above, you did not show your full code.
It's a combination of two factors. First, it's likely you tested and re-tested, trying to get this to work. And second, you are reusing the name DATASET1 as both the input and the output.
All of that is legal. However, it likely created the situation where DATASET1 already contains MYTEXT (possibly with all missing values at some point along the way). Get rid of it.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.