BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
IOT
Calcite | Level 5 IOT
Calcite | Level 5
Hello,

I need to change a large number of variable names of a dataset. The targeted names are included in another dataset as data. For example, there are two datasets, A and B. The variable names of A are a1 a2 a3. And B has two variables, of which names are b1 and b2. b2 has data: c1, c2, and c3. Then I want to replace a1-3 by c1-3. Could you please help? Thanks in advance.








1 ACCEPTED SOLUTION

Accepted Solutions
CurtisMackWSIPP
Lapis Lazuli | Level 10

It can be done in a few different ways.  I think this one would be the easiest to understand. It uses CALL Execute to write code the uses the PROC DATASETS procedure to do the rename. 

 

data work.have;
  x = 1; y = 'Y'; output;
run;

data renames;
  oldname = "X"; NewName = "X_NEW"; OUTPUT;
  oldname = "Y"; NewName = "Y_NEW"; OUTPUT;
run;

data _null_;
  set renames end= finished;
  if _n_ = 1 then do;
    call execute("proc datasets library=work nolist;");
    call execute(" modify have;");
  end;
  call execute(" rename " || oldname || " = " || NewName || ";");
  if finished then do;
    call execute("quit;");
  end;
run;

View solution in original post

5 REPLIES 5
ChrisNZ
Tourmaline | Level 20

Like this?

data NAMES;
 input LIBNAME $ MEMNAME $ OLDNAME $ NEWNAME $;
cards;
WORK A a1 c1
WORK A a2 c2
WORK A a3 c3
run;

data _null_;
  set NAMES;
  call execute('proc datasets lib='||LIBNAME||' noprint;');
  call execute('  modify '||MEMNAME||';');
  call execute('  rename '||OLDNAME||'='||NEWNAME||';');
  call execute('run;');
run;

 

vellad
Obsidian | Level 7

@ChrisNZ's code works. And to get the dataset NAMES used in his code, you can do this:

proc transpose data=A (obs=0) out=A_t (keep=_NAME_ rename=(_NAME_=oldname));
    var :;
run;

data NAMES;
    set A_t;
    set B (keep=b2 rename=(b2=newname));
    libname='WORK';
    memname='A';
run;
CurtisMackWSIPP
Lapis Lazuli | Level 10

It can be done in a few different ways.  I think this one would be the easiest to understand. It uses CALL Execute to write code the uses the PROC DATASETS procedure to do the rename. 

 

data work.have;
  x = 1; y = 'Y'; output;
run;

data renames;
  oldname = "X"; NewName = "X_NEW"; OUTPUT;
  oldname = "Y"; NewName = "Y_NEW"; OUTPUT;
run;

data _null_;
  set renames end= finished;
  if _n_ = 1 then do;
    call execute("proc datasets library=work nolist;");
    call execute(" modify have;");
  end;
  call execute(" rename " || oldname || " = " || NewName || ";");
  if finished then do;
    call execute("quit;");
  end;
run;
IOT
Calcite | Level 5 IOT
Calcite | Level 5
Thanks for the simple solution. It’s quite helpful. I appreciate the other answers as well.
Venky3110
Fluorite | Level 6
Try using this code


data test;
input a1 a2 a3;
datalines;
1 2 3
4 5 6
7 8 9
;
run;


data var_names;
input var1 $2. var2 $3.;
datalines;
b1 c1
b2 c2
b3 c3
;
run;

data contents;
ds=open("test",'i');
num=attrn(ds,'nvars');
do v=1 to num;
vn=varname(ds,v);
output;
end;
run;

proc sql;
select  count(distinct var1)  into : count from var_names;
%let count=&count;
select distinct var2 into : newvar_1-: newvar_&count from var_names;
quit;

proc sql;
select count(distinct v) into : count1 from contents;
%let count1=&count1;
select distinct vn into : oldvar_1-:oldvar_&count1 from contents;
quit;

%put &oldvar_1 &oldvar_2 &oldvar_3;
%put &newvar_1 &newvar_2 &newvar_3;



data want1;
set test;
run;
%macro rename;

%do i=1 %to &count1.;


data want1;
set want1;
rename &&oldvar_&i..=&&newvar_&i..;
run;

%end;

%mend;
%rename; 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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