BookmarkSubscribeRSS Feed
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;
5 REPLIES 5
Kurt_Bremser
Super User

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.

David_Billa
Rhodochrosite | Level 12

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?

Kurt_Bremser
Super User

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.

David_Billa
Rhodochrosite | Level 12

So, index is being retained after renaming the dataset name.

yabwon
Amethyst | Level 16

This:

data cars_new;
set cars;
run;

is not renaming. As @Kurt_Bremser wrote, use proc datasets to rename.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1533 views
  • 3 likes
  • 3 in conversation