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.
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
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 |
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
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;
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.",".");
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.