SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
rajredy7172012
Calcite | Level 5

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!

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Are they all applicable as SAS variable names?

rajredy7172012
Calcite | Level 5

Yes, they are all sas variable names.

PaigeMiller
Diamond | Level 26

@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?

--
Paige Miller
Tom
Super User Tom
Super User

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;

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 4 replies
  • 1026 views
  • 1 like
  • 4 in conversation