BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Jolly1289
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26
%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.

--
Paige Miller
Jolly1289
Calcite | Level 5

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.

ballardw
Super User

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.

 

Jolly1289
Calcite | Level 5

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!

Astounding
PROC Star

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 802 views
  • 0 likes
  • 4 in conversation