Help using Base SAS procedures

need to replace the varible names with the values of the first observation.

Reply
Contributor
Posts: 44

need to replace the varible names with the values of the first observation.

I have the following Datatset with all characrter variables (SS,DD,YY,PP,NN):


SS     DD    YY      PP       NN
--    ---    --     ---       --
hy     tr    uy      io       oi
fr      1     3       5        6
er      4     5       7       10

I need to replace the varible names with the values of the first observation.
So the new variable names will be (hy, tr, uy, io, oi) can't do any hard coding.
the result dataset should be as below:

hy     tr    uy      io       oi
--    ---    --     ---       --
fr      1     3       5        6
er      4     5       7       10

Thank you so much from the bottom of my heart for helping me.

Super User
Posts: 17,832

Re: need to replace the varible names with the values of the first observation.

How did you get this data, the best place would be to change how you imported it in. 

That being said, is this one time, with only 5 columns or it could be more?

Super User
Posts: 17,832

Re: need to replace the varible names with the values of the first observation.

This answers your question, but I still think you should fix your import, especially since all your variables are character when you probably want them to be numeric:

data have;

input SS $ DD $ YY $ PP  $ NN $;

cards;

hy     tr    uy      io       oi

fr      1     3       5        6

er      4     5       7       10

;

run;

proc transpose data=have out=names;

  var _char_;

run;

proc sql;

  select catx(" = ", _name_ , col1)

  into :rename_list separated by " "

  from names;

quit;

%put &rename_list;

data want;

  set have (firstobs=2);

  rename &rename_list;

run;

Super User
Super User
Posts: 6,500

Re: need to replace the varible names with the values of the first observation.

Add OBS=1 dataset option to the input to the PROC TRANSPOSE to prevent it from transposing the whole dataset.

Super User
Posts: 9,681

Re: need to replace the varible names with the values of the first observation.

As Reeza pointed out .You should fix the importing way . Not consider change its name.

data have;
input SS $ DD $ YY $ PP  $ NN $;
cards;
hy     tr    uy      io       oi
fr      1     3       5        6
er      4     5       7       10
;
run;
proc sql noprint;
select name into : list separated by ' ' 
 from dictionary.columns
  where libname='WORK' and memname='HAVE' ;
quit;
data _null_;
 set have(obs=1);
 length x y $ 32767;
 x=catx(' ',of _all_);
 do i=1 to countw(x);
  y=catx(' ',y,catx('=',scan("&list",i),scan(x,i)));
 end;
 call symputx('rename',y);
run;
data want;
  set have (firstobs=2);
  rename &rename ;
run;

Xia Keshan

Super Contributor
Posts: 305

Re: need to replace the varible names with the values of the first observation.

Hello,

data have;

input  SS $     DD $    YY  $    PP $    NN $;

datalines;

hy     tr    uy      io       oi

fr      1     3       5        6

er      4     5       7       10

;

data _null_;

set have (obs=1);

array allvar{*}  ss--nn ;

length syntax $ 50;

do i=1 to dim(allvar);

syntax=strip(syntax) || ' ' || catt(vname(allvar{i}),'=',allvar{i});

put syntax=;

call symputx ('newvars', 'rename ' || strip (syntax)  );

end;

run;

data want;

set have (firstobs=2);

&newvars;

run;

Ask a Question
Discussion stats
  • 5 replies
  • 235 views
  • 0 likes
  • 5 in conversation