Hello,
I am working on creating a stability analysis program using macros to assist with our redundant stability analysis that we perform. For part of the program, I need to use a conditional statement to determine with model to run. However, in my example code I am unable to get the conditional statement to correctly execute based on a p-value that I have save into a dataset. The section of code that is giving me the issue is:
%macro Stability();
data stats;
set stats;
%if PROB > 0.25 %then %do;
proc glm data=stability;
model RP=time;
run;
%end;
%else %do;
proc glm data=stability;
class batch;
model RP=batch time batch*time;
run;
%end;
%mend Stability;
%Stability();
where "%if PROB > 0.25 %then %do;" is not leading to the use of the correct proc glm procedure being executed. Basically, in this part of the code I am trying to automate the process of testing for poolability of data across batches or if the batches need to be kept separate.
I have also attached an entire SAS code file for a full example. Any help would be greatly appreciated.
Thank you,
Jeff S. O.
Do you really use this?
data stats; set stats;
As shown a complete waste of CPU cycles as you copy data with no changes into the same data set.
Note: If you are going to work with macro programming I strongly suggest that you always include a RUN; statement to end every procedure.
You state : " conditional statement to correctly execute based on a p-value that I have save into a dataset. ". I see no reference to any variable in a data set that might be used in that manner.
If you intend to use a value from from a data set as a comparison with macro code you first have to make a MACRO variable. That would use the function CALL SYMPUTX. And since your reference a p-value then you need to take control of converting that p_value to the text that a macro comparison will use.
I suspect that you need something like :
data _null_; set stats; call symputx('prob', put(pvaluevariablename,f4.2) ); run;
%if &prob. > 0.25 %then %do;
The format you use to convert the pvalue to text is up to you. However you want to be aware of probably rounding of the p-value AND how the comparison works.
%if PROB > 0.25 %then %do;
This will never work, as to use %IF requires you test a macro variable against some constant. You can't test something named PROB as the macro facility will treat that as text and not a number. However this should work:
%if &PROB > 0.25 %then %do;
here I have used a macro variable &PROB — do you have such a macro variable? It's not obvious.
Hello,
Thank you for your follow up. PROB is a column in the dataset "stats" containing a numerical value. Based on this numerical value would depend which proc glm statement I want to run.
Thank you,
Jeff S. O.
Do you really use this?
data stats; set stats;
As shown a complete waste of CPU cycles as you copy data with no changes into the same data set.
Note: If you are going to work with macro programming I strongly suggest that you always include a RUN; statement to end every procedure.
You state : " conditional statement to correctly execute based on a p-value that I have save into a dataset. ". I see no reference to any variable in a data set that might be used in that manner.
If you intend to use a value from from a data set as a comparison with macro code you first have to make a MACRO variable. That would use the function CALL SYMPUTX. And since your reference a p-value then you need to take control of converting that p_value to the text that a macro comparison will use.
I suspect that you need something like :
data _null_; set stats; call symputx('prob', put(pvaluevariablename,f4.2) ); run;
%if &prob. > 0.25 %then %do;
The format you use to convert the pvalue to text is up to you. However you want to be aware of probably rounding of the p-value AND how the comparison works.
Thank you very much ballardw. I completely forgot about the call symputx statements (picking up SAS first the first time in a few years). I was thinking I could load the data sets within the macro and being able to pull directly from them without needing to do anything else. Thank you very much!
DATA steps contain a tool to make this easy. You don't need CALL SYMPUT at all. You don't need a macro definition. To execute one set of code or another after your DATA step ends, here is the way to do it:
data _null_;
set stats;
if PROB > 0.25 then call execute(
'proc glm data=stability;
model RP=time;
run;');
else call execute(
'proc glm data=stability;
class batch;
model RP=batch time batch*time;
run;');
run;
If STATS contains multiple observations, you may need to add to the logic to make sure you execute this logic just once.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.