Help using Base SAS procedures

VLABEL Question

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

VLABEL Question

I want to transfer the variables lables from the X's to the Y's. here is how i think i should be able to do it:

data have;

input x1 x2 x3 y1 y2 y3;

  label x1 = Var1

           x2 = Var2

           x3 = Var3;

cards;

1 2 3 4 5 6

;

run;

data want;

     array x {*} x1-x3;

    array y {*} y1-y3;

    do i = 1 to dim(x);

        y{i} = x(vlabel(i));

    end;

run;


Accepted Solutions
Solution
‎04-11-2012 03:35 PM
Super User
Super User
Posts: 7,074

Re: VLABEL Question

Let SAS do it for you.

data want ;

  set

    have(obs=0 rename=(x1-x2=y1-y2) drop=y1-y2)

    have

  ;

run;

View solution in original post


All Replies
Super User
Posts: 5,516

Re: VLABEL Question

Shouldn't the final assignment be rearranged?

y{i} = vlabel(x{i});


Contributor
Posts: 62

Re: VLABEL Question

Posted in reply to Astounding

Thanks astounding.

this also made me realize that what i really want is to assign the x array labels to the y array.

such that:

label y{i} = vlabel(x{i})

but i dont get the desired result.

any suggestions?

Super User
Posts: 5,516

Re: VLABEL Question

Nothing simple, and nothing pretty.

I guess I would use CALL EXECUTE in that DATA step to generate the proper PROC DATASETS statements to assign labels.

In real life, does the data set already exist in SAS form (and you need to change the labels)?  Or are you really inputting from raw data?  I guess my approach could work either way, but it does get messy.

Contributor
Posts: 62

Re: VLABEL Question

Posted in reply to Astounding

Yea i have a SAS dataset with about 100 variables, each with a label.

the variables are named in this fashion, BP1-BP100.

Solution
‎04-11-2012 03:35 PM
Super User
Super User
Posts: 7,074

Re: VLABEL Question

Let SAS do it for you.

data want ;

  set

    have(obs=0 rename=(x1-x2=y1-y2) drop=y1-y2)

    have

  ;

run;

Super User
Posts: 5,516

Re: VLABEL Question

Nice, simple, and practical.

Complications to watch for.  When there are conflicting variable attributes assigned, the FORMAT attribute is taken from the first data set, but the LABEL and INFORMAT attributes are taken from the second data set.  So if the Y variables already have labels that you need to change, you'll have to switch the order of the "have" references.  Of course, that wasn't part of the original problem, and this is a great solution.

Respected Advisor
Posts: 3,799

Re: VLABEL Question

Posted in reply to Astounding

Complications to watch for.  When there are conflicting variable attributes assigned, the FORMAT attribute is taken from the first data set, but the LABEL and INFORMAT attributes are taken from the second data set.  So if the Y variables already have labels that you need to change, you'll have to switch the order of the "have" references.  Of course, that wasn't part of the original problem, and this is a great solution.

If you want to keep the FORMATS and/or INFORMATS that is doable with to a little more code.

data have;

   input x1 x2 x3 y1 y2 y3;

   label x1 = Var1

         x2 = Var2

         x3 = Var3;

   format x1-x3 dollar12.2;

   format y1-y3 comma12.3;

   informat x1-x3 F12.2;

   informat y1-y3 D12.3;

   cards;

1 2 3 4 5 6

;

   run;

data want;

   if 0 then set have; *keep varnum order;

   if 0 then set have(keep=x: rename=(x1-x3=y1-y3)); *copy attributes;

   format _all_;

   informat _all_;

   set have;

   run;

proc contents varnum;

   run;

proc print label;

   run;

Contributor
Posts: 62

Re: VLABEL Question

Thank you all for your input.

Valued Guide
Posts: 2,177

Re: VLABEL Question

I wanted to offer a simple approach:=

proc contents OUT= provides a dataset with the labels as data you can use simply to generate a LABEL statement.

%let data1 = data_with_labels;

%let data2 = data_without ;

%let lib= %scan( work.&data2, -2, . ) ;  * get libname for target or work ;

%let toMem = %scan( &data2, -1, . ) ;  * get memname of target ;

proc contents data= &data1 noprint out= conts1 ;

run ;

(and then I thought about the possibility that there might be more variables in the label source than the unlabelled target!

So an sql join came into it because the names might not have the same "caSe"

proc contents data= &data2 noprint out= conts2 ;

run ;

proc sql ;

create table new_labels4old  as

   select b.name, a.label

  from  conts1 as A , conts2 as B

where upcase(a.name) = upcase(b.name) and a.label ne ' '

  ;

quit ;

%let cont1 = &syslast ;

data _null_ ;

call execute( 'proc datasets library= "&lib" nolist mt= data; ') ;

call execute( ' modify &toMem; ' ) ;

do while( not end_of_data ) ;

set new_labels4old end= end_of_data  ;

call execute( name !! '=' !! quote(trim( label))  ) ;

end ;

call execute( '; run; quit; ' );

stop;

run ;

(so it stopped looking so "simple") :-)

Super User
Posts: 10,041

Re: VLABEL Question

data have;
input x1 x2 x3 y1 y2 y3;
  label x1 = Var1
           x2 = Var2
           x3 = Var3;
cards;
1 2 3 4 5 6
;
run;
data _null_;
 set sashelp.vcolumn(keep=libname memname name label where=(libname='WORK' and memname='HAVE' and name like 'x%')) end=last;
 if _n_ eq 1 then call execute('proc datasets library=work nolist;modify have; label ');
 call execute(catx(' ',translate(name,'y','x'),'="',label,'"',name,'=" "'));
 if last then call execute(';quit;');
run;

Ksharp

🔒 This topic is solved and locked.

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

Discussion stats
  • 10 replies
  • 472 views
  • 1 like
  • 6 in conversation