BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Sean_OConnor
Obsidian | Level 7

Morning Folks,

 

I'm just wondering is this is possible. I have multiple historic datasets which have naming conventions such as follows 'Underlying Code 'Age Group'. 

 

Is it possible to write some code to pull all the variable names out of my dataset and any which have a space in them to replace them with an underscore. So 'Underlying Code' would be renamed to 'Underlying_Code' etc.

 

Any help would be welcome.

 

All the best,

 

Sean 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
options validvarname=any;
data fake;
    'Underlying Code'n=12;
    'Age Group'n=4;
    noblank=0;
run;

proc sql noprint;
    select cats('"',name,'"n=',translate(trim(name),'_',' ')) into :renames separated by ' ' 
    from dictionary.columns where libname='WORK' and memname='FAKE' and find(trim(name),' ')>0;
quit;
%put &=renames;

proc datasets library=work nolist;
modify fake;
rename &renames;
run; quit;
options validvarname=v7;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
options validvarname=any;
data fake;
    'Underlying Code'n=12;
    'Age Group'n=4;
    noblank=0;
run;

proc sql noprint;
    select cats('"',name,'"n=',translate(trim(name),'_',' ')) into :renames separated by ' ' 
    from dictionary.columns where libname='WORK' and memname='FAKE' and find(trim(name),' ')>0;
quit;
%put &=renames;

proc datasets library=work nolist;
modify fake;
rename &renames;
run; quit;
options validvarname=v7;
--
Paige Miller
Patrick
Opal | Level 21

Here a variant for the same. Main difference besides the macro is the use of function nvalid() to test for any non-compliant character and prxchange() to then translate any non-compliant character to an underscore

%macro renameVars(ds);
  %local lib tbl rename_list;

  %let lib=%upcase(%scan(work.&ds,-2,.));
  %let tbl=%upcase(%scan(work.&ds,-1,.));

  %let rename_list=;
  proc sql noprint;
    select cats("'",name,"'n =",prxchange('s/[^\w]/_/oi',-1,strip(name)))
      into :rename_list separated by ' '
  /*  select **/
    from dictionary.columns
    where 
      libname="&lib" 
      and memname="&tbl"
      and memtype='DATA' 
      and nvalid(name,'v7')=0
    ;
  quit;

  %if %nrbquote(&rename_list) ne %nrbquote() %then
    %do;
      proc datasets lib=&lib nolist nowarn;
        modify &tbl;
        rename &rename_list;
        run;
      quit;
    %end;
%mend;

data work.have;
  set sashelp.class;
  'var 2 x'n    =1;
  'var_other$5'n=20;
run;
%renameVars(have);

proc contents data=work.have;
run;quit;

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1458 views
  • 0 likes
  • 3 in conversation