Hello
I would like to define a function which will return log() only for positive values. If values are less than or equal to 0 then it should delete the observations.
Below are the codes I have tried. Correct me where I am going wrong.
proc fcmp outlib=work.Validlog.MathFuncs;
function Vlog(y);
if y>0 then return( log(y));else delete;
return(.);
endsub;
options cmplib=work.Validlog;
data values;
input x;
sf=vlog(x);
datalines;
1
3
0
-4
.
10
;
run;
THanks
I think you would be better off with datastep to remove the observations:
data want;
set have;
if x le 0 then delete;
else sf=vlog(x);
run;
I am assuming you actually want to do more otherwise the complexity of setting up a call a function to replcace a SAS function is introducing unneeded complexity to a program.
Data is intended to be passed to and returned from the function. Since DELETE would affect variables not explicitly passed to the function (ie everything in that observation)that doesn't make sense for a functions behavior.
if you mean to return a MISSING value, which makes sense then instead of DELETE
proc fcmp outlib=work.Validlog.MathFuncs;
function Vlog(y);
if y>0 then return( log(y));
else return(.);
endsub;
Instinctively I'd go with @ballardw and say id wouldn't make sense to support DELETE in FCMP. And indeed using it results in a run-time (not compile time!) error:
ERROR: Parsing error generating code.
However, I tried to find support for this in the docs and couldn't find it. In fact, the 9.1 pdf doc of FCMP states that DELETE is in fact supported. So go figure.
Anyways the use case of @KafeelBasha makes it a trivial problem. Which does not preclude the existence of use cases that make it desirable to be able to delete observations.
- Jan.
Thanks for the explanation.
I came across this example while searching for FCMP documention.
When I use log() function in SAS, I got the same result as we will get after definition the function Vlog() which returns missing(.) for zeros and negative values.
I wonder the particular example is discussed because sas will return the value 0 for zeros and negative values in previous(old) versions. Is it so?
Request you to provide me some examples using FCMP which is more focused towards Mathematics.
Hi @KafeelBasha,
I don't think the LOG function in old versions of SAS "return[ed] the value 0 for zeros and negative values." Wouldn't that have been worse than a cheap pocket calculator?
In this very recent thread PROC FCMP was used to implement a formula involving (a special kind of) an incomplete gamma function.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: