I am trying to format a particular field in the below format: If the value is 60.00 it has to be displayed as 60. if the value is 14.32 it has to displayed as 1432 and if it is 0.00 then output should be 0 i.e. No decimal should be Displayed.
Below is the datasets and the option i have tried
data input_dataset;
input srno $ bill_amt $10.;
datalines;
1 60.00
2 0.00
3 14.32
;
run;
data test;
set input_dataset;
format mc062 $10.;
mc062 = put((bill_amt ),10.);
run;
Expected Result:
MC062:
60
0
1432
@Anmolkhandelwal wrote:
I am trying to format a particular field in the below format: If the value is 60.00 it has to be displayed as 60. if the value is 14.32 it has to displayed as 1432 and if it is 0.00 then output should be 0 i.e. No decimal should be Displayed.
Below is the datasets and the option i have tried
data input_dataset;
input srno $ bill_amt $10.;
datalines;
1 60.00
2 0.00
3 14.32
;
run;
data test;
set input_dataset;
format mc062 $10.;
mc062 = put((bill_amt ),10.);
run;
Expected Result:
MC062:
60
0
1432
So do you have a generic rule for how to differentiate between when 60.00 is displayed as 60 and 14.32 as 1432?
I could not tell what I would have to do if the value was 55.55 or 66.00 or anything else. We need an explicit generic rule unless your data only consists of those three values.
Do you expect the result to be a numeric value or a character value?
And likely if the value were numeric to start with it will be easier to implement. Also since your example code reads the values as 10 characters you might want to provide longer values if they are encountered as you may have yet more rules not yet stated if the bill_amt is something like 1,234.00
No generic Rule as such. Just the decimal has to be removed from the given value.
if the value is 55.55: has to be displayed as 5555 or if the value is 66.00 the value i want to output is 66.
the result can be in numeric format.
data input_dataset;
input srno $ bill_amt $10.;
datalines;
1 60.00
2 0.00
3 14.32
;
run;
data want;
set input_dataset;
k=input(bill_amt,8.);
if k ne 0 then k1=k/int(k);
want=ifn(k1 ne 1 ,k*100,k);
drop k:;
run;
@Anmolkhandelwal Since you mentioned "the result can be in numeric format." I have kept the result want variable as numeric.
@Anmolkhandelwal wrote:
No generic Rule as such. Just the decimal has to be removed from the given value.
if the value is 55.55: has to be displayed as 5555 or if the value is 66.00 the value i want to output is 66.
the result can be in numeric format.
Then why would 14.32 end up as 1432 (as stated in your initial post)?
@Anmolkhandelwal wrote:
No generic Rule as such. Just the decimal has to be removed from the given value.
if the value is 55.55: has to be displayed as 5555 or if the value is 66.00 the value i want to output is 66.
the result can be in numeric format.
That further example sounds a whole lot like "if the decimal portion is 00 then remove, else remove the decimal point"
Which this may help with:
data input_dataset; input srno $ bill_amt $10.; length mc062 $ 10; if index(bill_amt,'.00')>0 then mc062 = scan(bill_amt,1,'.'); else if index(bill_amt,'.')>0 then mc062 = transtrn(bill_amt,'.',trimn("")); datalines; 1 60.00 2 0.00 3 14.32 ; run;
@ballardw Clever as always 🙂 You don't have blood in your veins. It's all brains. Well done!
Doesn't Work
Doesn't work is awful vague.
Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.
No output? Post any log in a code box.
Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
I don't understand why you would do this.
How are you going to tell the difference between 1.23 and 123 ?
data input_dataset;
input srno $ bill_amt $10.;
datalines;
1 60.00
2 0.00
3 14.32
;
run;
data test;
set input_dataset;
format mc062 $10.;
mc062 = compress(prxchange('s/\.0+$//',1,strip(bill_amt) ),'.');
run;
Hello,
How do you remove decimals and insert commas for non-currency numbers?
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.