Help using Base SAS procedures

get label of a macro variable

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

get label of a macro variable

Hi,

I am transforming some variables and want to keep their labels.

For example,

I took 3 months lags on each variable of interest.

proc expand data=in_a out=out_b method = none;

     id time;

     %inn_cvt(innvar3=&&inxvar&inn_i3.., innlg=3);

run;

where &&inxvar&inn_i3.. loop through a bunch of variables. After this transformation, I want to keep the label of pre-transform variable, but edit label for after-transform variable as " pre-transform variable label + Lag 3"

How can I do it? 

Thanks in advance.

Thanks,

Sunny


Accepted Solutions
Solution
‎03-09-2015 03:44 PM
Contributor
Posts: 74

Re: get label of a macro variable

Posted in reply to Sunny_Sun

Hi,

Something like this might do the trick:

data test1;

    attrib var1 label = 'Label of var 1'

           var2 label = 'Label of var 2'

           var3 label = 'Label of var 3';

    var1 = 1;

    var2 = 2;

    var3 = 3;

run;

proc contents data = test1 out = contents_test1 (keep = name label) ; run;

data contents_test2;

    set contents_test1;

    length labelname $100;

    labelname = 'label_'||strip(name);

run;

data _null_;

    set contents_test2;

    call symputx(labelname, label);

run;

%macro do_stuff(innlg= );

%do i = 1 %to 1;

    %put;

    %put i = &i;

    %put varname for &i -> &&varname&i;

    /* get label using varname */

    %put label for &&varname&i -> &&&&label_&&varname&i;

    %let new_label = Pre-transform &&&&label_&&varname&i + &innlg;

    %put new label = &new_label;

%end;

  

%mend;

%do_stuff(innlg = 3);

View solution in original post


All Replies
Super User
Posts: 11,343

Re: get label of a macro variable

Posted in reply to Sunny_Sun

You might want to use the VLabel function prior to the current step you are working with to add the labels of the desired variables.

Another approach would be to collect the information about the variables in your input data set before the step and then use the information after the expand to modify the labels.

proc sql;

     create table variableinformation as

     select *

     from dictionary.columns

     where member='IN_A' and libname='WORK';

quit;

Use the data in the resulting data set variableinformation to create the new labels and assign via proc datasets would be on way.

Solution
‎03-09-2015 03:44 PM
Contributor
Posts: 74

Re: get label of a macro variable

Posted in reply to Sunny_Sun

Hi,

Something like this might do the trick:

data test1;

    attrib var1 label = 'Label of var 1'

           var2 label = 'Label of var 2'

           var3 label = 'Label of var 3';

    var1 = 1;

    var2 = 2;

    var3 = 3;

run;

proc contents data = test1 out = contents_test1 (keep = name label) ; run;

data contents_test2;

    set contents_test1;

    length labelname $100;

    labelname = 'label_'||strip(name);

run;

data _null_;

    set contents_test2;

    call symputx(labelname, label);

run;

%macro do_stuff(innlg= );

%do i = 1 %to 1;

    %put;

    %put i = &i;

    %put varname for &i -> &&varname&i;

    /* get label using varname */

    %put label for &&varname&i -> &&&&label_&&varname&i;

    %let new_label = Pre-transform &&&&label_&&varname&i + &innlg;

    %put new label = &new_label;

%end;

  

%mend;

%do_stuff(innlg = 3);

Respected Advisor
Posts: 3,799

Re: get label of a macro variable

Posted in reply to Sunny_Sun

It is possible to write some macro code using SAS data set access function or depending on the version of SAS you are using DOSUBL. 

The details of your macro %INC_CVT will also play a role in how it can be implemented.

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 887 views
  • 6 likes
  • 4 in conversation