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

Hi guys,

 

I'm trying to find a solution for the following issue. In the table below, I want to count the number of values from each continuous string of values that are higher than 1. 

 

Variable_1Expected_result 
10 
10 
20 
32(String of 2 consecutive values>1)
10 
50 
60 
30 
20 
50 
46(String of 6 consecutive values>1)
10 
21 
10 
30 
22(String of 2 consecutive values>1)
11 
1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Amethyst | Level 16

Maybe like this:

data have;
input x;
cards;
1
1
2
3
1
5
6
3
2
5
4
1
2
1
3
2
1
;
run;


/* format for constructing groups */
proc format;
value gtOne
  1<-High = 1
  other  = 0
  ;
run;

data want;
  format x gtOne.;
  set have;
  by x groupformat notsorted;

  if first.x then i = 0; drop i;

  i + x>1; /* counter for group */

  if last.x and x>1<i /* condition for group */
    then y = i; 
  else y = 0;
run;
proc print data = want;
  format x; /* drop format from x */
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
Reeza
Super User
Is the third column what you expect as output? It would probably help to show at least one use case that had more than one value in a row and the expected output in that scenario.
Vic6
Fluorite | Level 6

The third column is just with observations, to better explain the logic.

What I need to calculate is the second column (Expected_result).

 

yabwon
Amethyst | Level 16

Maybe like this:

data have;
input x;
cards;
1
1
2
3
1
5
6
3
2
5
4
1
2
1
3
2
1
;
run;


/* format for constructing groups */
proc format;
value gtOne
  1<-High = 1
  other  = 0
  ;
run;

data want;
  format x gtOne.;
  set have;
  by x groupformat notsorted;

  if first.x then i = 0; drop i;

  i + x>1; /* counter for group */

  if last.x and x>1<i /* condition for group */
    then y = i; 
  else y = 0;
run;
proc print data = want;
  format x; /* drop format from x */
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



Ksharp
Super User
data have;
input x;
cards;
1
1
2
3
1
5
6
3
2
5
4
1
2
1
3
2
1
;
run;

data temp;
 set have;
 if x>1 then count+1;
  else count=0;
group=(count=0);
run;
data want;
 set temp;
 by group notsorted;
 if not last.group then count=0;
 drop group;
run;

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
  • 1302 views
  • 4 likes
  • 4 in conversation