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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

10 REPLIES 10
Astounding
PROC Star

Shouldn't the final assignment be rearranged?

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


SAShole
Pyrite | Level 9

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?

Astounding
PROC Star

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.

SAShole
Pyrite | Level 9

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

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

Tom
Super User Tom
Super User

Let SAS do it for you.

data want ;

  set

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

    have

  ;

run;

Astounding
PROC Star

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.

data_null__
Jade | Level 19

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;

SAShole
Pyrite | Level 9

Thank you all for your input.

Peter_C
Rhodochrosite | Level 12

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") 🙂

Ksharp
Super User
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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