BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ramchinna24
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

5 REPLIES 5
Reeza
Super User

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?


 

Reeza
Super User

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;
ramchinna24
Obsidian | Level 7
Thank you Reeza
Astounding
PROC Star

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;

ballardw
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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