BookmarkSubscribeRSS Feed
Rasheed
Calcite | Level 5

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);

11 REPLIES 11
Kurt_Bremser
Super User

%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.

Rasheed
Calcite | Level 5

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

Rasheed
Calcite | Level 5

Please guide how could I write correctly this line

%if y < 2.49482 %then

Rasheed
Calcite | Level 5

The y variable is numeric

Kurt_Bremser
Super User

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?

Ksharp
Super User

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

Astounding
PROC Star

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.

Rasheed
Calcite | Level 5

Thanks a lot

Tom
Super User Tom
Super User

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;

Rasheed
Calcite | Level 5

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

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!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 1353 views
  • 1 like
  • 5 in conversation