BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
David_Billa
Rhodochrosite | Level 12
Currently I have the code as below to create the index.
 
Proc sql noprint;
CREATE INDEX division on sas_prompts_lib_table (division );
CREATE INDEX product_family on sas_prompts_lib_table (product_family );
CREATE INDEX upn on sas_prompts_lib_table (upn );
quit;
Now I want to dynamically create the index based on the field name from below SAS macro variable.
%let current_distinct_var_list = %sysfunc(prxchange(%bquote(s/\*/,/), -1, &current_typ));
Value of current_distinct_var_list can have any number of fields separated by comma. How to use this macro variable in create index for each field. May be scan function can helps but I'm not sure to implement. I was asked not create any macro for this as this solution will be added to other existing macro.
 
On a related point, I'd like to know if we can create the index on a table (e.g.,  libname.test) and rename the table (e.g. libname.test_1).  Does the index name get updated too?
 
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

The primary ways to create looping in SAS are in DATA steps, and via macros. There is no looping in SQL unless you create a macro that works inside of SQL. You can also create indexes in PROC DATASETS, but again, there is no looping without macros that work inside of PROC DATASETS. So ... I think those are your choices.

 

 

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

The primary ways to create looping in SAS are in DATA steps, and via macros. There is no looping in SQL unless you create a macro that works inside of SQL. You can also create indexes in PROC DATASETS, but again, there is no looping without macros that work inside of PROC DATASETS. So ... I think those are your choices.

 

 

--
Paige Miller
David_Billa
Rhodochrosite | Level 12

@PaigeMiller well, do you answer to the below question as well?

 

I'd like to know if we can create the index on a table (e.g.,  libname.test) and rename the table (e.g. libname.test_1).  Does the index name get updated too?
PaigeMiller
Diamond | Level 26

Typically, my answer to a question that wants to know if something works or not:

 

Try it yourself and see.

 

I think that applies here as well.

--
Paige Miller
David_Billa
Rhodochrosite | Level 12

I've tried the following program to create a index and I want to check whether index will be retained if we rename the dataset name which has index.

 

I observed that index is not being retained if we rename the dataset name. Is there a way to retain the index even if we rename the dataset name?

data cars;
set sashelp.cars;
run;

proc contents data=cars noprint
out=index_list(keep=libname memname name);
run;

filename code temp;
data _null_;
set index_list;
by libname memname ;
file code;
if first.libname then put 'proc datasets nolist lib=' libname ';' ;
if first.memname then put 'modify ' memname ';' / '  create index ' @;
put name @;
if last.memname then put ';' / 'run;' ;
if last.libname then put 'quit;' ;
run;

%include code/ source2;

data cars_new;
set cars;
run;

proc contents data=cars_new;
run;

proc contents data=cars;
run;
ballardw
Super User

First you have to realize that

data cars_new;
set cars;
run;

Does not "copy" a data set. It builds a completely new data set setting all of its own properties including indexes (none). Second it has to process every single record.

 

If you want to copy to a Different library (COPY means keep the same name in this case so you can't have two sets with the same name in the same library). Or Proc Copy but again it goes to a new library.

libname Dest "<library location>";
Proc datasets library=work;
   copy out=dest; /*name of destination library*/
    select cars;
run;
quit; 

Or create the Cars_new and run the index afterwards.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1170 views
  • 1 like
  • 3 in conversation