How to generate error in log for duplicte records
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;
How big is your data set?
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.