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
hi ... how about this ...
data want;
set have;
b+1;
if missing(a) then b = .;
run;
hi ... how about this ...
data want;
set have;
b+1;
if missing(a) then b = .;
run;
Hi Mike,
It totally worked - can't believe it was this simple. Thanks heaps dude!
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
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?
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
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 ---> .
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
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)
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.