Hi all,
I am converting a character variable to numeric and want the numeric variable to have the same label as the character variable but am not sure how to do this. See example code below:
data blah(drop=var1_c); set blahblah(rename=(var1=var1_c));
var1=input(var1_c,1.);
label var1=*same label as var1_c*;
run;
Would something like this work for you?
http://support.sas.com/resources/papers/proceedings10/047-2010.pdf
Use the call label routine. It is documented at: SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition
The syntax is simply:
CALL LABEL(variable1,variable2);
which assigns the label from variable1 to the character variable variable2
Correct. I tried Art's suggestion and it did not work. @data_null_: im assuming that this conversion is something that everyone wants but there is no easy solution?
You can use vlabel to get the label in one step and then a proc datasets to assign the label but still not really nice in many ways.
data blah(drop=var1_c); set blahblah(rename=(var1=var1_c));
var1=input(var1_c,1.);
call symput (var1_label, vlabel(var1_c));
run;
proc datasets lib =work nolist nodetails;
modify blah;
label var1="&var1_label";
run; quit;
I'ts not difficult but there is no function to assign a label in a data step. How many variables are you converting? How many different informats?
Usually converting 5-6 variables per dataset. Not using any informats since the variables are already coming from a SAS dataset. So really I am not converting too many variables but its five too many to label manually :smileysilly:.
Would something like this work for you?
http://support.sas.com/resources/papers/proceedings10/047-2010.pdf
You seek the "holly grail" of type conversion. :smileymischief:
Sorry for not having first tested my suggestion. You and DN are definitely correct, it won't work as I suggested.
However John's paper, as referenced by , should work.
If you only have one variable that you want to reassign a label to, the following will also work (yes, I did test it this time):
data blahblah;
label var1='first variable';
input var1 $;
cards;
1
2
3
;
filename relabel temp;
data _null_;
set blahblah;
file relabel;
length label $32;
call label(var1,label);
put "data blah (drop=var1_c);";
put "set blahblah (rename=var1=var1_c);";
put "label var1=:"@;
put label @;
put ";";
put "var1=input(var1_c,1.);";
put "run;";
run;
%include relabel;
Thank you all for your suggestions. I was hoping there was a function to the likes of label var1=getlabel(var1_c); but wouldn't we all
I thing this technique is interesting and demonstrates interesting features, and default behavior, of PROC TRANSPOSE.
Personally I have a utility macro function I have been using for 15 years that will return information about a particular variable from an existing dataset which makes this problem easy.
data blah(drop=var1_c);
set blahblah(rename=(var1=var1_c));
var1=input(var1_c,1.);
label var1="%varexist(blahblah,var1,label)";
run;
Tom, is your macro publicly available?
%macro varexist(ds,var,info);
%local dsid rc varnum;
%*----------------------------------------------------------------------
Use the SYSFUNC macro to execute the SCL OPEN, VARNUM,
other variable information and CLOSE functions.
-----------------------------------------------------------------------;
%let dsid = %sysfunc(open(&ds));
%if (&dsid) %then %do;
%let varnum = %sysfunc(varnum(&dsid,&var));
%if (&varnum) & %length(&info) %then
%sysfunc(var&info(&dsid,&varnum))
;
%else
&varnum
;
%let rc = %sysfunc(close(&dsid));
%end;
%else 0;
%mend varexist;
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 how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.