'Convert SAS if else to python if else
I am converting SAS script to Python and there's a if then else statement that I would like to convert to Python.
Its something like
if mi_proceeds<=1 then mi_coverage =0.01; else mi_coverage = (mi_proceeds/claim_amt)
if mi_coverage<0.01 then mi_coverage=0.01; else if mi_coverage>0.01 then mi_coverage=1.00
Solution 1:[1]
Its an odd bit of code, but yeah the answer is very simple.
SAS:
if mi_proceeds<=1 then mi_coverage =0.01; else mi_coverage = (mi_proceeds/claim_amt) if mi_coverage<0.01 then mi_coverage=0.01; else if mi_coverage>0.01 then mi_coverage=1.00
python:
if mi_proceeds<=1:
mi_coverage =0.01
else:
mi_coverage = (mi_proceeds/claim_amt)
if mi_coverage<0.01:
mi_coverage=0.01
elif mi_coverage>0.01:
mi_coverage=1.00
Solution 2:[2]
You are not very clear on what you actually want to do, but I suspect you are not asking how to write python if statements, like some commenters seem to, but rather you are probably asking how to write a compiler for this task.
Beware, I am not very well acquainted with SAS. The solution might not be as straightforward as one might think. In a vacuum, it's just a regular language: replace then with :\n\t, ; else with \n else: \n\t. However, in practice, python's indentation makes the task slightly more difficult. The parsed result will depend on the surrounding indentation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Daniel Thomas |
| Solution 2 | Antilos |
