BookmarkSubscribeRSS Feed
R_Win
Calcite | Level 5
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
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
The OP stated clearly that the string to be manipulated represented a "word" -- so possibly TRANWRD may apply.

Scott Barry
SBBWorks, Inc.
Peter_C
Rhodochrosite | Level 12
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
Patrick
Opal | Level 21
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;

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