1

According to Understanding SAS Indexes

If you rename key variable in SAS, the simple index is renamed instead of being deleted.

However, when I try this on my own, the index file will disappear after I rename the key variable, I wonder what is wrong?

Here is the code that I used:

data work.temp(index=(id));
input id $ amount;
cards;
p       52
p       45
a       13
a       56
r       34
r       12
r       78
;
proc contents data=work.temp;
run;

data WORK.temp ;
set WORK.temp (keep=Id amount rename=(Id=Id_Code));
run;

proc contents data=work.temp;
run;

You can see from the result that after renaming the key variable Id, the index disappears.

Thank you very much!

lleiou
  • 475
  • 7
  • 13

2 Answers2

4

It would disappear even if you didn't rename the variable.

data temp;
set temp;
run;

That destroys the index as well. The reason is that the Data Step is replacing the original data set with a new one. All the information about indexes are lost in that replace.

Instead, use PROC DATASETS to modify the data set in place:

proc datasets lib=work nolist;
modify temp;
rename id=id_code;
run;
quit;
DomPazz
  • 12,415
  • 17
  • 23
-1

Use MODIFY instead of SET.

DATA temp;
  MODIFY temp (RENAME=id=id_code);
RUN;
Dave O
  • 19
  • 4
  • this doesn't work. The rename is not supported with Modify and is ignored. At least not on the version I have. – DomPazz Oct 26 '16 at 17:15
  • You're right. I didn't know MODIFY was limited in a DATA step. Strange that is doesn't give any notes, warnings or errors when this is run. – Dave O Oct 26 '16 at 17:50
  • @DomPazz The RENAME data set option is NOT ignored, the PDV contains the RENAMED variable you can verify this with PUT \_ALL\_; Id_Code=p amount=52 _ERROR_=0 _IORC_=0 _N_=1 This does not change the name in TEMP as you have noted. – data _null_ Oct 27 '16 at 13:07