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

Dear Fellow SAS Procedure Users,

 

I have below proc report code I would like to produce the total for each row and append it to the "All Combined" column. For some reason I got blanks for the total. I would like you to advise where my code is wrong and how to fix it. Apologize if this is obvious.

Thanks in advance,

Beatrice

 

BeatriceWang_0-1663706245272.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Here:

 

data ds1;
    set ds1;
	by var1 notsorted;
	label = compress(label, '|');
	if first.var1 then idx = 0;
	idx + 1;  
	length stat $32;
	if not missing(sum) 
    then stat = strip(put(sum, 12.0)); 

run;

 

You have defined STAT as $32 which is character. Than later you try to assign it a character value from a numeric variable named SUM. Just use the numeric variable named SUM.

--
Paige Miller

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

No data to test this on, so this is UNTESTED CODE

 

From now on, please provide code as text rather than screen capture, and by pasting it into the box that appears when you click on the "little running man" icon, like this:

 

proc report data=ds1;
    column idx label (var1,stat) stat=sumstat;
/* Your DEFINE statements go here, plus another DEFINE statement */
/* Do not include DEFINE statement for N */
    define sumstat/sum f=comma18.0 "All Combined";
run;
--
Paige Miller
BeatriceWang
Obsidian | Level 7

Thank you Miller for your suggestion.

I tried but I got the following error:

BeatriceWang_0-1663712305151.png

 

Please note stat itself is a variable in my input dataset and it is the sum I got from proc means.

Any further suggestion?

 

Reeza
Super User
Output from proc means should be numeric. Show your work please. As previously mentioned, not as images.
BeatriceWang
Obsidian | Level 7

Thank you Reeza for your reply.

Here is my original code in text format. Please kindly further suggest.

 

ods results=off;
filename fout '/C/test.xls';
ods excel file=fout options(embedded_titles='yes') style=Excel;
ods excel options(sheet_name='Table9' absolute_column_width='21' flow='tables' frozen_headers='3');
ods listing close;
proc report data=ds1 split='~';
column idx label (var1, stat) n;
define idx / noprint group;
define label / '' group left;
define var1 / '' across nozero order=internal format=anagrpnfmt.;
define stat / '' center;
define n/computed f=comma18.0 "ALL Combined";
compute n;
n=sum(stat);
endcomp;

run;

ods excel close;
filename fout clear;
title;
ods results;

PaigeMiller
Diamond | Level 26

@BeatriceWang wrote:

Thank you Miller for your suggestion.

I tried but I got the following error:

BeatriceWang_0-1663712305151.png

 

Please note stat itself is a variable in my input dataset and it is the sum I got from proc means.

Any further suggestion?

 


STAT is not numeric. You cannot create totals from variables that are not numeric. Your claim that is it numeric is not correct. Show us the ENTIRE LOG from before PROC REPORT of the part of your code that creates the data set DS1.

 

Please STOP showing us screen captures of text. Text, whether code or log, MUST be presented as text, using the "little running man" icon or the </> icon (to the left of the "little running man"), respectively.

--
Paige Miller
BeatriceWang
Obsidian | Level 7
Hi Miller,
Below is my sas code with some changes from the suggestions. The total is still blank.
Yes. My code defined stat to character type.
But I am not sure how to fix it. Can you please suggest a solution?

proc means data=ds0 sum stackodsoutput; class var1; var tot_phys_ov_cnt tot_ov_pts tot_hv_pts tot_all_phys_cnt tot_all_pts_cnt; ods output summary=ds1(drop=_:); run; data colfmt; set ds1; by var1 notsorted; keep fmtname start label; fmtname = 'anagrpnfmt'; if first.var1 then do; start = var1; label = cats(put(var1, anagrpnfmt.),'~(N=', put(nobs, comma5.0), ')'); output; end; run; proc format cntlin=colfmt; run; data ds1; set ds1; by var1 notsorted; label = compress(label, '|'); if first.var1 then idx = 0; idx + 1; length stat $32; if not missing(sum) then stat = strip(put(sum, 12.0)); run; ods results=off; filename fout '/c/test.xls'; ods excel file=fout options(embedded_titles='yes') style=Excel; ods excel options(sheet_name='Table9' absolute_column_width='21' flow='tables' frozen_headers='3'); title j=left h=13pt 'test'; ods listing close; proc report data=ds1 split='~'; column idx label (var1, stat) n; define idx / noprint group; define label / '' group left; define var1 / '' across nozero order=internal format=anagrpnfmt.; define stat / '' center; define n/computed f=comma18.0 "ALL Combined"; compute n; n=sum(stat); endcomp; run; ods excel close; filename fout clear; title; ods results;
PaigeMiller
Diamond | Level 26

Here:

 

data ds1;
    set ds1;
	by var1 notsorted;
	label = compress(label, '|');
	if first.var1 then idx = 0;
	idx + 1;  
	length stat $32;
	if not missing(sum) 
    then stat = strip(put(sum, 12.0)); 

run;

 

You have defined STAT as $32 which is character. Than later you try to assign it a character value from a numeric variable named SUM. Just use the numeric variable named SUM.

--
Paige Miller
BeatriceWang
Obsidian | Level 7

Thanks Miller.

 

It works now.

 

You are awesome! 😊

FM_MF
Calcite | Level 5

Agree with @PaigeMiller.

I supply some additional information about your code.

According to your think, you don't need to add a new computed column .

proc report data=ds1 split='~';
	column idx label (var1, stat) stat=n;
	define idx / noprint group;
	define label / '' group left;
	define var1 / '' across nozero order=internal format=anagrpnfmt.;
	define stat / '' center;
	define n / '' sum f=comma18.0 "ALL Combined";
run;

P.S. The error you got is that you can't use "sum()", you should key "n=stat.sum".

 

Hope this helps.

 

Thanks,

Fred

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 9 replies
  • 1049 views
  • 1 like
  • 4 in conversation