mathsastrigonometrydegreesradians

How to switch between radians and degrees in SAS


just looking for an easy way to run trig functions in SAS without having to manually correct in each calculation. Below is what I am working with.

I am running this in SAS 9 probably, the SAS Studio Student Module but this is a general SAS question.

I have manually created a variable, 'rad' in the 'calc' data step to deal with this but it adds a step of complexity that I would like to avoid.

I am asking whether there is a system setting, alternate trig function or ... ? that would change the calculation from: bh_x = cos(rad*bh_a)*bh_l ; to: bh_x = cos(bh_a)*bh_l ; so I don't have to manually convert my angle in degrees to radians for the trig function to work.

Thanks to anyone reading this and putting any mental effort to the solution! Tim

data spec   ;
    length
        b2h_a 8
        b2h_l 8
        b2h_l_e 8
        bike $ 8
        name $ 16
    ;
    input
        bike $
        name $
        bh_a
        bh_l
        ht_a
        spcr
        st_h
        st_a
        st_l
        hb_r
        hb_a
    ;
    datalines   ;
        srcn (0,0) 0 0 67 0 0 0 0 0 0
        srcn c 41 658 71.5 27 40 25 120 100 13
        srcn ne_27_n13 41 658 71.5 27 40 27 127 100 13
        srcn ne_15_0 41 658 71.5 15 40 27 127 100 0
        srcn ne_5_0 41 658 71.5 5 40 27 127 100 0 
        srcn ne_2_n9 41 658 71.5 2 40 27 127 100 9
        srcn ne_5_10 41 658 71.5 5 40 27 127 100 -10
        srcn ne_10_rf10 41 658 71.5 10 40 27 127 20 -10
        srcn max 41 658 90 250 0 0 250 0 0
    ;
run ;

data calc   ;
    set spec    ;

    pi=constant('pi')   ;
    rad=pi/180  ;

    bh_x = cos(rad*bh_a)*bh_l   ;
    bh_y = sin(rad*bh_a)*bh_l   ;

    sr_x = (cos(rad*ht_a)*(spcr+st_h/2))*-1 ;
    sr_y = sin(rad*ht_a)*(spcr+st_h/2);

    st_x = cos(rad*(90-ht_a+st_a))*st_l ;
    st_y = sin(rad*(90-ht_a+st_a))*st_l ;

    hb_x = cos(rad*(90-hb_a))*hb_r*-1   ;
    hb_y = sin(rad*(90-hb_a))*hb_r  ;

    hd_x = bh_x + sr_x + st_x + hb_x    ;
    hd_y = bh_y + sr_y + st_y + hb_y    ;

    if hd_x=0 then do   ;
        b2h_a=0 ;
        b2h_l=0 ;
    end ;
    else do ;
        b2h_a = atan(hd_y/hd_x)/rad ;
        b2h_l = hd_y/sin(b2h_a*rad) ;
    end ;

    b2h_l_e = b2h_l/25.4    ;

    drop pi rad ;

    format
        b2h_a 5.
        b2h_l 5.
        b2h_l_e 5.
        bh_a 5.
        bh_l 5.
        ht_a 5.
        spcr 5.
        st_h 5.
        st_a 5.
        st_l 5.
        hb_r 5.
        hb_a 5.
        bh_x 5.
        bh_y 5.
        sr_x 5.
        sr_y 5.
        st_x 5.
        st_y 5.
        hb_x 5.
        hb_y 5.
        hd_x 5.
        hd_y 5.
        b2h_a 5.
        b2h_l 5.
        b2h_l_e 5.1
        ;

run ;

Solution

  • There are no trig functions in SAS that accept DEGREE or GRADIAN arguments. You always need to convert from your data's angular measurement system to RADIAN.

    You can write a macro to perform the conversion. Example:

    %macro cosD(theta);
      %* theta is angle in degrees;
      %* emit data step source code that performs conversion from degrees to radians;
      cos(&theta*constant('PI')/180)
    %mend;
    

    In use:

    data calc   ;
        set spec    ;
    
        bh_x = %cosD(bh_a) * bh_l   ;
    

    You could convert the angular data to radians during the step where input occurs and then not have to worry about it again.