简体   繁体   中英

in SAS proc fcmp how to get 2 numbers returned

I have a function that should return 2 numbers (geographic coordinates).
Is it possible to get the function to return 2 numbers?

Here is my function (I need to return x and Y)

proc fcmp outlib=common.functions.geo;
    function latlng2lambert72(lat,lng);

        LongRef = 0.076042943;
        bLamb = 6378388 * (1 - (1 / 297));
        aCarre = 6378388 ** 2;
        eCarre = (aCarre - bLamb ** 2) / aCarre;
        KLamb = 11565915.812935;
        nLamb = 0.7716421928;

        eLamb = sqrt(eCarre);
        eSur2 = eLamb / 2;

        *conversion to radians;
        lat_rad = (constant("pi") / 180) * lat;
        lng_rad = (constant("pi") / 180) * lng;

        eSinLatitude = eLamb * sin(lat_rad);
        TanZDemi = (tan((constant("pi") / 4) - (lat_rad / 2))) * 
                    (((1 + (eSinLatitude)) / (1 - (eSinLatitude))) ** (eSur2));

        RLamb = KLamb * ((TanZDemi) ** nLamb);

        Teta = nLamb * (lng_rad - LongRef);

        x = 150000 + 0.01256 + RLamb * sin(Teta - 0.000142043);
        y = 5400000 + 88.4378 - RLamb * cos(Teta - 0.000142043);
        *put x y ;

        return (x);
        *return (x*1000000000000 + y);
        *return (x||'_'||y);
    endsub;
quit;

data test;
    lat = 50.817500;
    lng = 4.374400;
    x = latlng2lambert72(lat,lng);
    run;

I guess not but then the only option I see would be to make 2 functions and have one return the 1st number and the other return the 2nd number. These 2 functions would be 99% identical and I don't like to duplicate code. Is there a more efficient way to achieve this?

(I don't really understand how subroutines work. Could they be used to that end? Execute the common code and just make 2 short functions to return x and y?)

Functions by definition return a single value; that's the definition of a function. It should be able to be on the right hand side of an equal sign.

Subroutines, in SAS known as Call routines, are able to "return" zero, one, or more values. They do this by not returning anything directly (you cannot put a call routine on the right-hand side of an equal sign), but by modifying the arguments they are called with.

You see this in routines like call missing , which can set any number of values to missing; you see it a bit more directly in routines like call scan , which tells you the start position and length of the nth word in a string.

In order to do this, then, you would first want to change your function to a subroutine/call routine, ie replace function with subroutine , and then specify OUTARGS .

An example of this would be:

proc fcmp outlib=work.funcs.func;
  subroutine roots(inval, outval_pos, outval_neg);
    outargs outval_pos,outval_neg;  *specifies these two will be "returned";
    outval_pos = sqrt(inval);
    outval_neg = -1*outval_pos;
  endsub;
quit;

options cmplib=work.funcs;

data _null_;
 x=9;
 call missing(y_pos,y_neg);
 call roots(x,y_pos, y_neg);
 put x= y_pos= y_neg=;
run;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM