BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Kamal5522
Obsidian | Level 7
Hi i have below data set
Company Profit
OYO 45
PAYTM 65
PHONPE 50
Total 160

in the above dataset I want add new varibale Total where i want total value 160. I do not want to create variable by giving hard value as

Total=160
I want to create in such a way that if total value changes then variable value also changes accordingly
Can someone advise please
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Do you want the maximum value in the new column, or the value of a specific observation?

Maximum:

proc sql;
create table want as
select
  have.*,
  max(profit) as X
from have;
quit;

Specific:

proc sql;
create table want as
select
  have.*,
  (select profit from have where company = 'paytm') as X
from have;
quit;

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

You want a new observation with company = 'Total' and profit = sum of profits?

This is mostly needed for reporting, and the SAS procedures for that can handle it, so there's no need to add an observation.

But just to show you how it's done:

data want;
set have end=eof;
retain sum_profit 0;
output;
sum_profit + profit;
if eof
then do;
  company = 'Total';
  profit = sum_profit;
  output;
end;
drop sum_profit;
run;

Kamal5522
Obsidian | Level 7
Thanks kurt. this code is quite useful
Kamal5522
Obsidian | Level 7
Hi kurt
I also want to know that is it possible to create a variable with the particular observation value. if we change observation value then value of variable chnages. suppose in the below example if I want to create a varibale X that show paytm 65 value
company profit X
oyo 55 65
paytm 65 65
phone 40 65
if change paytm profit to 70 then whole X varibale should show 70.
please advise
Kurt_Bremser
Super User

Do you want the maximum value in the new column, or the value of a specific observation?

Maximum:

proc sql;
create table want as
select
  have.*,
  max(profit) as X
from have;
quit;

Specific:

proc sql;
create table want as
select
  have.*,
  (select profit from have where company = 'paytm') as X
from have;
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1372 views
  • 1 like
  • 2 in conversation