SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Problem in if Condition

Reply
Frequent Contributor
Posts: 84

Problem in if Condition

I wrote SAS program and facing some difficulty, SAS code is given below

Code description

The objective of this SAS program is to obtain correct value of variable count in each iteration of macro final.

In macro final there are some macro like %macro Boot_Perzentil1, %boot1, %boot_reml1, %boot_grenze1, together all these macros produce a single variable y and that value of y should be checked either < or > 2.49482 under the macro of %Boot_Perzentil1.

I got all macros write but code below is not working correctly, every time I run this program get count is 0 even y < 2.49482

%if y < 2.49482 %then %do;

%let count=% eval(&count+1);

%Put count is &count;

%end;

%else %do;

%let count=&count;

%Put count is &count; 

%end

Plz help me

                                          Complete SAS Code

%macro final(sim);                                                                                                                                                                                                                                         

   %let count=0;                                                                                                                                                                                                                                               

   %do rep= 1 %to 2;                                                                                                                                                                                                                                           

                                                                                                                                                                                                                                                               

   %let D=&simdata&rep;                                                                                                                                                                                                                                         

                                                                                                                                                                                                                                                               

         %macro Boot_Perzentil1(data,pat,b_reps);                                                                                                                                                                                                              

                                                                                                                                                                                        

         %do b_rep=1 %to &b_reps;                                                                                                                                                                                                                              

         %put Boot &b_rep;                                                                                                                                                                                                                                     

                                                                                                                                                                                                                                                               

             %boot1(&data,btest_1,9);                                                                                                                                                                                                                          

             %boot_reml1(btest_1,outin_1,9,logAUC,logCmax,1);                                                                                                                                                                                                  

        %end;                                                                                                                                                                                                                                                  

                                                                                                                          

            %boot_grenze1(outin_1,&b_reps);                                                                                                                                                                                                                    

        run;                                                                                                                                                                                                                                                   

 

%if y < 2.49482 %then %do;                                                                                                                                                                                                                                  

%let count=%eval(&count+1);                                                                                                                                                                                                                                    

%Put count is &count;                                                                                                                                                                                                                                      

%end;                                                                                                                                                                                                                                                          

%else %do;                                                                                                                                                                                                                                                     

%let count=&count;                                                                                                                                                                                                                                             

%Put count is &count; 

%end;                                                                                                                                                                                                                                                          

                                                                                                                                                                                                                                                               

        %mend;                                                                                                                                                                                                                                                 

       %Boot_Perzentil1(&D,9,1);                                                                                                                                                                                                                              

                                                                                                                                                                                                                                                              

%end;                                                                                                                                                                                                                                                         

%mend;                                                                                                                                                                                                                                                         

                                                                                                                                                                                                                                                               

%final(want);

Super User
Posts: 7,854

Re: Problem in if Condition

%if y < 2.49482 %then

This condition will always be false, as the character "y" comes later in the character sequence than the character "2".

Keep in mind that the macro processor has only "character" as data type, and that it runs before any data step variables come into existence.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 7,854

Re: Problem in if Condition

PS I see no other reference to "y" outside of the %if statement. What should "y" be?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 84

Re: Problem in if Condition

Posted in reply to KurtBremser

Y is the variable that is generated in the result of macro %boot_grenze1(outin_1,&b_reps);

Frequent Contributor
Posts: 84

Re: Problem in if Condition

Please guide how could I write correctly this line

%if y < 2.49482 %then

Frequent Contributor
Posts: 84

Re: Problem in if Condition

The y variable is numeric

Super User
Posts: 7,854

Re: Problem in if Condition

No, it is not a variable, it is the character string "y".

IF you created a macro variable earlier with %let, then you must reference it as &y.

%let y=1;

%macro test1;

%if y < 2 %then %put "This is fun"; %else %put "not funny";

%mend;

%test1;

%macro test2;

%if &y < 2 %then %put "This is fun"; %else %put "not funny";

%mend;

%test2;

How did you create y?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 10,044

Re: Problem in if Condition

In default , SAS will put %eval() around it , for your question ,you need to put %sysevalf() around it to let SAS know it float not integer .

Code: Program

%let y=2;
%macro test;
%if %sysevalf(&y < 2.49482) %then %put "This is fun";
%else %put "not funny";
%if &y < 2 %then %put "This is fun";
  %else %put "not funny";
%mend;
%test


Log: Program

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;

57 

58 %let y=2;

59 %macro test;

60 %if %sysevalf(&y < 2.49482) %then %put "This is fun";

61 %else %put "not funny";

62 %if &y < 2 %then %put "This is fun";

63 %else %put "not funny";

64 %mend;

65 %test

"This is fun"

"not funny"

66 

67 

68 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;

80 


Xia Keshan

Super User
Posts: 5,516

Re: Problem in if Condition

If y is a macro variable, you have to refer to it as &y.  Since you are comparing decimal fractions, you need a function that allows macro language to work with decimal fractions as numbers:

%if %sysevalf (&y < 2.48492) %then %do;

Good luck.

Frequent Contributor
Posts: 84

Re: Problem in if Condition

Posted in reply to Astounding

Thanks a lot

Super User
Super User
Posts: 7,076

Re: Problem in if Condition

Assuming Y is a macro variable then you probably want to use %SYSEVALF() to compare it to a non-integer value.

Formatting your code will also help.  Note that you should NOT embed macro definitions inside of other macro definitions.  That just does not make any sense and will make reading and maintaining the code harder.

%macro Boot_Perzentil1(data,pat,b_reps);

    %do b_rep=1 %to &b_reps;

        %put Boot &b_rep;

    %boot1(&data,btest_1,9);

    %boot_reml1(btest_1,outin_1,9,logAUC,logCmax,1);

    %end;

  %boot_grenze1(outin_1,&b_reps);

/*  You should not need a RUN statement here. Check the definition of BOOT_GRENZEL macro to make sure that

    it is properly terminating any data or proc steps that it generates

*/
    %if %sysevalf(&y < 2.49482) %then %do;

        %let count=%eval(&count+1);

        %Put count is &count;

    %end;

    %else %do;

        %let count=&count;

        %Put count is &count;

    %end;

%mend;

%macro final(sim);

      %let count=0;

      %do rep= 1 %to 2;

            %let D=&simdata&rep;

      %Boot_Perzentil1(&D,9,1);

      %end;

%mend;

Frequent Contributor
Posts: 84

Re: Problem in if Condition

Thank you very much I did the same and got the desired resutls

Ask a Question
Discussion stats
  • 11 replies
  • 537 views
  • 1 like
  • 5 in conversation