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

Hi All,

 

I think I have an easy one and multiple ways to go about it but I just can't get it to work as I'm used to character formats and I'm trying to do this with a numeric.

 

Datahave

123456789

 

Datawant

896712345

 

Last two become the first two, second to last 2 become the 2nd 2 and finally first 5 become last 5.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Use a character value as go-between:

charval = put(numval,z9.);
charval = substr(charval,8,2) !! substr(charval,6,2) !! substr(charval,1,5);
numval_new = input(charval,9.);
drop charval;

Another, pure numerical way would involve multiple use of divisions, INT and MOD functions.

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Use a character value as go-between:

charval = put(numval,z9.);
charval = substr(charval,8,2) !! substr(charval,6,2) !! substr(charval,1,5);
numval_new = input(charval,9.);
drop charval;

Another, pure numerical way would involve multiple use of divisions, INT and MOD functions.

PGStats
Opal | Level 21

A purely numeric method would be:

 

data test;
array _i{9};
array _o{*} _i8 _i9 _i6 _i7 _i1 _i2 _i3 _i4 _i5;
numIn = 123456789;
_x = numIn;
do i = dim(_i) to 1 by -1;
    _i{i} = mod(_x, 10);
    _x = int(_x/10);
    end;
_x = 0;
do i = 1 to dim(_o);
    _x = _x*10 + _o{i};
    end;
numOut = _x;
drop i _:;
run;

This kind of math occurs behind the scene whenever you convert between number and character.

PG

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 845 views
  • 2 likes
  • 3 in conversation