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

Hello all!

I have a brainteaser here…

 

I have sorted data where I mark an indicator by Z or X, depending on some condition.

From the sample data, I need to count the occurrences of X within the first field. If X occurs more than one time continuously, I need to count it as 1…

Any suggestions?

 

Thank you in advance!

I am on SAS 9.2 using DI 4.21

 

sample:

0000178610001120Z 
0000178610002160Z 
0000178610003110X1
0000178610004190X1
0000178610005110X1
0000178610006140Z 
0000178690001120Z 
0000178690002140Z 
0000178690003110X2
0000178690004190X2
0000178690005110X2
0000178690006140Z 
0000178690007110X3
0000178690008190X3
0000178690009110X3
0000178690010190X3
0000178690011110X3
0000178690012140Z 
0000178690013110X4
0000178690014140Z 
0000178690015110X5
0000178690016140Z 
0000178690017110X6
0000178690018140Z 
0000178690019180Z 
0000178690020200X7
0000178770001120Z 
0000178770002150Z 
0000178770003140Z 
0000178770004110X1
0000178770005140Z 
0000178770006110X2
0000178770007140Z 
0000178770008110X3
0000178770009140Z 
0000178770010110X4
0000178780001120Z 
0000178780002140Z 
0000178780003110X1
0000178780005110X1
0000178780007110X1
0000178780008140Z 
0000178780010180Z 
1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please post test data in the form of a datastep, its not fun for me to have to type that in!

data have;
  infile datalines dlm=" " missover;
  input id $ num_id $ val type $;
datalines;
000017861 0001 120 Z    
000017861 0002 160 Z    
000017861 0003 110 X
000017861 0004 190 X
000017861 0005 110 X
000017861 0006 140 Z    
000017869 0001 120 Z    
000017869 0002 140 Z    
000017869 0003 110 X
000017869 0004 190 X
000017869 0005 110 X
000017869 0006 140 Z    
000017869 0007 110 X
000017869 0008 190 X
000017869 0009 110 X
000017869 0010 190 X
000017869 0011 110 X
;
run;

data want;
  set have;
  by id type notsorted;
  retain total curr;
  if first.id then total=0;
  if first.type and type="X" then do;
    total=total+1;
    curr=total;
  end;
  if first.type and type ne "X" then curr=.;
run;

Basically keep a running total within id, and set a retained value based on type on or off.

View solution in original post

6 REPLIES 6
Shmuel
Garnet | Level 18

Why is '1' in 

000017877	0004	110	X	1

I can't figure the rules when to restart counting.

 

For the first part, up to restarting the count 

assuming variables are v1 v2 v3 vX counter you can

data test;
 set have;
       retain phase 0 counter;

      if vx = 'X' and phase=0 then do;
         phase=1;
         counter +1;
     end;
     if vx ne 'X' then phase = 0;
run;

can do:

 

NikosStratis
Obsidian | Level 7

Thank you for you quick response. The 1 is not part of the data. I put it there to show the result that I need.

NikosStratis
Obsidian | Level 7
Figured it out!
Thanks a lot!!!
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please post test data in the form of a datastep, its not fun for me to have to type that in!

data have;
  infile datalines dlm=" " missover;
  input id $ num_id $ val type $;
datalines;
000017861 0001 120 Z    
000017861 0002 160 Z    
000017861 0003 110 X
000017861 0004 190 X
000017861 0005 110 X
000017861 0006 140 Z    
000017869 0001 120 Z    
000017869 0002 140 Z    
000017869 0003 110 X
000017869 0004 190 X
000017869 0005 110 X
000017869 0006 140 Z    
000017869 0007 110 X
000017869 0008 190 X
000017869 0009 110 X
000017869 0010 190 X
000017869 0011 110 X
;
run;

data want;
  set have;
  by id type notsorted;
  retain total curr;
  if first.id then total=0;
  if first.type and type="X" then do;
    total=total+1;
    curr=total;
  end;
  if first.type and type ne "X" then curr=.;
run;

Basically keep a running total within id, and set a retained value based on type on or off.

NikosStratis
Obsidian | Level 7

Thank you for your quick response! Sorry about that! I attached a sample file and tried to copy data in the original message...

I will try the solution!

NikosStratis
Obsidian | Level 7

Runs like a champion!

Thank you!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 979 views
  • 2 likes
  • 3 in conversation