BookmarkSubscribeRSS Feed
need_sas_help
Calcite | Level 5

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.

5 REPLIES 5
Reeza
Super User

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?

Reeza
Super User

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;

Tom
Super User Tom
Super User

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

Ksharp
Super User

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

Loko
Barite | Level 11

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;

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
  • 5 replies
  • 785 views
  • 0 likes
  • 5 in conversation