BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
spirto
Quartz | Level 8

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;

1 ACCEPTED SOLUTION

Accepted Solutions
18 REPLIES 18
art297
Opal | Level 21

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



data_null__
Jade | Level 19

I don't think that will do what wants.

spirto
Quartz | Level 8

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?

Reeza
Super User

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;

data_null__
Jade | Level 19

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?

spirto
Quartz | Level 8

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:.

data_null__
Jade | Level 19

You seek the "holly grail" of type conversion. :smileymischief:

art297
Opal | Level 21

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;

spirto
Quartz | Level 8

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 Smiley Wink

data_null__
Jade | Level 19

I thing this technique is interesting and demonstrates interesting features, and default behavior, of PROC TRANSPOSE.

proc datasets kill noprint nolist;
  
run;
  
quit;
data class;
   infile cards dsd;
  
input (Name Sex Age Height Weight) (:$8.);
   attrib age     label='Age (years)';
  
attrib height  label='Height (inches)';
  
attrib weight  label='Weight (lbs.)';
  
attrib sex     label='Gender';
  
cards;
"Alfred","M","14","69","112.5"
"Alice","F","13","56.5","84"
"Barbara","F","13","65.3","98"
"Carol","F","14","62.8","102.5"
"Henry","M","14","63.5","102.5"
"James","M","12","57.3","83"
"Jane","F","12","59.8","84.5"
"Janet","F","15","62.5","112.5"
"Jeffrey","M","13","62.5","84"
"John","M","12","59","99.5"
"Joyce","F","11","51.3","50.5"
"Judy","F","14","64.3","90"
"Louise","F","12","56.3","77"
"Mary","F","15","66.5","112"
"Philip","M","16","72","150"
"Robert","M","12","64.8","128"
"Ronald","M","15","67","133"
"Thomas","M","11","57.5","85"
"William","M","15","66.5","112"
;;;;
   run;
proc transpose data=class out=tall;
   by Name;
   copy Sex;
   var Age Height Weight;
   run;
proc print;
  
run;
data convert / view=convert;
   set tall;
   num1 = input(col1,comma16.);
   run;
proc transpose data=convert out=class2(drop=_name_);
   by Name;
   copy Sex;
   var num1;
   run;
proc print;
  
run;
Tom
Super User Tom
Super User

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;

spirto
Quartz | Level 8

Tom, is your macro publicly available?

Tom
Super User Tom
Super User

%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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 18 replies
  • 2576 views
  • 6 likes
  • 8 in conversation