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

Hello SAS world,

 

I need help with the following problem. 

I've a column with a 8 digit number, this should be padded with zeros in the appropriate places until the 9 digit, 5-4 format is established. 

Example:                 original data                                             Convert to

                       (4-4) XXXX-XXXX                                   (5-4) 0XXXX-XXXX

                       (5-3) XXXXX-XXX                                   (5-4) XXXXX-0XXX

 

Thanks

--Sujith

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Is there a - in the middle of the digits? If not how do you know which part may need the padding? And if the is a - it is not a "number" but a character value.

 

Something like this may get you started.

data junk;
   length x $ 10;
   input x $ 1-9;
   x= catx('-',put(input(scan(x,1),best.),z5.),put(input(scan(x,2),best.),z4.));

datalines;
1234-1234
12345-123
;
run;

Note if your orginal variable is specified as a 9 character field the result will not fit and you'll need the length set before any SET statement to allow the code to work.

 

View solution in original post

6 REPLIES 6
Reeza
Super User

Is this a character variable?

Can you post some more sample data we can work with?

Have you looked at the Z5/Z4 formats with a PUT statement?

 

 

ballardw
Super User

Is there a - in the middle of the digits? If not how do you know which part may need the padding? And if the is a - it is not a "number" but a character value.

 

Something like this may get you started.

data junk;
   length x $ 10;
   input x $ 1-9;
   x= catx('-',put(input(scan(x,1),best.),z5.),put(input(scan(x,2),best.),z4.));

datalines;
1234-1234
12345-123
;
run;

Note if your orginal variable is specified as a 9 character field the result will not fit and you'll need the length set before any SET statement to allow the code to work.

 

Sujithpeta
Quartz | Level 8

Hello,

 

Yes, there is a - in the middle of the digits and it's character (my mistake).

If you don't mind, can you please your 3rd line in your code is doing? It would be a great learning.

 

Thanks

--Sujith

Patrick
Opal | Level 21

Sample data would help. Here a code sample making assumptions on how your data looks like.

data sample;
  have=' 4444-333 ';
  /* bit of spaghetti code but should work and doesn't need intermediary variables */
  want=put(input(scan(have,1,'-'),f5.),z5.)||'-'||put(input(scan(have,2,'-'),f4.),z4.);
  output;
  stop;
run;

For your real implementation: You probably should also have a check that your source string looks as expected so that you don't unvoluntarily remove digits/characters when converting to your want string.

Jagadishkatam
Amethyst | Level 16

you could also try the perl regular expression

 

 

data junk;
   length x $ 10;
   input x $ 1-9;
if prxmatch('m/\d{5,}\-\d{3,}/',x) then new=prxchange('s/(\d{5,})\-(\d{3,})/\1-0\2/',-1,x);
else if prxmatch('m/\d{4,}\-\d{4,}/',x) then new=prxchange('s/(\d{4,})\-(\d{3,})/0\1-\2/',-1,x);
else if prxmatch('m/\d{5,}\-\d{4,}/',x) then new=x;
datalines;
1234-1234
12345-123
;
run;
Thanks,
Jag
Sujithpeta
Quartz | Level 8
Thanks for helping me out guys!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1136 views
  • 0 likes
  • 5 in conversation