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

Hi All,

 

Can you please help me to add up each observation value with its previous var value.

Suppose below is my data,

 

Sub      var1

001       12

002       5

003         .

004       3

005       .

006        6       

output1:

I want a results as below,

sub     var1      var2

001       12        12

002       5          17

003         .         17

004       3           20

005       .            20

006        6          26

 

output2:

I want a results as below,

sub     var1      var2

001       12        12

002       5          17

003         .         .

004       3           20

005       .            .

006        6          26

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Please try

 

data have;
input Sub var1;
cards;
001 12
002 5
003   .
004 3
005 .
006  6 
;
 
data want1(keep=sub var1 var3 rename=(var3=var2)) want2(keep=sub var1 var2);
set have ;
retain var3;
if var1 ne . then var3=sum(var1,var3);
if var1 ne . then var2=var3;
run;
Thanks,
Jag

View solution in original post

5 REPLIES 5
Jagadishkatam
Amethyst | Level 16

Please try

 

data have;
input Sub var1;
cards;
001 12
002 5
003   .
004 3
005 .
006  6 
;
 
data want1(keep=sub var1 var3 rename=(var3=var2)) want2(keep=sub var1 var2);
set have ;
retain var3;
if var1 ne . then var3=sum(var1,var3);
if var1 ne . then var2=var3;
run;
Thanks,
Jag
yabwon
Onyx | Level 15

Just for fun with shorter syntax:

data have;
input Sub var1;
cards;
001 12
002 5
003 .
004 3
005 .
006 6
;
 
data 
  want1(drop = var2 rename=(var3=var2)) 
  want2(drop = var3)
;
  set have ;
  var2 + var1;
  var3 = ifn(var1,var2,var1);
run;

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ramya2
Calcite | Level 5
Thank you so much.
ed_sas_member
Meteorite | Level 14

Hi @Ramya2 

 

Here is another approach:

data want1;
	set have;
	var2 + var1;
run;

data want2;
	set have;
	_var + var1;
	if not missing (var1) then var2 = _var;
	drop _var;
run;

Best,

sustagens
Pyrite | Level 9

Try

data output1(drop=var2 rename=(cum_var1=var2)) output2(drop=cum_var1);
  set have;
  cum_var1+var1;
  if var1 eq . then var2=.;
  else var2=cum_var1;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 473 views
  • 1 like
  • 5 in conversation