SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Manipulating the first character in a string

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

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
Trusted Advisor
Posts: 1,131

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,

Jagadish

Thanks,
Jag

View solution in original post


All Replies
Solution
‎10-28-2013 06:21 AM
Trusted Advisor
Posts: 1,131

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,

Jagadish

Thanks,
Jag
Frequent Contributor
Posts: 89

Re: Manipulating the first character in a string

Thanks, you're a top-quality Jagadish. Smiley Happy

Respected Advisor
Posts: 3,777

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.

Need further help from the community? Please ask a new question.

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