Help using Base SAS procedures

Counting observations separated by missing values

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Counting observations separated by missing values

Hi folks,

How do I obtain column b?

ab
31
652
23
14
45
..
51
..
61
22
13
24
..
11
..
21
52
..
91
632
23
..
..
11
22
233
64
95
56
27
18

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
Valued Guide
Posts: 765

Re: Counting observations separated by missing values

hi ... how about this ...

data want;

set have;

b+1;

if missing(a) then b = .;

run;

View solution in original post


All Replies
Solution
‎09-15-2012 11:03 AM
Valued Guide
Posts: 765

Re: Counting observations separated by missing values

hi ... how about this ...

data want;

set have;

b+1;

if missing(a) then b = .;

run;

Occasional Contributor
Posts: 6

Re: Counting observations separated by missing values

Hi Mike,

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

Super Contributor
Posts: 1,041

Re: Counting observations separated by missing values

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

Valued Guide
Posts: 765

Re: Counting observations separated by missing values

Posted in reply to robertrao

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?

Super Contributor
Posts: 1,041

Re: Counting observations separated by missing values

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

Valued Guide
Posts: 765

Re: Counting observations separated by missing values

Posted in reply to robertrao

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

Super Contributor
Posts: 1,041

Re: Counting observations separated by missing values

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

Valued Guide
Posts: 765

Re: Counting observations separated by missing values

Posted in reply to robertrao

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)

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 248 views
  • 1 like
  • 3 in conversation