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;
Let SAS do it for you.
data want ;
set
have(obs=0 rename=(x1-x2=y1-y2) drop=y1-y2)
have
;
run;
Shouldn't the final assignment be rearranged?
y{i} = vlabel(x{i});
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?
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.
Yea i have a SAS dataset with about 100 variables, each with a label.
the variables are named in this fashion, BP1-BP100.
Let SAS do it for you.
data want ;
set
have(obs=0 rename=(x1-x2=y1-y2) drop=y1-y2)
have
;
run;
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.
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;
Thank you all for your input.
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") 🙂
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.