Here is one way to check for duplicate values, display a message to the user if they enter a duplicate, and also prevent them from leaving the observation when a duplicate value is present in the field being checked for duplicates. I am using a copy of the SASHELP.CLASS table, and the NAME column for this example. You could use a numeric column, but this would require changing the SEARCHC function to SEARCHN. At this point, this example considers uppercase and lowercase letters as different values. So you would be able to enter the following names as unique values: Jane, JANE, jane.
/* FSEINIT executes once when the screen is first opened */
/* create an SCL list with text to display in a messagebox */
FSEINIT:
msglist=makelist();
rc=insertc(msglist,'The NAME entered is not unique for in the table',-1);
rc=insertc(msglist,'Please enter a unique NAME',-1);
return;
/* INIT executes when you access a different observation in the table */
/* fill an SCL list with the unique values of a column in the table
that will be used to check newly entery values against */
INIT:
dsid=open('sashelp.class');
namelist=makelist();
n=0;
rc=lvarlevel(dsid,'name',n,namelist);
dsid=close(dsid);
return;
/* MAIN executes when there are actions on the screen - typically when you
press the ENTER key, but this can be modified using the CONTROL statement. I like to isolate validation by executing it when something changes. In This case, when the NAME field is modified on the screen. There is no need to check for duplicate NAME values if the NAME field is not modified. If the field is identified as a duplicate, I set the ERROR condition, and the user will not be able to leave the observation until they change the
value, or CANCEL the edit*/
MAIN:
erroroff _all_;
if modified(name) then
do;
if searchc(namelist,name) > 0 then
do;
erroron name;
text=messagebox(msglist,'S','O');
cursor name;
end;
end;
return;
/* TERM executes when you leave the observation that you are currently
viewing. I delete the NAMELIST that will be recreated when you
access a new observation */
TERM:
rc=dellist(namelist);
return;
... View more