BookmarkSubscribeRSS Feed
KafeelBasha
Quartz | Level 8

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

4 REPLIES 4
ballardw
Super User

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;

jklaverstijn
Rhodochrosite | Level 12

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.

 

KafeelBasha
Quartz | Level 8

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.

FreelanceReinh
Jade | Level 19

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1143 views
  • 1 like
  • 4 in conversation