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

Hi,

what is the best way to rename my variables?

data class;

  set sashelp.class;

newage1=age;

newweight1=weight;

newheight1=heigt;

run;

I have a lot of variables end with '1'. I want to take out '1'.

in my example, I need to rename newage1=newage and all other variables.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

how about:

data class;
  set sashelp.class (obs=1);
  newage1=age;
  neww1=weight;
  newh1=height;
run;

proc sql;
  select catx('=',name,substr(name,1,length(name)-1)) into : list separated by ' '
   from dictionary.columns
     where libname='WORK' and memname='CLASS' and substr(name,length(name))='1';
quit;

data newclass;
   set class;
   rename &list;
run;
proc print;run;

View solution in original post

2 REPLIES 2
Linlin
Lapis Lazuli | Level 10

how about:

data class;
  set sashelp.class (obs=1);
  newage1=age;
  neww1=weight;
  newh1=height;
run;

proc sql;
  select catx('=',name,substr(name,1,length(name)-1)) into : list separated by ' '
   from dictionary.columns
     where libname='WORK' and memname='CLASS' and substr(name,length(name))='1';
quit;

data newclass;
   set class;
   rename &list;
run;
proc print;run;

Patrick
Opal | Level 21

Pretty close to what Linlin suggest but using a RegEx to match more precisely the variables you want renamed - and using Proc Datasets instead of a Data Step to do the renaming (and so avoiding a full pass through the data).

data class;
  set sashelp.class;
  newage1=age;
  newweight1=weight;
  newheight1=height;
run;


proc sql noprint;
  select cats(name,'=',prxchange('s/(.+)(1\b)/$1/o',1,name)) into : list separated by ' '
   from dictionary.columns
     where libname='WORK' and memname='CLASS' and prxmatch('/.+1\b/',name)
  ;
quit;

proc datasets lib=work nolist;
  modify class;
    rename &list;
  ;
quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 979 views
  • 4 likes
  • 3 in conversation