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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 886 views
  • 1 like
  • 3 in conversation