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.
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;
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;
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.
@BrunoMueller within the proc cas block I'm not able to save all different by-groups
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 ???
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.