BookmarkSubscribeRSS Feed
srinath3111
Quartz | Level 8

data ds;
infile datalines;
input var1 var2$10.;
datalines;
201 Pakistan
206 Bangladesh
209 Russia
208 China

245 In

862 L
;
run;

what i need is


Paxxstan
baxxladesh
Ruxxia

chxxa

In
L

4 REPLIES 4
PaigeMiller
Diamond | Level 26
data want;
    set have;
    substr(country,3,1)='x';
    substr(country,4,1)='x';
run;
--
Paige Miller
Astounding
PROC Star

One case that is missing from your example:  Sri

 

Should that become Srx or should it become Srxx?

 

At any rate, here are the basic tools that you can modify if needed:

 

if length(var2) > 2 then substr(var2, 3, 2) = 'xx';

soham_sas
Quartz | Level 8

data want;
set ds;
if length(var2) >2 then
var3 = tranwrd(var2,substr(var2,3,2),"XX");
else var3 = var2;
run;

SuryaKiran
Meteorite | Level 14

You can use substr() function along with IF condition:

data want;
set ds;
if length(var2)<=2 then new_var=var2;
else if length(var2)=3 then new_var=substr(var2,1,2)||"x"||substr(var2,5);
else new_var=substr(var2,1,2)||"xx"||substr(var2,5);
run;

Another alternate solution will be using the Perl expression:

data want;
set ds;
new_var=substr(var2,1,2)||prxchange('s/[a-z][a-z]/xx/i',1,substr(var2,3));
run;
Thanks,
Suryakiran

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!

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