BookmarkSubscribeRSS Feed
Melk
Lapis Lazuli | Level 10

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;

8 REPLIES 8
PGStats
Opal | Level 21
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
Melk
Lapis Lazuli | Level 10
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..
PGStats
Opal | Level 21

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

PG
Melk
Lapis Lazuli | Level 10
Not sure I know the difference but they are equal in values before and after formatting.
PGStats
Opal | Level 21

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
Melk
Lapis Lazuli | Level 10
it seems the ID needs to be numeric for it to work. ID as character did not work for me.
PGStats
Opal | Level 21

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1332 views
  • 1 like
  • 3 in conversation