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

Hello,

I would like to have a value repeated until the next line is zero and if the next line is greater than zero then value be added to the previous value . here is a sample of my dataset and my desired output.

My data looks like this

iddate gwil
120140
120150
12016100
120170
120180
1201920
120200
120210
220150
2201680
220170
220180
2201915
220200

 

My desired output will look like this

iddate gwillgwill1
1201400
1201500
12016100100
120170100
120180100
1201920120
120200120
120210120
2201500
220168080
22017080
22018080
220191595
22020095

 

I used the following code and was able to repeat until the next value is zero but can't figure out how to add the next value for the rest of the obseravations.

data want;
set have;
by id notsorted;
retain gwlim;
if first.id or gwill > 0 then gwlim = gwill;
rename gwlim = gwill1;
run;

Any help is greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

For each ID, you apparently want GWILL1 to contain the running sum of all values of GWILL:

 

data want;
  set have;
  by id notsorted;
  gwill1+gwill;
  if first.id then gwill1=gwill;
run;

Untested, in the absence of a working DATA step with the sample data.

 

And slightly more compact, but a bit more obscure:

 

data want;
  set have;
  by id notsorted;
  gwill2+ifn(first.id,gwill-gwill2,gwill);
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
mkeintz
PROC Star

For each ID, you apparently want GWILL1 to contain the running sum of all values of GWILL:

 

data want;
  set have;
  by id notsorted;
  gwill1+gwill;
  if first.id then gwill1=gwill;
run;

Untested, in the absence of a working DATA step with the sample data.

 

And slightly more compact, but a bit more obscure:

 

data want;
  set have;
  by id notsorted;
  gwill2+ifn(first.id,gwill-gwill2,gwill);
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
AmirSari
Quartz | Level 8
Thank you! This is exactly what I wanted.
japelin
Rhodochrosite | Level 12

try this code

 

proc sort data=have;
  by id date;
run;

data want;
  set have;
  by id date;
  retain gwill1;
  if first.id then gwill1=0;
  gwill1=gwill+gwill1;/* If you think of "add if greater equal to zero" as "always add," then you don't need the if statement. */
run;

 

 

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 1075 views
  • 1 like
  • 3 in conversation