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

Hi team,

 

This is my base, the table is already sorted by ID and flag.

 

 

data temp;
infile DATALINES dsd missover;
input DATE:DDMMYY10. value ID FLAG;
FORMAT DATE DDMMYY10.;
CARDS;
15/05/2020,9360,1,1 
22/05/2020,9360,1,1 
07/08/2020,9880,1,1 
21/08/2020,9100,1,1 
21/08/2020,9880,1,2 
25/08/2020,9800,1,3 
26/08/2020,9100,1,4 
24/11/2020,9569,2,1 
17/12/2020,9600,2,1 
17/12/2020,9600,2,2 
17/12/2020,9600,2,3 
17/12/2020,9600,2,4 
17/12/2020,9600,2,5 
17/12/2020,9600,2,6 
17/12/2020,9600,2,7 
27/05/2020,9020,3,1 
10/06/2020,9519,3,1 
16/06/2020,9553,3,1 
18/06/2020,9553,3,2 
25/06/2020,9500,3,1 
10/07/2020,9498,3,1 
13/07/2020,9498,3,2 
16/07/2020,9075,3,3 
17/07/2020,9663,3,4 
17/07/2020,9368,3,5 
20/07/2020,9663,3,6 
21/07/2020,9663,3,7 
22/07/2020,9600,3,8 
;
run;

 

 

First i need to SUM value, if the flag is consecutive and need to be consecutive to 4 or more. 

 

And if is possible, i need to count if the flag is consecutive and need to be consecutive to 4 or more.

 

The result that i need:

Sk1_SAS_0-1614970096995.png

 

 

Thanks!!!!

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Amethyst | Level 16

I think this will do:

data temp;
infile DATALINES dsd missover;
input DATE:DDMMYY10. value ID FLAG;
FORMAT DATE DDMMYY10.;
CARDS;
15/05/2020,9360,1,1 
22/05/2020,9360,1,1 
07/08/2020,9880,1,1 
21/08/2020,9100,1,1 
21/08/2020,9880,1,2 
25/08/2020,9800,1,3 
26/08/2020,9100,1,4 
24/11/2020,9569,2,1 
17/12/2020,9600,2,1 
17/12/2020,9600,2,2 
17/12/2020,9600,2,3 
17/12/2020,9600,2,4 
17/12/2020,9600,2,5 
17/12/2020,9600,2,6 
17/12/2020,9600,2,7 
27/05/2020,9020,3,1 
10/06/2020,9519,3,1 
16/06/2020,9553,3,1 
18/06/2020,9553,3,2 
25/06/2020,9500,3,1 
10/07/2020,9498,3,1 
13/07/2020,9498,3,2 
16/07/2020,9075,3,3 
17/07/2020,9663,3,4 
17/07/2020,9368,3,5 
20/07/2020,9663,3,6 
21/07/2020,9663,3,7 
22/07/2020,9600,3,8 
;
run;

data temp;
  set temp;
  if flag=1 then grp+1; /* create groups */ 
run;

data want;
 do _N_ = 1 by 1 until(last.grp);
   set temp;
   by grp;
   sum + value;
   cnt + 1;
 end;

 if cnt < 4 then call missing(sum, cnt);

 do _N_ = 1 to _N_;
  set temp;
  output;
 end;

 call missing(sum, cnt);
run;

proc print;
run;

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



View solution in original post

4 REPLIES 4
mkeintz
PROC Star

You've shown what you want for runs constant flag valules of length 4   (sum of value over the 4 obs).  And you've shown what you want for shorter runs (carry forward the previous sum).

 

Questions:

  1. If flag=1 the only value that can have such runs?
  2. What if a run has a length >4?

Additional question:  Does flag always proceed in ascending order.  I.e. can flag=1 reappear after flag>1?  Oops, I see the answer.

 

 

--------------------------
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

--------------------------
Sk1_SAS
Obsidian | Level 7

i need to count and sum if the FLAG is consecutive to 4 or more, like this:

1 2 3 4 

1 2 3 4 5

and etc...

 

if the flag is consecutive but less than 4, like:

 

1 2 3

1 2

does not need to be done.

 

Yes, flag always proceed in ascending order, and that example can happen.

 

 

yabwon
Amethyst | Level 16

I think this will do:

data temp;
infile DATALINES dsd missover;
input DATE:DDMMYY10. value ID FLAG;
FORMAT DATE DDMMYY10.;
CARDS;
15/05/2020,9360,1,1 
22/05/2020,9360,1,1 
07/08/2020,9880,1,1 
21/08/2020,9100,1,1 
21/08/2020,9880,1,2 
25/08/2020,9800,1,3 
26/08/2020,9100,1,4 
24/11/2020,9569,2,1 
17/12/2020,9600,2,1 
17/12/2020,9600,2,2 
17/12/2020,9600,2,3 
17/12/2020,9600,2,4 
17/12/2020,9600,2,5 
17/12/2020,9600,2,6 
17/12/2020,9600,2,7 
27/05/2020,9020,3,1 
10/06/2020,9519,3,1 
16/06/2020,9553,3,1 
18/06/2020,9553,3,2 
25/06/2020,9500,3,1 
10/07/2020,9498,3,1 
13/07/2020,9498,3,2 
16/07/2020,9075,3,3 
17/07/2020,9663,3,4 
17/07/2020,9368,3,5 
20/07/2020,9663,3,6 
21/07/2020,9663,3,7 
22/07/2020,9600,3,8 
;
run;

data temp;
  set temp;
  if flag=1 then grp+1; /* create groups */ 
run;

data want;
 do _N_ = 1 by 1 until(last.grp);
   set temp;
   by grp;
   sum + value;
   cnt + 1;
 end;

 if cnt < 4 then call missing(sum, cnt);

 do _N_ = 1 to _N_;
  set temp;
  output;
 end;

 call missing(sum, cnt);
run;

proc print;
run;

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



Sk1_SAS
Obsidian | Level 7
Very nice.

Thanks.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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