BookmarkSubscribeRSS Feed
kari
Calcite | Level 5

Hello,

 

I have a data set which has two different variables.

 

Var1: household

Var2: Person in one specific household

 

To get a unique respondent I need to merge Var1 an Var 2. I can't seem to figure out how to do this in SAS? I have tried some code but it doesn't make sense to me. Once I have merged these two variables. I then need to merge two datasets that both contain that merged variable.

 

Thank you very much

7 REPLIES 7
Kurt_Bremser
Super User

merge: combine datasets side-by-side.

You never "merge" variables. Variables can be concatenated.

Look if the catx() function does what you want.

Astounding
PROC Star

There is no need to do this.  If you want to merge two data sets, you are allowed to use more than one variable in the BY statement:

 

data want;

merge dataset1 dataset2;

by var1 var2;

run;

error_prone
Barite | Level 11
Almost always posting examples of the data you have and what you need as result increases the likelihood that someone posts usable code.
SuryaKiran
Meteorite | Level 14

Not sure if I understood your problem, Are you trying to find the var1 value in var2 or vice versa and treat them as one common variable. You can either use FIND(), INDEX(), PRXMATCH() function.

 

For Example:

data test ;
Var1="household";
Var2="Person in one specific household";
if find(var1,var2,'i') or find(var2,var1,'i') then var=var2;
run;
Thanks,
Suryakiran
kari
Calcite | Level 5

Hi there,

 

The userguide says merge but I think what it means is to combine the two as the information from both together will provide a unique participant. Does this make more sense? Thank you!

ballardw
Super User

@kari wrote:

Hi there,

 

The userguide says merge but I think what it means is to combine the two as the information from both together will provide a unique participant. Does this make more sense? Thank you!


User guide to what?

If this is preparing data for use by an external (not SAS) process then say so. Likely that means you want to use one of the concatenation functions, in a data step to combine the two values. Does the user guide provide any hints rules on how to combine them? Such as which order or provide a space between the fields or a special character, ensure only one blank/special character between words or any thing like that?

Are your current variables character or numeric?

Can provide a couple of examples of actual values and what the result should look like?

 

I would start with something like:

data need;

    set have;

    uniqueid = catx(' ',var1, var2);

run;

 

which would create a new variable with the values of the two variables and single space between them.

 

Kurt_Bremser
Super User

Right now this is all just guessing on our part. Please post usable example data (data step with datalines, see my footnotes), and what you expect out of it.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1592 views
  • 0 likes
  • 6 in conversation