BookmarkSubscribeRSS Feed
R_Win
Calcite | Level 5
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;
4 REPLIES 4
andreas_lds
Jade | Level 19
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]
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
Patrick
Opal | Level 21
Hi

Try:

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

HTH
Patrick
Peter_C
Rhodochrosite | Level 12
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 713 views
  • 0 likes
  • 5 in conversation