Hi everyone, Is it possible to create unique index on more than one variable?
For example I need unique index in three variables:
proc datasets library=lib1;
modify dataset1;
index create var1 / unique;
index create var2 / unique;
index create var3 / unique;
run;
the problem with that code is that it gives me an error since only one can have the only one
ERROR: Duplicate values not allowed on index var1 for file dataset1
and it's okay for var1 to have duplicates but not in combination with var2 and var3
I thought about concatenating var1, var2, var3 and giving that variable the unique index but I want to see if it is possible to do it directly in those 3 variables.
Because I need that dataset not to have duplicates by combining those 3 variables
Thanks
You can create a composite index that's unique as documented here.
See sample code below:
data work.demo;
set sashelp.class;
run;
proc datasets library=work;
modify demo;
index create my_index=(sex age height) / unique;
quit;
You would need to make sure that your index variables have no duplicate values.
If Var1 =1 appears more than once then you can't use it as a unique index. Period.
If you go the concatenation route and any of those variables are numeric you want to make sure to control how you convert numeric to text values (which would be the only way concatenation makes sense) as the default conversion of numeric to text used by the CAT functions may not yield what you expect.
You can create a composite index that's unique as documented here.
See sample code below:
data work.demo;
set sashelp.class;
run;
proc datasets library=work;
modify demo;
index create my_index=(sex age height) / unique;
quit;
So what you want is that the combination of three variables uniquely identify the observations.
So in terms of sorting you mean something like:
proc sort data=lib1.dataset1 nodupkey ;
by var1 var2 var3;
run;
In terms of indexes that means you want a composite index, an index based on the value or two or more variables.
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.