Counting through do loop.

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

Counting through do loop.

Hi Everyone,

My data is 01 series of number. I want to count the number of records between value -3 and 3.

I thought my code should work but somehow, it doesn't count, even though it can identify point where it should count.

I believe that there could be better solution but I personally like the DO LOOP approach.

I appreciate it if you could check it for me.

Thank you,

HHC


data have;
  input time value;
  id=_n_;
  datalines;
1 2
2 -3
3 2
1 3
2 0
3 6
4 3
1 -2
2 1
3 3
4 3
1 -3
0 3
3 0
2 4
1 -3
0 0
;
run;

data want;
set have nobs=nobs;

found=0;
v3=0;
i+1;

if value=-3 then
do j=i +1 to nobs until (found=1); *start loop through file have;
N=0;
set have (keep = value rename=(value=value3)) point=j ;
if value3^=3 then do;
  N=N+1;end;
else if value3=3 then do;
  found=1;
  v3=value3;
  end;
end;
run;

Thank you Everyone for helping.

I found my error.

data want;
set have nobs=nobs;
i+1;

if value=-3 then do;     *cannot be do without, I see;

N=0;                            *N=0 must be outside the loop; 
do j=i +1 to nobs until (found=1); *start loop through file have;

set have (keep = value rename=(value=value3)) point=j ;
if value3^=3 then do;
  N=N+1;end;
else if value3=3 then do;
  found=1;
  v3=value3;
  end;
end;
end;
run;


Accepted Solutions
Solution
‎06-18-2014 03:13 PM
Super User
Posts: 5,080

Re: Counting through do loop.

Well, you're close.  Your program does have a few extra moving parts, but the main culprit is that N=0 executes on every iteration of your DO loop.  Here's a simplified approach:

data want;

set have nobs=nobs;

if value=-3 and _n_ < nobs then do;

   n = 0;

   do i=_n_ + 1 to nobs until (nextvalue =3);

      set have (keep=value rename=(value=nextvalue)) point=i;

      n + 1;

   end;

   if nextvalue ne 3 then n=.;

end;

drop nextvalue;

run;

Note that the variable ID is not needed.

View solution in original post


All Replies
Respected Advisor
Posts: 3,124

Re: Counting through do loop.

What is your expected outcome given the sample data?

Solution
‎06-18-2014 03:13 PM
Super User
Posts: 5,080

Re: Counting through do loop.

Well, you're close.  Your program does have a few extra moving parts, but the main culprit is that N=0 executes on every iteration of your DO loop.  Here's a simplified approach:

data want;

set have nobs=nobs;

if value=-3 and _n_ < nobs then do;

   n = 0;

   do i=_n_ + 1 to nobs until (nextvalue =3);

      set have (keep=value rename=(value=nextvalue)) point=i;

      n + 1;

   end;

   if nextvalue ne 3 then n=.;

end;

drop nextvalue;

run;

Note that the variable ID is not needed.

Regular Contributor
Posts: 217

Re: Counting through do loop.

I am not certain I know what you are trying to end up with.  I do know that your are not retaining values through each read of the data.  If you are trying to count and place the count in the variable N, then you need to retain N.

64
65   data want;
66   set have nobs=nobs;
67   found=0;
68      v3=0;
69   put "i = " i;
70      i+1;
71   put "i = " i;
72   if value=-3 then
73         put 'start '  i= n= j= ;
74        do j=i +1 to nobs until (found=1); *start loop through file have;
75             N=0;
76             set have (keep = value rename=(value=value3)) point=j ;
77             if value3^=3 then do;
78               N=N+1;
79               put 'start '  i= n= j= ;
80             end;
81             else if value3=3 then do;
82               found=1;
83               v3=value3;
84        end;
85   end;
86   run;

i = 0
i = 1
start i=1 n=1 j=2
start i=1 n=1 j=3
i = 1
i = 2
start i=2 n=. j=4
start i=2 n=1 j=3
i = 2
i = 3
i = 3
i = 4
start i=4 n=1 j=5
start i=4 n=1 j=6
i = 4
i = 5
start i=5 n=1 j=6
i = 5
i = 6
i = 6
i = 7
start i=7 n=1 j=8
start i=7 n=1 j=9
i = 7
i = 8
start i=8 n=1 j=9
i = 8
i = 9
i = 9
i = 10
i = 10
i = 11
start i=11 n=1 j=12
i = 11
i = 12
start i=12 n=. j=13
i = 12
i = 13
start i=13 n=1 j=14
start i=13 n=1 j=15
start i=13 n=1 j=16
start i=13 n=1 j=17
i = 13
i = 14
start i=14 n=1 j=15
start i=14 n=1 j=16
start i=14 n=1 j=17
i = 14
i = 15
start i=15 n=1 j=16
start i=15 n=1 j=17
i = 15
i = 16
start i=16 n=. j=18
start i=16 n=1 j=17
i = 16
i = 17

Regular Contributor
Posts: 217

Re: Counting through do loop.

hhchenfx,

What values of N are you expecting in the following list of values?


                                  Obs    time    value    id    n    nextvalue

                                        1      1        2      1    0        .
                                        2      2       -3      2    2        3
                                        3      3        2      3    2        3
                                        4      1        3      4    2        3
                                        5      2        0      5    2        3
                                        6      3        6      6    2        3
                                        7      4        3      7    2        3
                                        8      1       -2      8    2        3
                                        9      2        1      9    2        3
                                       10      3        3     10    2        3
                                       11      4        3     11    2        3
                                       12      1       -3     12    1        3
                                       13      0        3     13    1        3
                                       14      3        0     14    1        3
                                       15      2        4     15    1        3
                                       16      1       -3     16    .        0
                                       17      0        0     17    .        0


                                

Super Contributor
Posts: 371

Re: Counting through do loop.

Thank you Everyone for helping.

I found my error.

data want;
set have nobs=nobs;
i+1;

if value=-3 then do;     *cannot be do without, I see;

N=0;                            *N=0 must be outside the loop; 
do j=i +1 to nobs until (found=1); *start loop through file have;

set have (keep = value rename=(value=value3)) point=j ;
if value3^=3 then do;
  N=N+1;end;
else if value3=3 then do;
  found=1;
  v3=value3;
  end;
end;
end;
run;

Trusted Advisor
Posts: 1,204

Re: Counting through do loop.

Another way using proc sql

proc sql;

create table want as

select *,case when value between -3 and 3 then 1 else 0 end as flag from have;

select count(*) as cnt from want

where flag=1;

quit;

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 281 views
  • 6 likes
  • 5 in conversation