BookmarkSubscribeRSS Feed
0 Likes

Often in reporting we do the same thing to several variables - print them, calculate statistics, etc.  For example, in the hypothetical store scenario we might sum the number of shipments, the number of returns, the amount of inventory cost. 

I propose an enhancement to base SAS to allow definition of a named group of variables  that can then be used everywhere and be expanded into that list of variables.  This would make some programs a lot "cleaner" and save time writing programs.

For example

DATA My_DATA(KEEP=Store Store_Variables);

    SET Input_Dataset;

    GROUP Store_Variables Shipments, Returns, Inventory_Cost, Sales;

PROC SORT DATA=My_DATA; By Store;

PROC MEANS DATA=My_DATA; By Store;

      VAR Store_Variables;

      OUTPUT OUT=Store_Summary;
                     SUM=Store_Variables;

Store_Summary should then contain Store Shipments Returns Inventory_Cost Sales  which are sums of the input data variables of those names.

For discussion:  should the named group be carried forward if all of its constituent variables exist in the summary? Should PROCs instead support naming groups for output variables?  Should GROUP be a dataset option instead of a DATA step statement?

8 Comments
paulkaefer
Lapis Lazuli | Level 10

We already do that with macro variables:

 

%let myVars = a b c d e;
TimH
Quartz | Level 8

I know it can be done with macros, but macros don't get saved - you have to make sure you code them in every program where you want to use a group.  Having the group be stored in the library eliminates that requirement and gives some 'permanence" the group definition.

Tom
Super User
Super User

Note sure there is much value in modifying the SAS syntax to support this.  Perhaps with a totally rebuild dataset structure and language syntax it might be useful.

 

For now use another method. 

 

You could use extended attributes to store the grouping information.

https://blogs.sas.com/content/sasdummy/2013/10/17/extended-attributes-sas-94/

 

Or just make a view that only has the group of variables.

proc sql ;
create view Store_Variables as
  select Store ,Shipments ,Returns ,Inventory_Cost ,Sales
  from Input_Dataset 
  order by Store
;
quit;
Reeza
Super User

What's wrong with just creating  a list of variables into a macro variable?

 

For example

 

%let myGroupVars = Shipments Returns Inventory_Cost Sales;

DATA My_DATA(KEEP=Store &myGroupVars.);

    SET Input_Dataset;

run;

PROC SORT DATA=My_DATA; 
By Store;
Run;

PROC MEANS DATA=My_DATA; By Store;

      VAR &myGroupVars.;

      OUTPUT OUT=Store_Summary;
                     SUM=Store_Variables / autoname;
TimH
Quartz | Level 8

As I thought I stated before - the macro method works but has to be done in every program.  Defining the group in the data library adds "permanence" to the definition and makes it part of the metadata rather than part of the application program

Reeza
Super User

Sounds like a role functionality in the data model to some degree that SAS could access. This is pretty commonly done when building data marts that then leave the data available for analysis. But so much of this requires conditional logic so the amount of exceptions wouldn't make it that effective, IMO.

 

You could probably add it as an extended attribute, but not sure how accessible that would be.

paulkaefer
Lapis Lazuli | Level 10

You could have it defined as a macro variable in autoexec / a common macro. Then include it in programs that would use it: %init_group_vars; and the %init_group_vars macro function declares the required group(s).

ballardw
Super User

If your "groups" are semi-persistent, as in used lots of times for the same group then you could also use a common stem name such as Grp1_

So have variable names:

 

 Grp1_Shipments, Grp1_Returns, Grp1_Inventory_Cost, Grp1_Sales

 

Then use lists like Grp1_:

Careful naming would allow use of an existing syntax feature.

Extensible to multiple levels as well. With multiple "groups" created such as above Grp2_ and so on. All of the  variables could be referenced with Grp: