DATA Step, Macro, Functions and more

Question about assigning labels

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

Question about assigning labels

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;


Accepted Solutions
Solution
‎01-13-2014 01:40 PM
Super Contributor
Posts: 307

Re: Question about assigning labels


All Replies
PROC Star
Posts: 7,363

Re: Question about assigning labels

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



Respected Advisor
Posts: 3,777

Re: Question about assigning labels

I don't think that will do what wants.

Contributor
Posts: 38

Re: Question about assigning labels

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?

Super User
Posts: 17,868

Re: Question about assigning labels

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;

Respected Advisor
Posts: 3,777

Re: Question about assigning labels

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?

Contributor
Posts: 38

Re: Question about assigning labels

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

Solution
‎01-13-2014 01:40 PM
Super Contributor
Posts: 307

Re: Question about assigning labels

Respected Advisor
Posts: 3,777

Re: Question about assigning labels

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

PROC Star
Posts: 7,363

Re: Question about assigning labels

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;

Contributor
Posts: 38

Re: Question about assigning labels

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

Respected Advisor
Posts: 3,777

Re: Question about assigning labels

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;
Super User
Super User
Posts: 6,502

Re: Question about assigning labels

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;

Contributor
Posts: 38

Re: Question about assigning labels

Tom, is your macro publicly available?

Super User
Super User
Posts: 6,502

Re: Question about assigning labels

%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;

☑ This topic is SOLVED.

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

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