- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Dear community,
I am trying to collapse median, q1 and q3 for an analysis variable to have them displayed in one cell in the final report. It works as long as I use only group variables, as soon as I switch to across variables I get an error:
ERROR: The variable type of 'MSRP.MEDIAN'n is invalid in this context.
So this works:
proc report data=sashelp.cars;
columns Origin Make Type msrp msrp=msrpq1 msrp=msrpq3 stats;
define Origin /" " group;
define Make /" " group;
define Type / " " group;
define msrp / analysis median noprint;
define msrpq1 / analysis q1 noprint;
define msrpq3 / analysis q3 noprint;
define stats / computed;
compute stats / character length=50;
stats = catt(put(msrp.median, dollar7.1-l), " (", put(msrpq1, dollar7.1-l), "-", put(msrpq3, dollar7.1-l), ")");
endcomp;
run;
But this does not:
proc report data=sashelp.cars;
columns Origin Make Type msrp msrp=msrpq1 msrp=msrpq3 stats;
define Origin /" " group;
define Make /" " group;
define Type / " " group;
define msrp / analysis median noprint;
define msrpq1 / analysis q1 noprint;
define msrpq3 / analysis q3 noprint;
define stats / computed;
compute stats / character length=50;
stats = catt(put(msrp.median, dollar7.1-l), " (", put(msrpq1, dollar7.1-l), "-", put(msrpq3, dollar7.1-l), ")");
endcomp;
run;
Ultimately I would like to obtain a table similar to one generated by below code, but with median, q1 and q3 collapsed into one computed variable:
proc report data=sashelp.cars;
columns Origin Make Type, msrp, (n median q1 q3);
define Origin /" " group;
define Make /" " group;
define Type / " " across;
define n / format=2.;
run;
What am I missing? Thanks!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi: Thanks for the vote of confidence @PaigeMiller. This paper https://support.sas.com/resources/papers/proceedings14/SAS388-2014.pdf goes into detail on using ACROSS items. And using a subset of SASHELP.CARS, if I understand what the original poster wants, I'd use THIS approach shown below:
The way to find out what the correct absolute column numbers are is shown in my user group paper.
Cynthia
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@js5 wrote:
But this does not:
proc report data=sashelp.cars; columns Origin Make Type msrp msrp=msrpq1 msrp=msrpq3 stats; define Origin /" " group; define Make /" " group; define Type / " " group; define msrp / analysis median noprint; define msrpq1 / analysis q1 noprint; define msrpq3 / analysis q3 noprint; define stats / computed; compute stats / character length=50; stats = catt(put(msrp.median, dollar7.1-l), " (", put(msrpq1, dollar7.1-l), "-", put(msrpq3, dollar7.1-l), ")"); endcomp; run;
Works for me. What errors are you seeing from this code?
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Apologies, I made a copy-paste error. This is the failing code:
proc report data=sashelp.cars;
columns Origin Make Type, msrp msrp=msrpq1 msrp=msrpq3 stats;
define Origin /" " group;
define Make /" " group;
define Type / " " across;
define msrp / analysis median noprint;
define msrpq1 / analysis q1 noprint;
define msrpq3 / analysis q3 noprint;
define stats / computed;
compute stats / character length=50;
stats = catt(put(msrp.median, dollar7.1-l), " (", put(msrpq1, dollar7.1-l), "-", put(msrpq3, dollar7.1-l), ")");
endcomp;
run;
I.e. with Type as across variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You need a different variable nested under across. I'm not sure why you're using ACROSS if all the variables under are NOPRINT. This fixed the context issue.
proc report data=sashelp.cars /*list showall*/;
columns Origin Make Type, (msrp) msrp=msrp0 msrp=msrpq1 msrp=msrpq3 stats;
define Origin /" " group;
define Make /" " group;
define Type / " " across;
define msrp / n noprint; *<===;
define msrp0 / analysis median noprint;
define msrpq1 / analysis q1 noprint;
define msrpq3 / analysis q3 noprint;
define stats / computed;
compute stats / character length=50;
stats = catt(put(msrp0, dollar7.1-l), " (", put(msrpq1, dollar7.1-l), "-", put(msrpq3, dollar7.1-l), ")");
endcomp;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@js5 wrote:
Dear community,
I am trying to collapse median, q1 and q3 for an analysis variable to have them displayed in one cell in the final report. It works as long as I use only group variables, as soon as I switch to across variables I get an error:
ERROR: The variable type of 'MSRP.MEDIAN'n is invalid in this context.
So this works:
proc report data=sashelp.cars; columns Origin Make Type msrp msrp=msrpq1 msrp=msrpq3 stats; define Origin /" " group; define Make /" " group; define Type / " " group; define msrp / analysis median noprint; define msrpq1 / analysis q1 noprint; define msrpq3 / analysis q3 noprint; define stats / computed; compute stats / character length=50; stats = catt(put(msrp.median, dollar7.1-l), " (", put(msrpq1, dollar7.1-l), "-", put(msrpq3, dollar7.1-l), ")"); endcomp; run;
But this does not:
proc report data=sashelp.cars; columns Origin Make Type msrp msrp=msrpq1 msrp=msrpq3 stats; define Origin /" " group; define Make /" " group; define Type / " " group; define msrp / analysis median noprint; define msrpq1 / analysis q1 noprint; define msrpq3 / analysis q3 noprint; define stats / computed; compute stats / character length=50; stats = catt(put(msrp.median, dollar7.1-l), " (", put(msrpq1, dollar7.1-l), "-", put(msrpq3, dollar7.1-l), ")"); endcomp; run;
Ultimately I would like to obtain a table similar to one generated by below code, but with median, q1 and q3 collapsed into one computed variable:
proc report data=sashelp.cars; columns Origin Make Type, msrp, (n median q1 q3); define Origin /" " group; define Make /" " group; define Type / " " across; define n / format=2.; run;
What am I missing? Thanks!
Please double check your posted code. Your second block of code does not contain any ACROSS variables. If I change the Type to across I do not get that error, though the output is likely not what you want either. So, as with any error: Copy from the log the entire procedure code and any messages and then paste that to the forum in a code box opened with the </> icon to preserve formatting.
Personally I would likely pre-calculate the string using proc summary followed by a data step and then just have the group,across variables and that string value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This would be pretty simple if you wanted a column for the MEDIAN, another column for the Q1 and another column for the Q3.
If you absolutely must have a text string rather than the actual numeric values (why?), then I think you need some help from @Cynthia_sas
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi: Thanks for the vote of confidence @PaigeMiller. This paper https://support.sas.com/resources/papers/proceedings14/SAS388-2014.pdf goes into detail on using ACROSS items. And using a subset of SASHELP.CARS, if I understand what the original poster wants, I'd use THIS approach shown below:
The way to find out what the correct absolute column numbers are is shown in my user group paper.
Cynthia
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You may want to reference this document https://support.sas.com/resources/papers/proceedings15/SAS1642-2015.pdf
Because of the way ACROSS creates columns you have to reference the specific column number for calculations. Which I believe means that you have to test for which column the type is in and then compute with separate columns. I think.
Like I mentioned, likely much easier to make this variable BEFORE Proc Report and then just display it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for the responses! @data_null__'s solution does run, but type is not shown as across variable. The report has 3 columns: Origin, Make and Stats.
@Cynthia_sas's solution does indeed produce the intended result, with the disadvantage of having to refer to values by column numbers instead of variable names. Reading the paper, it appears there is no way around it, correct? In this case I might do what @PaigeMiller said and pre-calculate the concatenated column as the actual data I need to display has considerably more across groups.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi:
You MUST refer to the items under an ACROSS item by the absolute column number in a COMPUTE block. There's no way around this except to "pre-calculate" the values you want with PROC MEANS or PROC TABULATE. Even PROC REPORT would pre-summarize for you but for this purpose, I think PROC MEANS is the way to go:
Cynthia
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes, that is an alternative. The dummy variable way has been handy for me because using a dummy variable at the end of a row is also useful if I need to go back and do any conditional changes based on multiple items in the report row. Since it serves a double purpose, it's the one I generally use.
Cynthia