Solved
Contributor
Posts: 27

# Replacing first letter or value or last letter or value in given set of observations.

Hello All,

I want to replace first letter and last letter in given set of observations.

Below are my questions.

Q1: I have a variable called Road_No and i have 5000 observations in that.

I would like to replace first letter of the observations with H. How do i do it. Road_No is character variable and length of the observations are 7.

Example: Road_No are A001010,BC01010,and so on, I want replace the first letter of A and B to H001010 ,HC01010

Q2: Same way how do i replace last digit with M.

Q3: How do i replace third digit.  Example: HA01011 To HAB1011

Regards,

Bidd

Accepted Solutions
Solution
‎10-13-2017 04:47 AM
Contributor
Posts: 44

## Re: Replacing first letter or value or last letter or value in given set of observations.

Are you creating three different variables from one or you are only interested in replacing the first, third and last?

All Replies
Contributor
Posts: 44

## Re: Replacing first letter or value or last letter or value in given set of observations.

You can try this:

data want;
set have;
run;

Contributor
Posts: 27

## Re: Replacing first letter or value or last letter or value in given set of observations.

Solution
‎10-13-2017 04:47 AM
Contributor
Posts: 44

## Re: Replacing first letter or value or last letter or value in given set of observations.

Are you creating three different variables from one or you are only interested in replacing the first, third and last?

Contributor
Posts: 44

## Re: Replacing first letter or value or last letter or value in given set of observations.

if you are creating three separate variables, this should work..

data want;
set have;

run;

good luck

Contributor
Posts: 27

## Re: Replacing first letter or value or last letter or value in given set of observations.

HI, I left office some time back, will check it on Monday, thanks for ur help.
Contributor
Posts: 27

## Re: Replacing first letter or value or last letter or value in given set of observations.

Hi, Yes I am creating 3 different new variables keeping the original as reference, so all new variables new replacement with first digit, third and last.
Contributor
Posts: 27

## Re: Replacing first letter or value or last letter or value in given set of observations.

Hi, Yes I am creating 3 different new variables keeping the original as reference, so all new variables new replacement with first digit, third and last.
Frequent Contributor
Posts: 113

## Re: Replacing first letter or value or last letter or value in given set of observations.

Try this...

``````data HAVe;
INPUT VALUE \$7.;
CARDS;
A001010
B001010
C001010
D001010
;
run;

data WANT;
set HAVE;

value=ifc(substr(value,1,1) in ('A','B'), 'H'||substr(value,2), value); *replace 1st char if 'A' or 'B';

value = substr(value,1,6)||'M'; *REPLACE LAST CHAR;
value = substr(value,1,2)||'B'||substr(4,4); *REPLACE THIRD CHAR;
run;``````

Hope this helps.

Contributor
Posts: 27

## Re: Replacing first letter or value or last letter or value in given set of observations.

Hi, I left office some time back, will try running this on Monday. thanks for your help.
Super User
Posts: 9,427

## Re: Replacing first letter or value or last letter or value in given set of observations.

There is a trick here which is useful to learn.  The substr function can appear on the left of an equals sign.  What this does is for the given substr command put the text from the right in, so you can do:

```data want;
set have;
run;```

So what we do here is with the substr specify where the characters on the right should go in the string.  In the second one I take the lengthn of the string and drop back one char to get the last char position.

Contributor
Posts: 27

## Re: Replacing first letter or value or last letter or value in given set of observations.

check running this on Monday
Contributor
Posts: 27