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

I'm lost. 

I know that I can achieve the same with a oldie-but-goldie proc summary followed by a proc transpose or its siblings in cas actions. 

 

But I want to achieve it with the result, findtable and saveresult combinatuion. 

 

I've tried without success 2 different approaches. 

  1. To loop through the bygroup results and save them into work.summary_1, work.summary_2 using the loop counter i
  2. to save to the same work.summary and append the results using datastep.runcode

I don't know how to create a macro variable inside the loop, it gets quoted and secondly it does not recognize the loop counter i.

 

And the datastep.runcode fails IMO because of the fact that it does not allow a set statement for the work directory being the only library that saveresult admits....

 

data casuser.cars;
set sashelp.cars;
run;

cas mySession sessopts=(caslib="casuser"); 
proc cas;
   session mysession;
   simple.mdSummary result=res status=s /             
      inputs={"Horsepower", "Invoice", "Length" },                        
      subSet={ "mean",  "N"},
      sets={{groupBy={"Make"}},                   
         {groupBy={"make", "type"} }},
      table={ name="cars"};
run;
describe res;

print res["ByGroupSet2.ByGroup1.MDSummary"]; 

do i=1 to 2;
s=res[cats("ByGroupSet2.ByGroup",i, ".MDSummary")];
%let savename=%sysfunc(cats('summary_', i));
saveresult s dataout=work.summary;

/* source originTables; */
/*         data risk.all_sum; */
/*         set work.summary risk.all_sum; */
/*         run; */
/* endsource; */
/* dataStep.runCode / code=originTables; */

end;

/* print res["ByGroupSet2.MDSummary", 3:5]; */
/* do i = 1 to res.MDSummary.nrows; */
/* print res["MDSummary"][2, {1 3} ]; */
/* end; */
 
run;
quit;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

To combine the individual result tables you can use the CASL function COMBINE_TABLES.

You can use the SAVERESULTS statement to write to a CAS table, this allows for a dynamic name, this is not possible for a SAS data set.

 

See this example:

cas sugus sessopts=(caslib="casuser");
libname casuser cas caslib="casuser";

data casuser.cars;
  set sashelp.cars;
run;

proc cas;
  action simple.mdSummary result=res status=s /             
    inputs={"Horsepower", "Invoice", "Length" },                        
    subSet={ "mean",  "N"},
    sets={
    	{groupBy={"origin"}} 
      {groupBy={"origin", "type"} }
    },
    table={ name="cars"}
  ;
run;
describe res;
run;
do i=1 to 2;
  s=combine_tables(res, cats("ByGroupSet", i));
  table = cats("summary_a", i);
  saveresult s casout=table caslib="casuser" replace;
end;
run;
quit;

View solution in original post

4 REPLIES 4
Sundaresh1
SAS Super FREQ

Hi @acordes ,

 

The following statements may help.   Actually, consider them as two separate blocks / alternatives.

 

1. You actually solved part of the problem by specifying the table object key (MDSummary) , therefore don't need to use findable in block 1.

 

2. In block 2 ,  you can use findtable (your initial results object) to programmatically identify the table.  This is useful when you are unsure as to what your object really calls.

 

print res["ByGroupSet2.ByGroup1.MDSummary"]; 

resobject=res["ByGroupSet2.ByGroup1.MDSummary"]; 

saveresult resobject dataout=work.abc;


/* Block 2 */
/* If you want to use findable */

resobject2 = findtable(res);

saveresult resobject dataout=work.xyz;

 

 

I wanted to quickly touch upon another aspect - in your do loop (whenever / if you want to use a do loop), the dim(result) statement will identify the upper limit (perhaps you know that already, my apologies).   Just for interest, an alternative approach (perhaps less well-known or used) is provided in this paper.  Essentially the concept is that you create a new column in the result object, which actually is the result of a function you can define (to operate on each line of the result object).   Search for WHERE and COMPUTE operator in the link.  I like it for its conciseness.  However, this may be a little new for those who are used to the established SAS / CASL ways of programming.  

 

 

acordes
Rhodochrosite | Level 12
Thank you. The paper looks promising.
Hopefully this will help me sort out how to loop through the different by group tables and save them to different result tables.
acordes
Rhodochrosite | Level 12

@BrunoMueller within the proc cas block I'm not able to save all different by-groups 

  • or to different summary tables using the savestate line because I do not succeed in creating a different target table name within the do-loop
  • or to use a datastep.runcode appending / setting the different by-group related versions of the the summary table (cas in this case)

 

Furthermore I've noticed that 

saveresult s dataout=work.summary;

and 

saveresult s casout="summary" caslib="risk";

produce different output tables. the cas tables lacks the grouping variables ???

 

BrunoMueller
SAS Super FREQ

To combine the individual result tables you can use the CASL function COMBINE_TABLES.

You can use the SAVERESULTS statement to write to a CAS table, this allows for a dynamic name, this is not possible for a SAS data set.

 

See this example:

cas sugus sessopts=(caslib="casuser");
libname casuser cas caslib="casuser";

data casuser.cars;
  set sashelp.cars;
run;

proc cas;
  action simple.mdSummary result=res status=s /             
    inputs={"Horsepower", "Invoice", "Length" },                        
    subSet={ "mean",  "N"},
    sets={
    	{groupBy={"origin"}} 
      {groupBy={"origin", "type"} }
    },
    table={ name="cars"}
  ;
run;
describe res;
run;
do i=1 to 2;
  s=combine_tables(res, cats("ByGroupSet", i));
  table = cats("summary_a", i);
  saveresult s casout=table caslib="casuser" replace;
end;
run;
quit;

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
  • 4 replies
  • 645 views
  • 4 likes
  • 3 in conversation