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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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