Hi:
I have a dataset with 250 columns and would like to convert all the sas labels into variable names. How to do it?
Thanks!
Are they all applicable as SAS variable names?
Yes, they are all sas variable names.
@rajredy7172012 wrote:
I have a dataset with 250 columns and would like to convert all the sas labels into variable names. How to do it?
Why would you want to do that?
Generate an series of OLD=NEW pairs and use them in a RENAME statement or RENAME= dataset option. Note that labels can have up to 255 bytes, but names can only have 32. If the labels do not follow normal SAS variable naming rules then you will need to use VALIDVARNAME=ANY system option. You cannot use RENAME to change the case of a name. If you need to do that you will need to do a two step approach where you first change it something else and back to the name with different case for some of the letters. Also make sure that all of the labels are unique. You cannot have two variables with the same name.
Say your datasets is named MYDATA and it is pointed at by the libref MYLIB.
proc sql noprint;
select catx('=',nliteral(name),nliteral(substr(label,1,32)))
into :renames separated by ' '
from dictionary.columns
where libname='MYLIB'
and memname='MYDATA'
and upcase(name) ne upcase(substr(coalesce(label,name),1,32))
;
quit;
%if (&sqlobs > 0) %then %do;
proc datasets nolist lib=MYLIB;
modify mydata;
rename &renames;
run;
quit;
%end;
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.