Help using Base SAS procedures

Removing permanently spaces in the variables

Reply
Regular Contributor
Posts: 229

Removing permanently spaces in the variables

Hi i am having sas dataset in that i am having 200-300 variables having spaces
how can i remove all the spaces in the variables in that dataset permanently;

data a;
input 'id 1'n 'id 2' n-------------------------- 'id 200'n;
run;
Super Contributor
Posts: 345

Re: Removing permanently spaces in the variables

Hi R_Win,

the following code queries sashelp.vcolumn to get the variable names and call execute to create a proc datasets with the rename statment for all variables.

[pre]
data _null_;
set sashelp.vcolumn(
keep= Name LibName MemName
rename= (Name = OldName)
where= (Libname='WORK' and MemName = 'A')
) end= last;
length NewName $ 32;

if _n_ = 1 then do;
call execute('proc datasets library=work nolist;');
call execute('modify a;');
call execute('rename ');
end;

NewName = compress(OldName, ' ');
OldName = cats("'", OldName, "'n");

call execute(trim(OldName) !! ' = ' !! trim(NewName));

if last then do;
call execute(';');
call execute('run;');
end;
run;[/pre]
Super Contributor
Super Contributor
Posts: 3,174

Re: Removing permanently spaces in the variables

Posted in reply to andreas_lds
Also, consider using an ARRAY approach as shown below:

DATA A;
SET A;
ARRAY ALLCHARVARS (*) $ _CHARACTER_;
DO I=1 TO DIM(ALLCHARVARS);
ALLCHARVARS(I) = COMPRESS( ALLCHARVARS(I) );
END;
RUN;

Scott Barry
SBBWorks, Inc.
Respected Advisor
Posts: 4,173

Re: Removing permanently spaces in the variables

Hi

Try:

array charvars (*) _character_;
do i=1 to dim(charvars);
charvars(i)=compress(charvars(i));
end;
drop i;

HTH
Patrick
Valued Guide
Posts: 2,177

Re: Removing permanently spaces in the variables

R_Win

unless you define shorter lengths to reduce the size of variables, the data set will be the same size even if you compress to reduce multiple blanks to one. The compress of a variable just moves blanks from the middle to the end of the variable.
Except, using an option I didn't see recommended by SBB or Patrick, you can get sas to write the rows with compression, like
data reduced(compress=yes) ;
set original ;
array chrs _character_ ;
do over chrs ;
chrs= compbl( chrs ) ;
end ;
run;

Of course, that interprets the objective as referring to content of variables.

If you refer to the blanks in variable names (not values) adopt the solution from andreas_lds

peterc
Ask a Question
Discussion stats
  • 4 replies
  • 104 views
  • 0 likes
  • 5 in conversation