Renaming/Delete part of a label of a SAS variable in the dataset
Renaming/Delete part of a label of a SAS variable in the dataset
(OP)
I got a dataset with variables RESULTS00-RESULTS36, each with a label with something like 'Result blabla'. Now I want to remove the 'Result' from the label so it will be 'blabla' alone.
%MACRO rem_res(ds);
DATA data2;
SET &ds.;
%DO i=0 %TO 36;
res=TRANWRD(VLABEL(RESULTS0&i.),"Result","");
put res;
CALL SYMPUT('macr_res',res);
%PUT ¯_res.;
LABEL RESULTS0&i. = ¯_res.;
%END;
RUN;
%MEND;
However, using this code it does not recognize the macr_res anymore. Also, it puts just ¯_res., what's wrong?
%MACRO rem_res(ds);
DATA data2;
SET &ds.;
%DO i=0 %TO 36;
res=TRANWRD(VLABEL(RESULTS0&i.),"Result","");
put res;
CALL SYMPUT('macr_res',res);
%PUT ¯_res.;
LABEL RESULTS0&i. = ¯_res.;
%END;
RUN;
%MEND;
However, using this code it does not recognize the macr_res anymore. Also, it puts just ¯_res., what's wrong?
RE: Renaming/Delete part of a label of a SAS variable in the dataset
You need to use PROC DATASETS in this case.
The syntax would be like;
PROC DATASETS lib = whaterver_lib;
modify whatever_dataset;
/* this part can be automated by creating macro variable */
label RESULTS00 = blabla;
.
.
.
label RESULTS36 = XXXXX;
/* ************************************ */
quit;
As commented above the label statements part can be automated by creating some macro variable; you can refer to code snippent below
data _null_;
set input_dataset_name;
lengh str $ 256;
%do i = 1 %to 36;
str = trim(left(str)) || "label "|| "result0&i = "||TRANWRD(VLABEL(RESULTS0&i.),"Result","") || ";";
%end;
call symput ('lbl_str', str);
run;
Now you can use this customized label statement in PROC DATASETS code as below;
PROC DATASETS lib = whaterver_lib;
modify whatever_dataset;
&lbl_str;
quit;
I hope this will work.
sasbuddy
http://sites.google.com/site/sasbuddy/
RE: Renaming/Delete part of a label of a SAS variable in the dataset
I analyzed your query more closely;
I found out that the function VLABEL() cant be used inside another fucntion. So operating it separately and then using that result in TRANWRD() function would most probably work.
You can refer the following code snippet.
********************************************************************
data temp;
input RESULTS00-RESULTS36;
cards;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
;
run;
%global lbl_str;
options symbolgen;
%macro assign_label;
data _null_;
length str $ 1500;
%do i=0 %to 36;
%if &i lt 10 %then %do;
str = trim(left(str)) || "LABEL RESULTS0&i = RESULTS 0&i;";
%end;
%else %do;
str = trim(left(str)) || "LABEL RESULTS&i = RESULTS &i;";
%end;
%end;
call symput('lbl_str', str);
run;
%mend assign_label;
%assign_label;
proc datasets lib=work;
modify temp;
&lbl_str;
quit;
proc contents data = work.temp;
run;
%macro modify_label;
data _null_;
set temp;
length str $ 1500;
%do i=0 %to 36;
%if &i lt 10 %then %do;
vlbl = trim(left(VLABEL(RESULTS0&i)));
str = trim(left(str)) || "LABEL RESULTS0&i =" || trim(left(TRANWRD(vlbl,"RESULTS", ""))) || ";" ;
%end;
%else %do;
vlbl = trim(left(VLABEL(RESULTS&i)));
str = trim(left(str)) || "LABEL RESULTS&i =" || trim(left(TRANWRD(vlbl,"RESULTS", "")))|| ";" ;
%end;
%end;
call symput('lbl_str', str);
run;
%mend modify_label;
%modify_label;
proc datasets lib=work;
modify temp;
&lbl_str;
quit;
proc contents data = work.temp;
run;
sasbuddy
http://sites.google.com/site/sasbuddy/