Help using Base SAS procedures

Removing unwanted characters (zzzzzz,zzzzzzzzz,zz)

Reply
Regular Contributor
Posts: 229

Removing unwanted characters (zzzzzz,zzzzzzzzz,zz)

Hi

I have some observations which are like this

data name;
input id$ name$ add$;
cards;
mailtohim nani mainroad
zzzzz zzz street
raju zzzz zzzzzzzzzzzzz
zzzzzzzz zz indi
run;

I have 200 variables in some variables the observations are filled with zzzz and i dont the number of zzzzzz present how can i eliminte and keep space at that place
Super Contributor
Super Contributor
Posts: 3,174

Re: Removing unwanted characters (zzzzzz,zzzzzzzzz,zz)

If I understand your challenge correctly, you can use the SUBSTR function on the left side of the SAS assignment statement in a DATA step. And if you are unsure at what position the string starts, you can use the INDEX function to find the starting offset before using the SUBSTR function to change the value from "zzzzzz" to " " (six blank characters). And, you can use an ARRAY statement to setup a DO loop through your list of 200 SAS variables, so that you do not need to repeat the same code for each variable.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: Removing unwanted characters (zzzzzz,zzzzzzzzz,zz)

Or you could use the translate function:

TRANSLATE(source,to-1,from-1<,...to-n,from-n>)

so ....

name=translate(name,' ','z');

I assume you might expect some valid Zs so you might want to code round the function so that it only takes place if there are not non-z characters on either side of the z or something.

Alos, note the confusing way the translate has the TO before the FROM, personally I would have made it the other way round but there you go.
Super Contributor
Super Contributor
Posts: 3,174

Re: Removing unwanted characters (zzzzzz,zzzzzzzzz,zz)

The OP stated clearly that the string to be manipulated represented a "word" -- so possibly TRANWRD may apply.

Scott Barry
SBBWorks, Inc.
Valued Guide
Posts: 2,175

Re: Removing unwanted characters (zzzzzz,zzzzzzzzz,zz)

sounds like an ideal case for regular expression handling.
Unfortunately, I can't claim the expertise needed to quickly reply with the code you want. So, Michel_SAS, I recommend you read up on prxchange(). See http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a002601591.htm#a003190494 for an example that might be close to what you seek.

PeterC
Respected Advisor
Posts: 3,900

Re: Removing unwanted characters (zzzzzz,zzzzzzzzz,zz)

I think the code below should do the job.
Use the translate version if you want to keep as many blanks as there were 'z' - else use the compress version.
HTH
Patrick

data name;
input id$ name$ add$;
array vars {*} name add;
do i=1 to dim(vars);
if compress(lowcase(vars{i}),'z')='' then
/* vars{i}=translate(lowcase(vars{i}),' ','z');*/
vars{i}=compress(lowcase(vars{i}),'z');
put vars{i}= @;
end;
put;
cards;
mailtohim nani mainroad
zzzzz zzz street
raju zzAzz zzzzzzzzzzzzz
zzzzzzzz zz indi
run;
Ask a Question
Discussion stats
  • 5 replies
  • 132 views
  • 0 likes
  • 5 in conversation