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

Hello Team,

 

Greetings

 

I am new to SAS development so subject might be wrong. I am looking for assistance for below

 

There is one existing program and below is output of univariate statement.

 

CodeClientNameBalance
2ABC31646302.55
2ABC1852401.43
2ABC51067.47
2ABC626316.68
2ABC3542.8
2ABC1763759.37
2ABC18908152.99
2ABC64965.59
2ABC8460524.72
2ABC18094.11

 

Now we need to add 1 more column as below

 

CodeClientNameBalanceE1 New Column Required
2ABC31646302.5531646302.55/SUM(Balance)
2ABC1852401.431852401.43/SUM(Balance)
2ABC51067.4751067.47/SUM(Balance)
2ABC626316.68626316.68/SUM(Balance)
2ABC3542.83542.8/SUM(Balance)
2ABC1763759.371763759.37/SUM(Balance)
2ABC18908152.9918908152.99/SUM(Balance)
2ABC64965.5964965.59/SUM(Balance)
2ABC8460524.728460524.72/SUM(Balance)
2ABC18094.1118094.11/SUM(Balance)

 

E1 = value in balace column/Sum of all values in balance column

 

Can someone pleasse guide me which operation should I use to achieve above output. Original code is pasted below

 

proc univariate data=strat.master noprint;
var cursettlement;
by wclientcode wgroup;
output out=strat.rep sum=balance nobs=no_accs;
run;

 

Thanks In Advance

1 ACCEPTED SOLUTION

Accepted Solutions
DJongman
Obsidian | Level 7

Did you try to remove the

 

group by Code,ClientName

code?

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Add a step after the univariate:

proc sql;
  create table WANT as
  select  *,
          BALANCE / sum(BALANCE) as NEW_COLUMN
  from    STRAT.REP
  group by CODE,
           CLIENTNAME;
quit;
Ksharp
Super User
It is easy for SQL.




data have;
infile cards expandtabs truncover;
input Code	ClientName $	Balance;
cards;
2	ABC	31646302.55
2	ABC	1852401.43
2	ABC	51067.47
2	ABC	626316.68
2	ABC	3542.8
2	ABC	1763759.37
2	ABC	18908152.99
2	ABC	64965.59
2	ABC	8460524.72
2	ABC	18094.11
;
run;
proc sql;
create table want as
 select *,Balance/sum(Balance) as want
  from have
   group by Code,ClientName;
quit;



yudhishtirb
Calcite | Level 5

Hello All,

 

Thank you for feedback

 

But it is creating E1 as 1 always. It is not summing all values. Please look below sample

data have;
infile cards expandtabs truncover;
input Code	ClientName $	Balance;
cards;
2	ABC	10
2	ABC	20
2	ABC	30
;
run;

Then in output I must have

Code    Client  Balance  E1             E2
2 ABC 10 10/(10+20+30) 60 2 ABC 20 20/(10+20+30) 60 2 ABC 30 30/(10+20+30) 60


Can we do like this ?

Thanks In Advance

 

DJongman
Obsidian | Level 7
data have;
infile cards expandtabs truncover;
input Code	ClientName $	Balance;
cards;
2	ABC	10
2	ABC	20
2	ABC	30
;
run;

proc sql;
create table want as
 select *,Balance/sum(Balance) as E1, sum(Balance) as E2
  from have
   group by Code,ClientName;
quit;

Should do the trick.

 

And if you want to sort the result as in your sample:

 

proc sql;
create table want as
 select *,Balance/sum(Balance) as E1, sum(Balance) as E2
  from have
  
   group by Code,ClientName
   order by Code,ClientName,Balance;
quit;
yudhishtirb
Calcite | Level 5

Hi,

 

Thanks for feedback.

 

Sorry, I posted wrong input. Please consider below input

 

data have;
infile cards expandtabs truncover;
input Code	ClientName $	Balance;
cards;2	ABC	10
2	ABC	20
2	ABC	30
3 PQR 40
4 XYZ 50
;
run

 

Then Output must give

 

Code    Client  Balance  E1                          E2
2 ABC 10 10/(10+20+30+40+50) 150 2 ABC 20 20/(10+20+30+40+50) 150 2 ABC 30 30/(10+20+30+40+50) 150
3 PQR 40 40/(10+20+30+40+50) 150
4 XYZ 50 50/(10+20+30+40+50) 150

 

I cannot group by since SUM is required for whole column.

 

Thanks In Advance

DJongman
Obsidian | Level 7

Did you try to remove the

 

group by Code,ClientName

code?

yudhishtirb
Calcite | Level 5

Hi

 

Thanks, its working 🙂

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!

How to Concatenate Values

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.

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
  • 7 replies
  • 10210 views
  • 2 likes
  • 4 in conversation