Macro variables - Can I fetch a variable label from its name?

Reply
Contributor
Posts: 41

Macro variables - Can I fetch a variable label from its name?

Hello,

I am currently creating a macro to execute some analysis, and I am passing as parameters the name of the variables I want to use for the test (i.e.: the dependent and independent variables).

I am adding TITLE and FOOTNOTE lines to my code that should mention the variables being used. However, instead of using &var1 or &var2 to get the name of the variables used, I would like to use the label of the variables being used.

Is there a way to pull the label for the variables from the variable name?

Regards

,P.

PROC Star
Posts: 7,467

Re: Macro variables - Can I fetch a variable label from its name?

Yes, but it depends how you want to access it.  e.g., you could get it from dictionary.columns:

data class;

  set sashelp.class;

  label sex=gender;

run;

proc sql noprint;

  select label into :label

    from dictionary.columns

    where libname="WORK" and

          memname="CLASS" and

          upcase(name)="SEX"

  ;

quit;

%put &label;

There are also vlabel and vlabelx functions.

Respected Advisor
Posts: 3,799

Re: Macro variables - Can I fetch a variable label from its name?

I would create new variables that contain the values you are looking for and use thoes as BY variables.  You can put them in the TITLES/FOOTNOTES using the #BYval/var directives.  Create the new variables with VLABEL/VNAME or PROC TRANSPOSE

Contributor
Posts: 41

Re: Macro variables - Can I fetch a variable label from its name?

Posted in reply to data_null__

Hi Folks,

Thank you for trying to help.

I am totally new to SAS so I am struggling to understand the suggestions.

Arthur: I am not using PROC SQL in my code, so I am having a hard time understanding the bis and bulbs of you example and if/how I could use it in my macro.

dat_null__: I am not sure how to use the VLABEL/VNAME statement and the #BYval/var directives.

Here is my macro code in case it helps:

%MACRO twot(dataSrc, depVar, factVar);

TITLE "Hypothesis Test - H0: Mean &depVar by factor &factVar are equal";

PROC TTEST DATA=&dataSrc;

VAR &depVar;

CLASS &factVar;

RUN;

%MEND twot;

Instead of showing the variable names for &depVar and &factVar, I would like instead to show their label. So if I pass on ROI_Y1 as a depVar (dependent variable) then in the title I would like it to say Return of Investment Year 1 rather than ROI_Y1.

Thanks for helping me with this one.

Best wishes,

P.

Respected Advisor
Posts: 3,799

Re: Macro variables - Can I fetch a variable label from its name?

This example does not use #BY directives.  It creates two new macro variables to hold the labels.  If you want to also use the variable names as I did the CALL SYMPUTX to redefine those variable insures that when the value are printted they are "spelled" the same as in your data set.


If you have many variable pairs you could transpose your data such that you have BY variables for the VARs and LABELs.  If you want to see that post some sample data.

%MACRO twot(dataSrc, depVar, factVar,where=);
data _null_;
   if
0 then set &datasrc;
   call symputX('depvar',vname(&depvar));
   call symputX('deplbl',vlabel(&depvar));
   call symputX('factvar',vname(&factvar));
   call symputX('factlbl',vlabel(&factvar));
   STOP; *important!;
   run;

TITLE
"Hypothesis Test - H0: Mean &depVar:&deplbl by factor &factVar:&factlbl are equal"
PROC TTEST DATA=&dataSrc; 
where &where;
VAR &depVar; 
CLASS &factVar; 
RUN; 
%MEND twot;

%
twot(sashelp.gas,nox,fuel,where=fuel inSmiley Sad'E' 'I'));
Ask a Question
Discussion stats
  • 4 replies
  • 296 views
  • 4 likes
  • 3 in conversation