change label case

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

change label case

Hi,

 

I have following dataset with label names:

 

data class;

set sashelp.class;

label name="NAme"

sex="GEndeR"

age="Age"

height="HeiGht"

weight="kilograms";

run;

 

I need to change all label names to upcase/lowcase/propcase for all variables in one step.

Can anyone please help?


Accepted Solutions
Solution
‎05-09-2017 10:47 PM
Super User
Posts: 17,840

Re: change label case

Here's another way, some benefits to this method:

 

1. Proc datasets - doesn't recreate the dataset directly so it's faster

2. Uses SASHELP.VCOLUMN/Dictionary.Column that can be leveraged across multiple datasets

3. Accounts for variables that may not have labels, in this case it uses the variable name. 

4. This uses propcase. You can change it to lowcase or upcase as preferred.

 

data class;
    set sashelp.class;
    label name="NAme" sex="GEndeR" age="Age" height="HeiGht" weight="kilograms";
run;

proc sql noprint;
    select catx('=', name, quote(propcase(coalescec(trim(label), name)))) 
        into :label_list separated by " " from sashelp.vcolumn where libname='WORK' 
        and upper(memname)='CLASS';
quit;

proc datasets lib=work nodetails nolist;
    modify class;
    label &label_list;
    run;
quit;

proc print data=class label;
run;

View solution in original post


All Replies
Super User
Posts: 17,840

Re: change label case

Are you familiar with using the SASHELP.VCOLUMN dataset? I suggest you look there first as it has the labels all stored in the file as well variable names so you can derive them easily in one place to convert them. 

 


I need to change all label names to upcase/lowcase/propcase for all variables in one step.

Can anyone please help?


 

Solution
‎05-09-2017 10:47 PM
Super User
Posts: 17,840

Re: change label case

Here's another way, some benefits to this method:

 

1. Proc datasets - doesn't recreate the dataset directly so it's faster

2. Uses SASHELP.VCOLUMN/Dictionary.Column that can be leveraged across multiple datasets

3. Accounts for variables that may not have labels, in this case it uses the variable name. 

4. This uses propcase. You can change it to lowcase or upcase as preferred.

 

data class;
    set sashelp.class;
    label name="NAme" sex="GEndeR" age="Age" height="HeiGht" weight="kilograms";
run;

proc sql noprint;
    select catx('=', name, quote(propcase(coalescec(trim(label), name)))) 
        into :label_list separated by " " from sashelp.vcolumn where libname='WORK' 
        and upper(memname)='CLASS';
quit;

proc datasets lib=work nodetails nolist;
    modify class;
    label &label_list;
    run;
quit;

proc print data=class label;
run;
Contributor
Posts: 27

Re: change label case

Thank you Reeza
Super User
Posts: 5,084

Re: change label case

Even if you know how to automate the retrieval of labels, it's not necessarily simple to change the labels.  And there's certainly more than one approach to doing it.  Here's a method that retrieves the labels from PROC CONTENTS:

 

proc contents data=have noprint out=_contents_ (keep=name label where=(label > ' '));

run;

 

Then applying them might look like this (this is untested code, but should be working):

 

proc sql;

select name || '= "' || propcase(label) || '"' into : label_list from _contents_ separated by ' ' ;

run;

 

data want;

set have;

label &label_list;

run;

Super User
Posts: 10,500

Re: change label case

Propcase, while a very helpful function in many cases does have some weekness especially if you have text such as 'mg/dl' where propcase will return 'Mg/Dl' which is not quite standard or will turn "MacDonald" into "Macdonald". If your data doesn't hanve any of these then no problem but there may be a bit of manual clean up on process still.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 132 views
  • 0 likes
  • 4 in conversation