BookmarkSubscribeRSS Feed
Hopeful
Fluorite | Level 6

Hi,

 

I'm running simulations with proc bglimm to estimate coverage, bias, power of hypothesis tests, etc.

 

My proc bglimm syntax includes a by statement to run the model on each simulated dataset separately. Imagine something like:

 

proc bglimm data = sims outpost = sims_post;
by simulation;
class ID;
model y = / dist = normal;
random intercept / subject = ID;
run;

I would like to use the autocall macros for postprocessing, such as %postsum(), on the posterior samples: 

 

https://documentation.sas.com/doc/en/pgmsascdc/v_032/statug/statug_bglimm_details23.htm

 

How can I run these postprocessing macros by group (i.e. simulation) and store the results in a table?

 

Basically, the syntax below with a "by" statement would be great.

%postsum(data = sims_post, var = intercept)

 

 

Any suggestions?

Thanks!

 

2 REPLIES 2
webart999ARM
Quartz | Level 8

You can use the %DO loop in SAS to iterate over the different groups defined by the BY statement in your PROC BGLIMM code. For each group, you can call the %postsum macro and store the results in a table. Here's an example of what that might look like:

 

%macro postsum_by_group(data=, var=);
  /* Initialize an empty table to store results */
  data results;
  length simulation $32;
  length mean median stderr $32;
  output;

  /* Loop over each group defined by the BY statement */
  %do i = 1 %to &data..n_classes;
    /* Call the %postsum macro for the current group */
    %postsum(data=&data., var=&var., by=simulation, classid=&i.)

    /* Store the results in the table */
    simulation = "Simulation &i";
    mean = mean;
    median = median;
    stderr = stderr;
    output;
  end;
  run;
%mend;

/* Use the macro to compute summaries by group */
%postsum_by_group(data=sims_post, var=intercept);

This code defines a %postsum_by_group macro that takes in the name of the data set containing the posterior samples and the variable of interest, and computes summaries for each group defined by the BY statement. You can then use this macro to compute summaries for the intercept variable in your example.

Hopeful
Fluorite | Level 6

Thank you webart999ARM,

 

I had some trouble running the macro code you provided. But, it was very helpful as a starting point and structure to create my own macro version.

 

The macro I made below works for me and allows me to use some of the other postprocessing macros. However, it currently does not allow for options within the macros to be changed without manually changing within the macro code. For example, the ALPHA= option within %postint() would need to be changed directly within the macro. I'm sure it could be more efficient, so please feel free to show me more efficient code.

 

I'm happy to keep the discussion open to hear other ideas.

 

Thanks!

Bryan

 

/* Simulate example posterior results from proc bglimm */
data sims_post;
do simulation = 1 to 10;
	do iteration = 1 to 1000;
		intercept = rand("normal", 4, 1);
		random1_vc_1 = rand("normal", 0.10, 0.02);
		output;
	end;
end;



/* Macro for Bayesian posterior processing with autocall macro.
mac= the name of the autocall macro we want to run (only 1 allowed)
data= name of the dataset that has the posterior data (only 1 allowed)
sim_var= name of the simulation variable (only 1 allowed)
var= name of the parameters within the dataset that 
we want to run the autocall macro on (multiple allowed)*/ %macro post_by_sim(mac=, data=, sim_var=, var=); /* Delete previous results table if it exists */ proc datasets nolist; delete &mac._results; run; /* Identify number of simulations */ proc sql noprint; select count(distinct &sim_var.) into :nsim from &data.; quit; /* Loop over each simulation */ %do i = 1 %to &nsim.; proc sql noprint; create table subset as select simulation, %sysfunc(tranwrd(%sysfunc(compbl(&var.)), %bquote( ), %bquote(,))) from &data. where &sim_var. = &i.; quit; /* Run selected autocall macro to generate summary stats */ %if %lowcase(&mac.) = %bquote(postsum) %then %do; %postsum(data = subset, var = &var., print = no, out = simresults); %end; %else %if %lowcase(&mac.) = %bquote(postint) %then %do; %postint(data = subset, var = &var., print = no, out = simresults); %end; %else %if %lowcase(&mac.) = %bquote(sumint) %then %do; %sumint(data = subset, var = &var., print = no, out = simresults); %end; %else %if %lowcase(&mac.) = %bquote(ess) %then %do; %ess(data = subset, var = &var., print = no, out = simresults); %end; %else %if %lowcase(&mac.) = %bquote(geweke) %then %do; %geweke(data = subset, var = &var., print = no, out = simresults); %end; /* Store the summary stats in a table */ proc append base = &mac._results data = simresults; run; %end; proc sort data = &mac._results; by parameter; run; proc print data = &mac._results; by parameter; run; %mend; /* Use the macro to compute posterior summaries by simulation */ %post_by_sim(mac = postsum, data = sims_post, sim_var = simulation, var = intercept random1_vc_1); %post_by_sim(mac = postint, data = sims_post, sim_var = simulation, var = intercept random1_vc_1); %post_by_sim(mac = sumint, data = sims_post, sim_var = simulation, var = intercept random1_vc_1); %post_by_sim(mac = ess, data = sims_post, sim_var = simulation, var = intercept random1_vc_1); %post_by_sim(mac = geweke, data = sims_post, sim_var = simulation, var = intercept random1_vc_1);

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 400 views
  • 0 likes
  • 2 in conversation