I have a dataset where the row that I need as a header is present as a datarow. I want to convert that first row into header .
Data that I have
A B C
1 Name Address Age
2 John Carry 66
3 Andy NYC 32
Data that I want.
Name Address Age
1 John Carry 66
2 Andy NYC 32
2
Pull out the names from the first observation. A simple way is to use OBS=1 dataset option and PROC TRANSPOSE.
proc transpose data=have(obs=1) out=names;
var _all_;
run;
Now use them to create OLD=NEW pairs.
proc sql noprint;
select catx('=',nliteral(_name_),nliteral(col1))
into :renames separated by ' '
from names
where upcase(_name_) ne upcase(col1)
;
quit;
Then use those pairs to generate a RENAME statement (or RENAME= dataset option). use FIRSTOBS=2 to skip over the observation with the names.
data want;
set have(firstobs=2);
rename &renames;
run;
try the following:
DATA A0 a1 a2;
input A $4. B $8. C $4.;
if _n_=1 then output a2; else output a1;
cards;
Name Address Age
John Carry 66
Andy NYC 32
;
run;
%macro nms;
proc sql;
select count(name) into :n from sashelp.vcolumn where libname='WORK' and memname='A2';
%let n=&n.;
select name into :nm1 - :nm&n. from sashelp.vcolumn where libname='WORK' and memname='A2';
select &nm1. %do i=1 %to &n.;,&&nm&i.. %end; into :h1 %do j=1 %to &n.; ,:h&j. %end; from A2;
quit;
data new; set a1; rename %do t=1 %to &n.; &&nm&t.=&&h&t. %end;; run;
%mend;
%nms