In the first Star Wars movie, Obi-wan uses Jedi mind tricks to convince the stormtroopers that the droids they see are not the droids they're looking for. A colleague at SAS passed along a question from a SAS user where the column labels they were seeing were NOT the labels they were looking for:
Does anybody have code to modify SAS column labels so that the variable name and label are simultaneously displayed when viewing the table? For example, we would like to create a new label that has the value 'VarName: Label for VarName' instead of just 'Label for VarName'.
I started thinking about how I would customize the labels using SAS code. I'd want the process to be as automated as possible, and I'd want to accomplish the task without rewriting all of the data in the table just to change the labels. I settled on using a little PROC SQL with its dictionary table magic, a pinch of SAS macro, and finally the raw power of PROC DATASETS to do the deed. Let's start by creating a dataset to play with:
data haircuts; input Name $ Amt; label name='Person' Amt='Price of Last Haircut'; datalines; Luke 25 Obi-wan 15 Darth 0 ;
The dataset (with labels displayed) looks like this:
Now, we'll use PROC SQL and the dictionary.columns table to construct the ATTRIB statements we'll deploy in PROC DATASETS:
proc sql noprint; select cat('attrib ', strip(Name), ' Label="',catx(': ',Name,Label),'";') into :code separated by ' ' from dictionary.columns where memname='HAIRCUTS' and LIBNAME='WORK' ; quit;
The macro variable CODE now holds all of the ATTRIB statements we will need in the subsequent PROC DATASETS step:
attrib Name Label="Name: Person"; attrib Amt Label="Amt: Price of Last Haircut";
Finally, we'll submit the PROC DATASETS code to make the changes to our dataset descriptor:
proc datasets lib=work memtype=data nolist; modify haircuts; &code run;
And violá! Labels just like the customer ordered:
So now, these are the labels we were looking for and my work here is done (for today, at any rate).
Until next time - may the SAS be with you!