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
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);
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.
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);
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.