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

I need to covert data to remove the leading zeros from a text field that has decimals.  Here is what I have:

 

data Have;

infile datalines;
format textfield $8000.;
input textfield
;
datalines;
-3.51400
-0.00001
3.05000
0.09000
NA
-3.33100
-0.02900
-0.29000

;run;

data notwhatiwant;
set have;
get = put(input(textfield,8.5),7.5);
run;

textfield get need
-3.51400 -3.5140 -3.51400
-0.00001 -.00001 -.00001
3.05000 3.05000 3.05000
0.09000 0.09000 .09000
NA  . NA
-3.33100 -3.3310 -3.33100
-0.02900 -.02900 -.029000
-0.29000 -.29000 -.29000

 

I seem to be struggling with that 5th decimal place that I need when there is the negative and non-zero before the decimal.  And those that are positive with a leading zero. Seems like this should be easy, but boy am i struggling. Any assistance is appreciated. 

Thanks. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you want to convert text to text why not use text functions? Such as PRXCHANGE().

data have;
  input textfield $char40.;
datalines;
-3.51400
-0.00001
3.05000
0.09000
NA
-3.33100
-0.02900
-0.29000

;

data want;
  set have;
* Force NEWTEXT to have same length as TEXTFIELD ;
  newtext = textfield;
* Remove any zeros between optional - and . ;
  newtext = prxchange('s/(-{0,1})0*([\d\.]*)/$1$2/',-1,newtext);
run;

Results

Obs    textfield    newtext

 1     -3.51400     -3.51400
 2     -0.00001     -.00001
 3     3.05000      3.05000
 4     0.09000      .09000
 5     NA           NA
 6     -3.33100     -3.33100
 7     -0.02900     -.02900
 8     -0.29000     -.29000
 9


View solution in original post

5 REPLIES 5
Lost_Gary
Quartz | Level 8

Sorry - just 5 decimal places - one of my examples had too many.

 

textfield get need
-3.51400 -3.5140 -3.51400
-0.00001 -.00001 -.00001
3.05000 3.05000 3.05000
0.09000 0.09000 .09000
NA  . NA
-3.33100 -3.3310 -3.33100
-0.02900 -.02900 -.02900
-0.29000 -.29000 -.29000
Tom
Super User Tom
Super User

If you want to convert text to text why not use text functions? Such as PRXCHANGE().

data have;
  input textfield $char40.;
datalines;
-3.51400
-0.00001
3.05000
0.09000
NA
-3.33100
-0.02900
-0.29000

;

data want;
  set have;
* Force NEWTEXT to have same length as TEXTFIELD ;
  newtext = textfield;
* Remove any zeros between optional - and . ;
  newtext = prxchange('s/(-{0,1})0*([\d\.]*)/$1$2/',-1,newtext);
run;

Results

Obs    textfield    newtext

 1     -3.51400     -3.51400
 2     -0.00001     -.00001
 3     3.05000      3.05000
 4     0.09000      .09000
 5     NA           NA
 6     -3.33100     -3.33100
 7     -0.02900     -.02900
 8     -0.29000     -.29000
 9


Lost_Gary
Quartz | Level 8
Brilliantly Simple - Thank you.
Ksharp
Super User
data have;
  input textfield $char40.;
datalines;
-3.51400
-0.00001
3.05000
0.09000
NA
-3.33100
-0.02900
-0.29000
;

data want;
  set have;
  newtext = prxchange('s/^[\-0]+//',1,textfield);
  if first(textfield)='-' then newtext=cats('-',newtext);
run;
whymath
Barite | Level 11

Very impressive trick from @Tom  to keep the same length from raw variable.

As the question itself, in your specific example, replace "0." with "." will be OK, so you can use a tranwrd() function to solve this. 

* Force NEWTEXT to have same length as TEXTFIELD ;
  newtext = textfield;
* Repalce "0." with ".";
  newtext = tranwrd(newtext,"0.",".");

 

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

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 822 views
  • 2 likes
  • 4 in conversation