DATA Step, Macro, Functions and more

Change case of variable names for all data sets at a time with call execute

Reply
Occasional Contributor
Posts: 17

Change case of variable names for all data sets at a time with call execute

 
Super User
Super User
Posts: 7,407

Re: Change case of variable names for all data sets at a time with call execute

Some tips on asking a question.  Whilst providing a descriptive title is great, providing some text in the post should also be done.   Provide example test data, in the form of a dataset, what you have tried, and what the output should look like.  At a guess:

data a;
  a=1;
  b=2;
  label a="abcd";
  label b="ADer";
run;

data _null_;
  set sashelp.vcolumn (where=(libname="WORK" and memname="A")) end=last;
  if _n_=1 then call execute('proc datasets library=work noprint; modify a;');
  call execute (cat(' attrib ',strip(name),' label="',strip(upcase(label)),'";'));
  if last then call execute('run; quit;');
run;

Occasional Contributor
Posts: 17

Re: Change case of variable names for all data sets at a time with call execute

I wanted to change only variable names that to for multiple data sets.

I have changed it for my convenience, but its displaying warning messages
on log. below is my code.


data _null_;
set sashelp.vcolumn (where=(libname="WORK" and memname in ("ADOMSG"
"AIR"))) end=last;
if _n_=1 then call execute(catx(" ",'proc datasets library=work nolist;
modify',memname,';'));
call execute (cat(' attrib ','name="',strip(lowcase(name)),'"
label="',strip(upcase(label)),'";'));;
if last then call execute('run; quit;');
run;

[image: Inline image 1]

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 6,946

Re: Change case of variable names for all data sets at a time with call execute

It would be very helpful if you just copy/pasted the message(s) and the preceding code from the log into a {i} window.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,407

Re: Change case of variable names for all data sets at a time with call execute

Then you need to break up the code into by groups, this is why posting test data/required output is helpful, we don't have to guess:

data a;
  a=1;
  b=2;
  label a="abcd";
  label b="ADer";
run;
data b;
  a=1;
  b=2;
  label a="abcd";
  label b="ADer";
run;

data _null_;
  set sashelp.vcolumn (where=(libname="WORK" and memname in ("A","B")));
  by memname;
  if first.memname then call execute(cat('proc datasets library=work noprint; modify ',strip(memname),';'));
  call execute (cat(' attrib ',strip(name),' label="',strip(upcase(label)),'";'));
  if last.memname then call execute('run; quit;');
run;

Super User
Posts: 17,865

Re: Change case of variable names for all data sets at a time with call execute


SrikanthY wrote:
I wanted to change only variable names that to for multiple data sets.

I have changed it for my convenience, but its displaying warning messages
on log. below is my code.


data _null_;
set sashelp.vcolumn (where=(libname="WORK" and memname in ("ADOMSG"
"AIR"))) end=last;
if _n_=1 then call execute(catx(" ",'proc datasets library=work nolist;
modify',memname,';'));
call execute (cat(' attrib ','name="',strip(lowcase(name)),'"
label="',strip(upcase(label)),'";'));;
if last then call execute('run; quit;');
run;

[image: Inline image 1]

##- Please type your reply above this line. Simple formatting, no
attachments. -##

That first line should have been in your first post. 

Images aren't attached when responding via email as noted - simple formatting, no attachments. You need to specifically upload your images to the forum. 

Super User
Posts: 17,865

Re: Change case of variable names for all data sets at a time with call execute

Is this just for presentation purposes? SAS isn't case sensitive when referring to variable names. 

Super User
Posts: 5,257

Re: Change case of variable names for all data sets at a time with call execute

I think the issue somehow is valid.

You could have decided for a specific notation, that includes casing - usually for better "readability".

I don't have SAS at my fingertips right now, but I recall that I've tried to change the case of a variable/data set name, but if all the letter where the same, it didn't take effect. Or...?

Data never sleeps
Respected Advisor
Posts: 3,777

Re: Change case of variable names for all data sets at a time with call execute


LinusH wrote:

I think the issue somehow is valid.

You could have decided for a specific notation, that includes casing - usually for better "readability".

I don't have SAS at my fingertips right now, but I recall that I've tried to change the case of a variable/data set name, but if all the letter where the same, it didn't take effect. Or...?


It works with PROC DATASETS or the RENAME data set option but not the RENAME statement.

 

data class;
   set sashelp.class;*(rename=(Name=NaMe));
   rename name=NaMe;
   run;
proc print;
   run;
proc datasets lib=work;
   modify class;
   rename Name=name;
   run;
proc print;
   run;
Super User
Posts: 9,682

Re: Change case of variable names for all data sets at a time with call execute


data a;
  a=1;
  b=2;
  label a="abcd";
  label b="ADer";
run;
data b;
  a=1;
  b=2;
  label a="abcd";
  label b="ADer";
run;

data _null_;
  set sashelp.vcolumn (where=(lowcase(name)='a')) end=last;
  call execute(cat('proc datasets library=',libname,' nolist nodetails;'));
  call execute(cat('modify ',memname,'; rename ',name,'=new_',name,';quit;'));
run;


Occasional Contributor
Posts: 17

Re: Change case of variable names for all data sets at a time with call execute

Variable name is not changed low-case to up-case

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Super User
Posts: 9,682

Re: Change case of variable names for all data sets at a time with call execute

It depends on your OS, maybe you can't do it.

 

options validvarname=upcase;
data class;
 set sashelp.class;
run;
Super User
Super User
Posts: 7,407

Re: Change case of variable names for all data sets at a time with call execute

Oh, not label, variable name.  In which case why?  It matters not to programming if they are upper/lower/mixed case.  If you have an output spec to match to then I would suggest creating an emtpy table which matches your spec, then setting your data to that spec.  This has the benefits of having a fixed structure, and you can pull out any problems:

proc sql;
  create table MYOUT 
  ( 
    VAR1 char(200),
    VAR2 num
  );
quit;

data myout;
  set myout processed data;
run;
Ask a Question
Discussion stats
  • 12 replies
  • 636 views
  • 7 likes
  • 7 in conversation