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

Hi

it may or may not sound easy and not even sure u can understand what I am trying to do.

 

My question is that "without using proc"

and if my data table is shown like this

 

Var1 Var2

A        3

A        20

B        1

C        4

C        5

 

is it possible to sum column values with same var1 value,

which means 3+20 because they got Same Var1 value A

and 4+5 for C,

that makes new variable Var3 on the table.

 

if I just used

Var3 = Var3+Var2;

retain Var3 0;

it would just give the whole sum of numbers no matter what value they've got on Var1.

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

It is 1 data step? The first one simply creates the data to work with.

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

@AlohaHi Hi and welcome to the SAS Community 🙂

 

You need to control your By Groups and do something like this

 

data have;
input Var1 $ Var2;
datalines;
A 3
A 20
B 1
C 4
C 5
;

data want;
    set have;
    by Var1;
    if first.Var1 then Var3=0;
    Var3+Var2;
run;
AlohaHi
Calcite | Level 5

is it not possible to do within one data step? 

PeterClemmensen
Tourmaline | Level 20

It is 1 data step? The first one simply creates the data to work with.

ed_sas_member
Meteorite | Level 14

Hi @AlohaHi 

 

If you don't want to use a PROC, you can use the following code, assuming the dataset is sorted by Var1

 

data have;
	input Var1 $ Var2;
	cards;
A 3
A 20
B 1
C 4
C 5
;
run; 

data want;
	set have;
	by Var1;
	
	if first.var1 then Var3 = 0;
	Var3 + Var2;
	
	if last.var1 then output;
	
	drop var2;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 909 views
  • 0 likes
  • 3 in conversation