Good question from Kannan. I was also wondering. Perhaps you mean variable label, i.e., a short description of a variable's content (e.g. "Patient number" for a variable named PATNO)?
After all, even if the "headers" found in the raw data are some kind of "names", would we be sure that these names happen to be valid SAS names? So, it would be safer to regard them just as "labels".
In this case, there were several answers posted to a similar question in the SAS-L mailing list, 15 years ago:
https://groups.google.com/forum/#!topic/comp.soft-sys.sas/awuwQAwP1AA
However, my impression was that your situation is slightly different.
There is also a 2008 paper "Dynamically Create Variable Labels from Data Set Values" which may be applicable, but I haven't checked that in detail either.
Instead, I've quickly written two macros, which seem to work at least for the test data I've created (26 columns, 13 rows). The first changes the labels of the variables in dataset WHY to the "headers" found in the 1-observation dataset USEFUL, the second changes the variable names correspondingly (assuming that the headers are in fact valid SAS variable names).
/* Macro to label variables in &DATA with labels found in &LABELS */
%macro lblvars(data=, labels=);
/* Retrieve variable names */
proc sql noprint;
select name into :vn1 - :vn9999
from dictionary.columns
where libname='WORK' & memname=upcase("&data")
order by varnum;
quit;
/* Retrieve variable labels */
data _null_;
set &labels;
array lv[*] _character_;
do i=1 to &sqlobs;
call symput(cats('lbl',i), lv[i]);
end;
run;
%put Now labeling &sqlobs variables ...;
proc datasets nolist;
modify &data;
label
%do i=1 %to &sqlobs;
&&vn&i="&&lbl&i"
%end;;
quit;
%put ... Done.;
%mend lblvars;
/* Macro to rename variables in &DATA with new names found in &NAMES */
%macro renvars(data=, names=);
/* Retrieve old variable names */
proc sql noprint;
select name into :vn1 - :vn9999
from dictionary.columns
where libname='WORK' & memname=upcase("&data")
order by varnum;
quit;
/* Retrieve new variable names */
data _null_;
set &names;
array nv[*] _character_;
do i=1 to &sqlobs;
call symput(cats('name',i), nv[i]);
end;
run;
%put Now renaming &sqlobs variables ...;
proc datasets nolist;
modify &data;
rename
%do i=1 %to &sqlobs;
&&vn&i=&&name&i
%end;;
quit;
%put ... Done.;
%mend renvars;
/* Create test data */
%let nvar=26;
data why;
array v[&nvar];
do i=1 to 13;
do _n_=1 to &nvar;
v[_n_]=ranuni(31416);
end;
output;
end;
drop i;
run;
data useful;
array hdr[&nvar] $40;
do _n_=1 to &nvar;
hdr[_n_]=byte(64+_n_);
end;
run;
proc contents data=why;
run;
/* Apply the macros and see how meta data change */
%lblvars(data=why, labels=useful)
proc contents data=why;
run;
%renvars(data=why, names=useful)
proc contents data=why;
run;
Of course, this is just unvalidated draft code and it comes with no warranties whatsoever. In particular, the macros might fail if the headers are invalid labels or names, respectively.
Obviously, it is crucial that the n-th column of USEFUL contains the header for the n-th column of WHY for all n=1, ..., 900+.
As to your second question, I agree that it should be easier to retrieve variable names and labels from raw data.
Edit: For delimited raw data files such as CSV files there is in fact a way to retrieve variable names from the first record of the file: see PROC IMPORT, GETNAMES statement.
... View more