BookmarkSubscribeRSS Feed
0 Likes

One of the convenient SAS features I enjoy is the #byval feature in procedures such as proc gplot, proc report, etc. without resorting to macros. the obvious limitation is that it only works for one proc at a time, so if I want to run a series of procedures one by-group at a time (e.g., proc odstext followed by proc report for the first by-group, repeat for the second, etc.), I cannot do that without resorting to macros.

 

Would it make sense to create a new procedure such as proc encapsulate that would enable the user to have more control over procedures? An example of a rough sketch would be (example modified to make it more clear that all datasets involve require common primary level masterby variables). 

 

proc encapsulate;
   masterby MASTERVAR1;
   proc odstext data=input_data "This is the report section for #masterbyval1"; quit;

 

   within MASTERVAR1 do over MASTERVAR2;

       Title "Text for #masterbyvar(MASTERVAR1) #masterbyvar(MASTERVAR2)";
        proc report data=input_data2;
        run;

    endcomp;
run;

5 Comments
Reeza
Super User

SAS Studio Tasks do offer a way to do this, but in general, macros end up being the solution so far...good suggestion.

Tom
Super User
Super User

Not sure how SAS could implement it they way you have described it.  Where would all of the output be spooled before until the last procedure is called?

 

But it might be possible to add options to the REPLAY command of PROC DOCUMENT to have it replay the results ordered by the values of the BY groups eliminating the need to program that logic separately.

mkeintz
PROC Star

I think this would have to require the user to restrict the encapsulated code to apply to one dataset, and possibly its derivatives (like estimated dependent vars from a PROC REG ).     In such a case SAS could stop processing data at the end of the first by-level (possibly saving data in a utility file), then do all the procs, then restart (emulating a FIRSTOBS= jump) at the beginning of the next by-level, etc.  This would avoid re-reading the first by-levels multiple times.

 

But if the BYVAR levels for dataset A were 1,3,5,7 and for B were 2,4,6,8, it becomes a trickier process if both A and B were original datasets in the encapsulated code.  Sure it could be done, but likely would require a great deal more overhead.

 

 

dardesta
Fluorite | Level 6

I was thinking about such a hypothetical procedure needing to having a data= option to rely solely on one dataset. Would the overhead be less if the procedure had the option to pass on a dataset containing all distinct values of the masterby variables? Meaning, the procedure would require either the data= option or a datavalues= option.

Reeza
Super User

This is the SAS Studio approach I was thinking of when this came up. Seems like it could adapt the code to be a macro somehow and then run each one rather than store interim results. 

https://communities.sas.com/t5/SAS-Communities-Library/SAS-Studio-Custom-Task-Tuesday-How-to-Create-...