turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Counting observations separated by missing values

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-15-2012 12:50 AM

Hi folks,

How do I obtain column b?

a | b |

3 | 1 |

65 | 2 |

2 | 3 |

1 | 4 |

4 | 5 |

. | . |

5 | 1 |

. | . |

6 | 1 |

2 | 2 |

1 | 3 |

2 | 4 |

. | . |

1 | 1 |

. | . |

2 | 1 |

5 | 2 |

. | . |

9 | 1 |

63 | 2 |

2 | 3 |

. | . |

. | . |

1 | 1 |

2 | 2 |

23 | 3 |

6 | 4 |

9 | 5 |

5 | 6 |

2 | 7 |

1 | 8 |

Basically I would like b to restart counting every time it sees a missing value. My apologies if this technique is too simple for some of you. Thanks in advance.

Cheers

Accepted Solutions

Solution

09-15-2012
11:03 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-15-2012 11:03 AM

hi ... how about this ...

**data want;**

**set have;**

**b+1;**

**if missing(a) then b = .;**

**run;**

All Replies

Solution

09-15-2012
11:03 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-15-2012 11:03 AM

hi ... how about this ...

**data want;**

**set have;**

**b+1;**

**if missing(a) then b = .;**

**run;**

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-15-2012 08:24 PM

Hi Mike,

It totally worked - can't believe it was this simple. Thanks heaps dude!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-18-2012 03:50 PM

Mike,

Could you explain it to me once again.

I dint understand the question first.

he wants it to start counting from first when it encounters a missing value for b. whay not a?

Also whereever b has a missing value a also has a missing.

What if a doesnt have a missing value...?

When it comes to the answer you posted:

could you explain the code b+1;

also if missing(a) then b=.; (already in the data whereever a is missing b also is missing....i dint understand the logic???

Thanks in advance

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-18-2012 04:10 PM

hi ... the person who posted the question has a data set that has ONLY the variable A ...

**data have;**

**input a;**

**datalines;**

**3**

**65**

**2**

**1**

**4**

**.**

**5**

**.**

**6**

**2**

**1**

**2**

**.**

**1**

**.**

**2**

**5**

**.**

**9**

**63**

**2**

**;**

so, the code I posted reads that data set and create a new variable named B

at each pass through the data step, 1 is added to the variable B

if a missing value of A is encountered, B is set to MISSING and the count starts over again from 1 as soon as the next non-missing value of A is encountered

the statement B+1; is a SUM statement ...

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000289454.htm

and it has some nice features: the value of B is automatically RETAINED (NO RETAIN statement is needed to prevent B from being set to MISSING on each pass through the data step); the initial value of the variable in the SUM statement is ZERO, not missing; even if the value of B is missing, the statement still works

*** here's what I posted;**

**data want;**

**set have;**

**b+1;**

**if missing(a) then b = .;**

**run;**

*** it is equivalent to this;**

**data want;**

**retain b 0;**

**set have;**

**b = sum(b,1);**

**if missing(a) then b = .;**

**run;**

does that help you understand?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-18-2012 04:49 PM

hI mIKE ,

i UNDERSTOOD IT BUT I HAVE A SMALL QUESTION.

wHEN EVER a is missing.then b is also set to missing...

and b+1 should have been .+1=. (a dot for the next value of b)!!!!!how did u get 1 in that place?????

Regards

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-18-2012 05:07 PM

hi ...

B+1;

is the same as

B = SUM(B,1);

both of those statement produce a 1 as the result (a SUM statement works like a SUM function and IGNORES missing values), take a look, if I run the SAS job ...

**data _null_;**

**b = .;**

**b + 1;**

**put "SUM STATEMENT RESULTS GIVEN B IS MISSING ---> " b;**

**b = .;**

**b = sum(b, 1);**

**put "SUM FUNCTION RESULTS GIVEN B IS MISSING ---> " b;**

**b = .;**

**b = b + 1;**

**put "ASSIGNMENT STATEMENT RESULTS GIVEN B IS MISSING ---> " b;**

**run;**

I get this in the LOG ...

**SUM STATEMENT RESULTS GIVEN B IS MISSING ---> 1**

**SUM FUNCTION RESULTS GIVEN B IS MISSING ---> 1**

**ASSIGNMENT STATEMENT RESULTS GIVEN B IS MISSING ---> .**

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-18-2012 05:14 PM

Thanks Mike fo ryour time;

I was reading the link about sum function i the mean while.........

I got the key point :

IN THE EXPRESSION OF A SUM STATEMENT "SAS TREATS AN EXPRESSION THAT PRODUCES A MISSING VALUE AS ZERO"

THAT IS WHY IT IS BECOMING 0+1 instead of . (missing)+1

Isnt my understanding right???

Also I will try to run the code you just sent me...

Thanks a ton

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-18-2012 05:55 PM

hi ... you're welcome and your interpretation is correct

ps if you are learning about various things is SAS, all the notes I wrote for my course are free (along with lots of other stuff) at ...

EPI 514 (COMPUTER APPLICATIONS IN EPIDEMIOLOGY a/k/a INTRODUCTION TO SAS)