SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

How to create a dummy variable based on if the value in one column can be found in another one?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

How to create a dummy variable based on if the value in one column can be found in another one?

Hi guys. I have two columns value1 and value2, in value1, say, I have three observations a, b, c, in value2, I have 4 observations a, a, b,m, I want to create a dummy variable match for each row in value1, if the value can be found in value 2, then match=1 otherwise it is 0. So for this dataset, it should be 1,1,0. Can anyone help me with this? Thanks a lot!


Accepted Solutions
Solution
‎09-25-2015 06:23 AM
Super Contributor
Posts: 340

Re: How to create a dummy variable based on if the value in one column can be found in another one?

Data A;
  Input value1 $ value2 $;
  Datalines;
a a
b a
c b
. m
;
Run;

Data B;
  If _N_ eq 1 Then Do;
    Declare Hash H (Dataset:'A (Drop=value1 Rename=(value2=value1)');
 H.Definekey('value1');
 H.Definedone();
 If 0 Then Set A (Drop=value1 Rename=(value2=value1));
  End;
  Set A;
  dummy=1-Min(H.Find(),1);
Run;

View solution in original post


All Replies
Solution
‎09-25-2015 06:23 AM
Super Contributor
Posts: 340

Re: How to create a dummy variable based on if the value in one column can be found in another one?

Data A;
  Input value1 $ value2 $;
  Datalines;
a a
b a
c b
. m
;
Run;

Data B;
  If _N_ eq 1 Then Do;
    Declare Hash H (Dataset:'A (Drop=value1 Rename=(value2=value1)');
 H.Definekey('value1');
 H.Definedone();
 If 0 Then Set A (Drop=value1 Rename=(value2=value1));
  End;
  Set A;
  dummy=1-Min(H.Find(),1);
Run;

Occasional Contributor
Posts: 10

Re: How to create a dummy variable based on if the value in one column can be found in another one?

Posted in reply to user24feb
Thanks so much! It works very well!
Trusted Advisor
Posts: 1,913

Re: How to create a dummy variable based on if the value in one column can be found in another one?

Use PROC GLMMOD to create dummy variables.

Occasional Contributor
Posts: 10

Re: How to create a dummy variable based on if the value in one column can be found in another one?

Posted in reply to PaigeMiller

Thanks PaigeMiller!

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 338 views
  • 2 likes
  • 3 in conversation