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;
Your code does not rename a dataset anywhere. All I see is a data step which creates a new dataset (which, of course, will not have an index unless you create one).
Renaming a dataset is done with the CHANGE statement of PROC DATASETS.
There is outage time now due to licence renewal. In the interest of time, I'd like to know whether the index will be retained if we rename the dataset via proc datasets?
Answer this yourself. Run this code:
%let workpath=%sysfunc(pathname(work));
%put &=workpath.;
data class (index=(sex));
set sashelp.class;
run;
proc datasets lib=work noprint;
change class=class1;
quit;
data dirlist;
length dref $8;
rc = filename(dref,"&workpath.");
did = dopen(dref);
do i = 1 to dnum(did);
name = dread(did,i);
put name=;
end;
rc = dclose(did);
rc = filename(dref);
run;
and read the log.
So, index is being retained after renaming the dataset name.
This:
data cars_new; set cars; run;
is not renaming. As @Kurt_Bremser wrote, use proc datasets to rename.
Bart
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.