BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lerdem
Quartz | Level 8

I have some tables as below:

       Var1     Var2    Var3       Var4       Var5     

      AA       19,176   48.4%    20,001    49.6%

      AS        9,717    24.5%    9,609      23.8%

      BA        8,694    21.9%    8,891       22%

      BB        2,032    5,1%      1,849      4.6%

I need to add one new column at the top. And i want to sum each row. So the first new column should be like this:

       Var1       Var2     Var3       Var4       Var5    

     Sum        39,619   100.0%   40,350    100.0%

      AA         19,176   48.4%    20,001    49.6%

      AS          9,717    24.5%    9,609      23.8%

      BA          8,694    21.9%    8,891       22%

      BB          2,032    5,1%      1,849      4.6%

1 ACCEPTED SOLUTION

Accepted Solutions
Steelers_In_DC
Barite | Level 11

Here's a solution for you:

data have;

infile cards dsd dlm= '*';

informat var1 $2. var2 comma. var3 percent. var4 comma. var5 percent.;

input Var1 Var2 Var3 Var4 Var5;

cards;

AA*19,176*48.4%*20,001*49.6%

AS*9,717*24.5%*9,609*23.8%

BA*8,694*21.9%*8,891*22%

BB*2,032*5,1%*1,849*4.6%

;

run;

proc transpose data=have out=tran;id var1;var var2 var3 var4 var5; 

data prep;

set tran;

sum = sum(aa,as,ba,bb);

run;

proc transpose data=prep out=want (rename=(_NAME_ = var1));id _name_;var sum aa as ba bb;

View solution in original post

3 REPLIES 3
Steelers_In_DC
Barite | Level 11

Here's a solution for you:

data have;

infile cards dsd dlm= '*';

informat var1 $2. var2 comma. var3 percent. var4 comma. var5 percent.;

input Var1 Var2 Var3 Var4 Var5;

cards;

AA*19,176*48.4%*20,001*49.6%

AS*9,717*24.5%*9,609*23.8%

BA*8,694*21.9%*8,891*22%

BB*2,032*5,1%*1,849*4.6%

;

run;

proc transpose data=have out=tran;id var1;var var2 var3 var4 var5; 

data prep;

set tran;

sum = sum(aa,as,ba,bb);

run;

proc transpose data=prep out=want (rename=(_NAME_ = var1));id _name_;var sum aa as ba bb;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

proc sql;

     create table WANT as

     select     "Sum" as VAR1,

                    sum(VAR2) as VAR2,

                    sum((VAR3) as VAR3,

                    sum((VAR4) as VAR4,

                    sum((VAR5) as VAR5

     from        HAVE

     union all

     select     *

     from       HAVE;

quit;

lerdem
Quartz | Level 8

Thank you both works

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 1005 views
  • 0 likes
  • 3 in conversation