BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
EinarRoed
Pyrite | Level 9

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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

3 REPLIES 3
Jagadishkatam
Amethyst | Level 16

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
EinarRoed
Pyrite | Level 9

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

data_null__
Jade | Level 19

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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