DATA Step, Macro, Functions and more

flagging based on a different datset

Reply
Regular Contributor
Posts: 150

flagging based on a different datset

I have 2 datasets, data1 is repeated and data2 is not. I would like to add a flag=1 in data1 if ID is in data2 and flag=0 if not. I have the following code but it is marking all as flag=0, can you please help me troubleshoot?

 

   
data dataflagged;
  if _N_ = 1 then do;  
    declare hash CHK(dataset: "data2", multidata:'y');
    CHK.definekey("ID");
    CHK.definedone();
  end;
  set data1;
  flag = (CHK.check()=1);
run;
quit;

PROC Star
Posts: 2,311

Re: flagging based on a different datset

If found, CHK.check()=0.

Esteemed Advisor
Posts: 5,475

Re: flagging based on a different datset

data dataflagged;
  if _N_ = 1 then do;  
    declare hash CHK(dataset: "data2");
    CHK.definekey("ID");
    CHK.definedone();
  end;
  set data1;
  flag = CHK.check() = 0;
run;
PG
Regular Contributor
Posts: 150

Re: flagging based on a different datset

It is marking all flags as 0 for some reason. The ID variable are numbers formatted as 8.characters. in both datasets. I am just eyeballing and I see a lot of matching IDs, so I cant figure out why the flag isnt working..
Esteemed Advisor
Posts: 5,475

Re: flagging based on a different datset

Are they really equal in both datasets, or just equal once formatted?

PG
Regular Contributor
Posts: 150

Re: flagging based on a different datset

Not sure I know the difference but they are equal in values before and after formatting.
Esteemed Advisor
Posts: 5,475

Re: flagging based on a different datset

It *should* work.

 

data data1;
do id = 1,1,2,7,9; output; end;
format id 8.;
run;

data data2;
do id = 1,7; output; end;
format id 8.;
run;

data dataflagged;
  if _N_ = 1 then do;  
    declare hash CHK(dataset: "data2");
    CHK.definekey("ID");
    CHK.definedone();
  end;
  set data1;
  flag = CHK.check() = 0;
run;

proc print noobs; run;
                                        id    flag

                                         1      1
                                         1      1
                                         2      0
                                         7      1
                                         9      0

Post the actual code you are using.

 

PG
Regular Contributor
Posts: 150

Re: flagging based on a different datset

it seems the ID needs to be numeric for it to work. ID as character did not work for me.
Esteemed Advisor
Posts: 5,475

Re: flagging based on a different datset

This works:

 

data data1;
do id = "1","1","2","7","9"; output; end;
run;

data data2;
do id = "1","7"; output; end;
run;

data dataflagged;
  if _N_ = 1 then do;  
    declare hash CHK(dataset: "data2");
    CHK.definekey("ID");
    CHK.definedone();
  end;
  set data1;
  flag = CHK.check() = 0;
run;

proc print noobs; run;
                                                       id    flag

                                                       1       1
                                                       1       1
                                                       2       0
                                                       7       1
                                                       9       0
PG
Ask a Question
Discussion stats
  • 8 replies
  • 164 views
  • 1 like
  • 3 in conversation