## Manipulating the first character in a string

Solved
Frequent Contributor
Posts: 100

# Manipulating the first character in a string

Hi,

I have an input table with a character column called "CUST_NO". It includes values such as these:

"4512648"

"5487512"

"6123658"

"6548756"

The first character will always be 4, 5, or 6.

What I want to do is to reduce the the first character of each by 3. So the output should be like this:

"1512648"

"2487512"

"3123658"

"3548756"

I figure I might use an expression which starts this way:

firstchar = substr(CUST_NO,1,1)

case

when firstchar = "4" then firstchar = "1"

when firstchar = "5" then firstchar = "2"

when firstchar = "6" then firstchar = "3"

end

However, I can't think of an elegant way to proceed after this. Can you please advise me?

Accepted Solutions
Solution
‎10-28-2013 06:21 AM
Posts: 1,147

## Re: Manipulating the first character in a string

data have;

input  CUST_NO \$;

firstchar=put(input(substr(CUST_NO,1,1),8.)-3,1.)||strip(substr(CUST_NO,2));

cards;

4512648

5487512

6123658

6548756

;

Thanks,

Thanks,
Jag

All Replies
Solution
‎10-28-2013 06:21 AM
Posts: 1,147

## Re: Manipulating the first character in a string

data have;

input  CUST_NO \$;

firstchar=put(input(substr(CUST_NO,1,1),8.)-3,1.)||strip(substr(CUST_NO,2));

cards;

4512648

5487512

6123658

6548756

;

Thanks,

Thanks,
Jag
Frequent Contributor
Posts: 100

Posts: 3,847

## Re: Manipulating the first character in a string

Alternatively you could translate the character digits and use substr on the left side of =.

data first;
input x :\$quote16.;
substr(x,
1,1)=translate(first(x),'123','456');
cards;
"4512648"
"5487512"
"6123658"
"6548756"
;;;;
run;
proc print;

run;
🔒 This topic is solved and locked.

Discussion stats
• 3 replies
• 273 views
• 1 like
• 3 in conversation