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?
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;
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?
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;
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.