BookmarkSubscribeRSS Feed
ari
Quartz | Level 8 ari
Quartz | Level 8

Hi,

 

I want to replace the missing (spaces) in character variable with . (dot). 

 

Is there any way to do this in SAS?

 

Thanks

3 REPLIES 3
Astounding
PROC Star

If the original variable is 5 characters long, do you want 1 dot or 5 dots?  

 

If there is text in the variable, but the text includes some blanks, do you want to replace those blanks too?

LaurieF
Barite | Level 11

Yes. Fortunately it's very easy. The translate function does it, but the order of the paramater is odd. The second parameter is what you want the variable changed to, and the third is what you want it changed from.

data _null_;
length before after $ 10;
do i = 1 to 15;
   do j = 1 to 10;
      if ranuni(225465114) < .15
         then substr(before, j, 1) = ' ';
         else substr(before, j, 1) = byte(int(ranuni(225465114) * 26) + 1 + 64);
      end;
   after = translate(before, '.', ' ');
   put (before after) ($quote10. +2);
   end;
run;

 

The else line is randomly populating a ten-character field with capital letters, but there's a 15% chance (previous line) that it'll be a space instead.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

A good idea is to always post test data in the form of a datastep and what you want the output to look like so we avoid guessing certain things.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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