BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

Hi All,

  I have a dataset with same variables starting with underscore and without underscore. I would want to assign (in a loop)all underscore varaibles to those without undsrscore. can you help me on this?

For eg.

_id     id      _name   name

 

hardcoding I can do this way:

_id=id;

_name=name;

 

 

3 REPLIES 3
r_behata
Barite | Level 11
data have;
	_id='   ';
	id='abc';
	_name='   ';
	name='xyz';
	output;
run;

data want;
	set have;
	array ch[*] _character_;

	do i=1 to dim(ch);
		if   find( vname(ch[i]), '_') then
			ch[i]=vvaluex(compress(vname(ch[i]),'_'));
	end;

	drop i;
run;
FreelanceReinh
Jade | Level 19

Hi @SASPhile,

 

You could create the repetitive code, e.g., by PROC SQL:

proc sql noprint;
select catx('=',name,substr(name,2)) into :assign separated by '; ' 
from dictionary.columns
where libname='WORK' & memname='HAVE' & name eqt '_';
quit;

data want;
set have;
&assign;
run;
ballardw
Super User

Do your really want to create another set of variables with the same values?

Or do you really need to just Rename the ones that you have, such as for combining with another data set?

 

If you use the Search bar at the top of your message for something like "Rename all Variables" you find a number of variations on that:

Such as https://communities.sas.com/t5/New-SAS-User/rename-a-whole-set-of-variable-adding-the-same-suffix-to...

 

Your code example assigns the non_underscored variable value to an underscored name. That doesn't quite match your verbiage.

 

Something like this may work:

%macro dummy;
Proc sql;
   select name into : varlist separated by " "
   from dictionary.columns
   where libname='WORK' and memname='HAVE'
      and substr(name,1,1)='_'
   ;
quit;

data work.want;
   set work.have;
   %do i= 1 %to %sysfunc(countw(&varlist.));
      %let var = %substr(%scan(&varlist., &i),2);
      _&var.= &var.;
     /* or &var. = _&var.; depending on actual direction*/
   %end;
run;
 
%mend;

Which does assume that you have both Name and _Name for all the variables.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 380 views
  • 0 likes
  • 4 in conversation