user3252148
user3252148

Reputation: 153

Logistic regression Macro

    %macro intercept(i1= ,i2= );
      %let n = %sysfunc(countw(&i1));

       %do i = 1 %to &n;
          %let val_i1 = %scan(&i1,&i,'');
          %let val_i2 = %scan(&i2,&i,'');
          data scores;
            set repeat_score2;
            /* Segment 1 probablity score  */
            p1 = 0;
            z1 =   &val_i1      +
                    a         *      0.03            +
                    r         *    -0.0047841        +
                    p         *   -0.000916081      ;

            p1 = 1/(1+2.71828**-z1);

            /* Segment 2 probablity score  */
           p2 = 0;
           z2 =   &val_i2           +
                   r           *       0.09           +
                   m           *     0.012786245      +
                   c           *    -0.00179618        +
           p2 = 1/(1+2.71828**-z2);

           logit_score = 0;
           if max(p1,p2) =      p1 then logit_score = 1;
           else if max(p1,p2) = p2 then logit_score = 2;
         run;

         proc freq data = scores;
            table logit_score * clu_ /nocol norow nopercent;
         run;
    %end;
  %mend;
%intercept (i1=-0.456491042, i2=-3.207379842, i3=-1.380627318 , i4=0.035684096, i5=-0.855283373);
%intercept (i1=-0.456491042 0, i2=-3.207379842 -3.207379842, i3=-1.380627318 -1.380627318, i4=0.035684096 0.035684096,
            i5=-0.855283373 -0.855283373);

I have the above macro which takes the intercept for the two of the above models and then calculates the probablity score and then assigns a a value to to a segment based on that probablity score.

The first problem with above macro is when I execute the macro with one argument each it's resolving macro variable 'n' to 2 and executing twice. First iteration, it's giving the right results while for second it's wrong.

For the second implementation(macro with two aruguments each) n is resolving to 3 and scan is resolving to both of those values together at a time (eg. i1 for the iteration itself is -0.45 and 0), If I remove the space, then it taking '.' to be the delimiter and resolving that to ( 0,45,0 - one for each iteration). I don't get any results for this case.

How do I get this to work the right way?

Thanks!!!

Upvotes: 0

Views: 137

Answers (1)

Dmitry Shopin
Dmitry Shopin

Reputation: 1763

%SCAN and COUNTW function by default consider punctuation symbols and blanks as delimiters. Since your arguments include decimal points, you need to state explicitly that delimiter should be blank for both COUNTW and %SCAN. Which you have done for %SCAN, but not for COUNTW. So the 2nd line of the code should be:

 %let n = %sysfunc(countw(&i1,' '))

And I'm not sure if it's a typo or just formatting thing, but in your %SCAN functions third argument looks like two quotes together '', not quote-blank-quote ' ' as it should be.

Upvotes: 1

Related Questions