BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aayushi_17
Quartz | Level 8

How to generate error in log for duplicte records

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

There are several ways to approach a problem like this. Here is one using the hash object

 

data testdata(drop=i j);
   array vars var1-var3;
   do i=1 to 100;
      do j=1 to dim(vars);
         vars[j]=rand('integer', 1, 10);
      end;
      output;
   end;
run;

data test2;
   if _N_ = 1 then do;
      declare hash h();
      h.defineKey('var1', 'var2', 'var3');
      h.defineDone();
   end;
 
   set testdata;
 
   rc=h.check();

   if rc=0 then do;
      put 'ERROR: Duplicate Row Detected in Observation No: ' _N_;
      stop;
   end;
   if h.check() ne 0 then do;
      output;
      h.add();
   end;
run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

How big is your data set?

PeterClemmensen
Tourmaline | Level 20

There are several ways to approach a problem like this. Here is one using the hash object

 

data testdata(drop=i j);
   array vars var1-var3;
   do i=1 to 100;
      do j=1 to dim(vars);
         vars[j]=rand('integer', 1, 10);
      end;
      output;
   end;
run;

data test2;
   if _N_ = 1 then do;
      declare hash h();
      h.defineKey('var1', 'var2', 'var3');
      h.defineDone();
   end;
 
   set testdata;
 
   rc=h.check();

   if rc=0 then do;
      put 'ERROR: Duplicate Row Detected in Observation No: ' _N_;
      stop;
   end;
   if h.check() ne 0 then do;
      output;
      h.add();
   end;
run;
Aayushi_17
Quartz | Level 8
Thank you so much
Ksharp
Super User
data testdata(drop=i j);
   array vars var1-var3;
   do i=1 to 100;
      do j=1 to dim(vars);
         vars[j]=rand('integer', 1, 10);
      end;
      output;
   end;
run;

data _null_;
if 0 then set testdata;
      declare hash h(dataset:'testdata',duplicate:'e');
      h.defineKey('var1', 'var2', 'var3');
      h.defineDone();
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4 replies
  • 1491 views
  • 1 like
  • 3 in conversation