Solved
Contributor
Posts: 55

# SAS 9.2: Simple Total from a dataset

Hi,

I need "Total" text to be displayed in the last row along with the total number. I have tried with proc report and couldn't achieve. Please find the country.sas program and help me in achieving the same.

Current Output:

```ID   COUNTRY                           COUNT
A01  FINLAND                                  0
A02  FRANCE                                  0
A03  GERMANY                             11
A04  ITALY                                       0
A05  UNITED KINGDOM                  34
A06  UNITED STATES                     23
=========
68
=========

```

Desired Output:

```ID   COUNTRY                           COUNT
A01  FINLAND                                  0
A02  FRANCE                                  0
A03  GERMANY                             11
A04  ITALY                                       0
A05  UNITED KINGDOM                  34
A06  UNITED STATES                     23
=========
Total                                               68
=========

```

Accepted Solutions
Solution
‎02-15-2013 06:01 AM
Super Contributor
Posts: 276

## Re: SAS 9.2: Simple Total from a dataset

Hi..

Try this Proc Report Code.

Tested and working perfectly..

Data Test;

input ID \$5.  COUNTRY \$20. COUNT  ;

cards;

A01   FINLAND                    0

A02   FRANCE                    0

A03   GERMANY                11

A04   ITALY                         0

A05   UNITED KINGDOM      .

A06   UNITED STATES       23

;

run;

Proc Report data=test out=t;

columns ID Country Count;

define ID/Display ;

Define Country/Display;

Rbreak after/summarize Dul Dol;

Compute  ID;

If _BREAK_="_RBREAK_" then

ID="Total";

endcomp;

run;

Thanks &Regards.

Sanjeev.K

All Replies
Frequent Contributor
Posts: 97

## Re: SAS 9.2: Simple Total from a dataset

hi ,

use compute block.and check

COMPUTE AFTER COUNTRY;

COUNTRY=TRIM(COUNTRY)||'TOTAL"

ENDCOMP;

Regards,

ALLU

Contributor
Posts: 55

## Re: SAS 9.2: Simple Total from a dataset

Hi,

I tried using compute block but its not working.

```proc report data = country nowd;
columns id country count;
define id / display;
define country / order;
rbreak after  /  summarize style=[fontweight=bold color=black] dol dul ;
COMPUTE AFTER country;
country=TRIM(country)||'TOTAL';
ENDCOMP;
run;

```
SAS Super FREQ
Posts: 9,365

## Re: SAS 9.2: Simple Total from a dataset

Hi:

It looks like ID is character with a length of 3??? The problem is that when you use your compute block technique, you are limited to the length of the variable in the column that you want to assign the Total string to. For example, the SEX variable in SASHELP.CLASS is a character variable with a length of 1. See the problem with your code approach shown in the screenshot. And one possible solution, shown in the other screenshot.

cynthia

Contributor
Posts: 55

## Re: SAS 9.2: Simple Total from a dataset

Hi, Is there any other approach other than proc report? I want only Total and text as "Total" at the last row. Please help me.

Super Contributor
Posts: 276

## Re: SAS 9.2: Simple Total from a dataset

Hi..

Your requirement can be easily achievable with Proc report as    suggested.

As you mentioned in above post ,i can provide the solution  "Other then Proc Report".but i am not sure this is the  right way to achieve your requirement.

Here is the Solution for your requirement..

Data Test;

input ID \$4.  COUNTRY \$20. COUNT  ;

cards;

A01  FINLAND                    0

A02  FRANCE                    0

A03  GERMANY                11

A04  ITALY                         0

A05  UNITED KINGDOM     34

A06  UNITED STATES        23

;

run;

Data t(Drop=Count1);

Format ID \$6.;

set test  end=last ;output;

retain Count1 0;

Count1=sum(Count1,Count);

if last then do;

ID="Total";

Count=Count1;

COUNTRY=' ';output;

end;

run;

Proc print;

run;

Thanks & Regards.

Sanjeev.K

Solution
‎02-15-2013 06:01 AM
Super Contributor
Posts: 276

## Re: SAS 9.2: Simple Total from a dataset

Hi..

Try this Proc Report Code.

Tested and working perfectly..

Data Test;

input ID \$5.  COUNTRY \$20. COUNT  ;

cards;

A01   FINLAND                    0

A02   FRANCE                    0

A03   GERMANY                11

A04   ITALY                         0

A05   UNITED KINGDOM      .

A06   UNITED STATES       23

;

run;

Proc Report data=test out=t;

columns ID Country Count;

define ID/Display ;

Define Country/Display;

Rbreak after/summarize Dul Dol;

Compute  ID;

If _BREAK_="_RBREAK_" then

ID="Total";

endcomp;

run;

Thanks &Regards.

Sanjeev.K

Contributor
Posts: 55

## Re: SAS 9.2: Simple Total from a dataset

Hi Sanjeev,

Thanks a lot for code. It's working as per the requirement.

🔒 This topic is solved and locked.