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

Dear all

 

I want to check the numbers for each date. 

 

The values need to be coded as missing when the numbers less than 3. 

Then, the values needs to sum up with the previous values that shown as "New_Numbers".

(ex: 745=738+2+1+2+1+1)

 

 

The raw data are as follows:

 

DateNumbersNew_Numbers
20100101233233
20100102271271
20100103350350
20100104738745
201001052 
201001061 
201001072 
201001081 
201001091 
20100110413413
20100111710710
20100113575575
20100114543545
201001152 
20100116296296
20100117369369
20100118735735
20100119616616
20100120571571
20100122458460
201001231 
201001241 
20100125664664
20100126575575
20100127574574
20100129415415
20100130269269
20100131360362
201002012 
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

And a traditional method

 



data have;
infile cards truncover;
input Date :yymmdd10.	Numbers	;
format date yymmdd10.;
cards;
20100101	233	233
20100102	271	271
20100103	350	350
20100104	738	745
20100105	2	 
20100106	1	 
20100107	2	 
20100108	1	 
20100109	1	 
20100110	413	413
20100111	710	710
20100113	575	575
20100114	543	545
20100115	2	 
20100116	296	296
20100117	369	369
20100118	735	735
20100119	616	616
20100120	571	571
20100122	458	460
20100123	1	 
20100124	1	 
20100125	664	664
20100126	575	575
20100127	574	574
20100129	415	415
20100130	269	269
20100131	360	362
20100201	2	 
;

data temp;
set have;
if numbers>=3 then grp+1;
run;

proc sql;
create table want(drop=grp) as
select *, ifn(numbers>=3,sum(numbers),.) as new_numbers
from temp
group by grp
order by date;
quit;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

Hello @elvics  

 


data have;
infile cards truncover;
input Date :yymmdd10.	Numbers	;
format date yymmdd10.;
cards;
20100101	233	233
20100102	271	271
20100103	350	350
20100104	738	745
20100105	2	 
20100106	1	 
20100107	2	 
20100108	1	 
20100109	1	 
20100110	413	413
20100111	710	710
20100113	575	575
20100114	543	545
20100115	2	 
20100116	296	296
20100117	369	369
20100118	735	735
20100119	616	616
20100120	571	571
20100122	458	460
20100123	1	 
20100124	1	 
20100125	664	664
20100126	575	575
20100127	574	574
20100129	415	415
20100130	269	269
20100131	360	362
20100201	2	 
;

data want ;
if _n_=1 then do;
 if 0 then set have(rename=(numbers=n));
   dcl hash H (dataset:'have(rename=(numbers=n))') ;
   h.definekey  ("date") ;
   h.definedata ("n") ;
   h.definedone () ;
end;
set have;
if numbers>=3 then do;
do d=date+1 by 1 while(h.find(key:d)= 0);
 if n>=3  then leave;
 new_numbers=sum(new_numbers,n);
end;
new_numbers=sum(new_numbers,numbers);
end;
drop n d;
run;
Kurt_Bremser
Super User

Sort in reverse order.

Then, in a data step, retain a sum variable.

Set to zero at the beginning.

When numbers is le 3, sum the values up in your retained variable;

When numbers is gt 3, then:

  • add retained variable to numbers to give new_numbers
  • output
  • set retained variable to zero

Sort back to original order.

 

For code (especially tested code) supply example data in usable form (data step with datalines for easy copy/paste to program window)

novinosrin
Tourmaline | Level 20

And a traditional method

 



data have;
infile cards truncover;
input Date :yymmdd10.	Numbers	;
format date yymmdd10.;
cards;
20100101	233	233
20100102	271	271
20100103	350	350
20100104	738	745
20100105	2	 
20100106	1	 
20100107	2	 
20100108	1	 
20100109	1	 
20100110	413	413
20100111	710	710
20100113	575	575
20100114	543	545
20100115	2	 
20100116	296	296
20100117	369	369
20100118	735	735
20100119	616	616
20100120	571	571
20100122	458	460
20100123	1	 
20100124	1	 
20100125	664	664
20100126	575	575
20100127	574	574
20100129	415	415
20100130	269	269
20100131	360	362
20100201	2	 
;

data temp;
set have;
if numbers>=3 then grp+1;
run;

proc sql;
create table want(drop=grp) as
select *, ifn(numbers>=3,sum(numbers),.) as new_numbers
from temp
group by grp
order by date;
quit;
Jagadishkatam
Amethyst | Level 16

Alternatively, please try

 

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

data want;
set have;
by descending date;
if .<Numbers<=3 then grp=1;
else grp=2;
run;

data want2;
set want;
by grp notsorted;
if first.grp then sum=.;
if grp=1 then sum+numbers;
numbers2=coalesce(numbers+lag(sum),numbers);
if grp=1 then numbers2=.;
drop grp sum;
run;

proc sort data=want2 ;
by date;
run;

 

 

Thanks,
Jag

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 809 views
  • 1 like
  • 4 in conversation