SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Angel_Saenz
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

View solution in original post

5 REPLIES 5
ballardw
Super User

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.

 

 

Angel_Saenz
Quartz | Level 8
thanks ll the variables var var2 and var3 have duplicate, thats is ok, because I need is no is the duplicate value in combination of that three
So there is no way to establish a unique to 3 variables together unless you do not have duplicates?
Patrick
Opal | Level 21

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;
Angel_Saenz
Quartz | Level 8
thanks
Tom
Super User Tom
Super User

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.

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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