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.
It is 1 data step? The first one simply creates the data to work with.
@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;
is it not possible to do within one data step?
It is 1 data step? The first one simply creates the data to work with.
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.