Dynamic count

Reply
Frequent Contributor
Posts: 86

Dynamic count

hi,

If i given a Categories =18;     That is hard coded value for variable..... If i have a 18  variables (Category_1, Category_2, Category_3,Category_4,.....Category_18. how can i get a dynamic value for 'Categories' variable ?


thx,

Smiley Happy

PROC Star
Posts: 7,363

Re: Dynamic count

There are probably a numbers of ways to do what I think you want to do.  e.g.:

data have;

  input Categories Category_1-Category_18;

  cards;

18 1 2 3 0 0 1 2 3 0 0 1 2 3 0 0 1 2 3

18 2 3 4 0 7 2 3 4 0 7 2 3 4 0 7 2 3 4

;

run;

data want;

  array cat Category_1-Category_18;

  set have;

  Categories=0;

  do over cat;

    Categories=sum(categories,cat ne 0);

  end;

run;

Super Contributor
Posts: 1,636

Re: Dynamic count

is this what you want:

data have;

  input Category_1-Category_18;

  cards;

1 2 3 0 0 1 2 3 0 0 1 2 3 0 0 1 2 3

2 3 4 0 7 2 3 4 0 7 2 3 4 0 . . . .

;

run;

data want;

  array cat Category_:;

  set have;

  do over cat;

    Categories=sum(categories,cat ne .);

  end;

run;

proc print;run;

                                       C   C   C   C   C   C   C   C   C

      C   C   C   C   C   C   C   C   C   a   a   a   a   a   a   a   a   a    C

      a   a   a   a   a   a   a   a   a   t   t   t   t   t   t   t   t   t    a

      t   t   t   t   t   t   t   t   t   e   e   e   e   e   e   e   e   e    t

      e   e   e   e   e   e   e   e   e   g   g   g   g   g   g   g   g   g    e

      g   g   g   g   g   g   g   g   g   o   o   o   o   o   o   o   o   o    g

      o   o   o   o   o   o   o   o   o   r   r   r   r   r   r   r   r   r    o

      r   r   r   r   r   r   r   r   r   y   y   y   y   y   y   y   y   y    r

  O   y   y   y   y   y   y   y   y   y   _   _   _   _   _   _   _   _   _    i

  b   _   _   _   _   _   _   _   _   _   1   1   1   1   1   1   1   1   1    e

  s   1   2   3   4   5   6   7   8   9   0   1   2   3   4   5   6   7   8    s

  1   1   2   3   0   0   1   2   3   0   0   1   2   3   0   0   1   2   3   18

  2   2   3   4   0   7   2   3   4   0   7   2   3   4   0     .   .   .   .   14

PROC Star
Posts: 7,363

Re: Dynamic count

Or, if you have to check for both missing and zero:

data have;

  input Categories Category_1-Category_18;

  cards;

18 1 2 3 . 0 1 2 3 0 . 1 2 3 0 0 1 2 3

18 2 3 4 0 7 2 3 4 0 7 2 3 . 0 7 2 3 4

;

run;

data want;

  array cat Category_1-Category_18;

  set have;

  Categories=0;

  do over cat;

    Categories=sum(categories,not(missing(cat)) and cat ne 0);

  end;

run;

Frequent Contributor
Posts: 86

Re: Dynamic count

No!  If you don't mind can i have any contact id (skype) so that i can share my screen and show the code where am struck up....Please need your help very badly Smiley Sad

Super User
Posts: 5,257

Re: Dynamic count

Perhaps a transpose followed by a select count(*) group by category work for you...?

Otherwise, it seems we're lacking information on how you would like to do the count...:smileyconfused:

Data never sleeps
PROC Star
Posts: 7,363

Re: Dynamic count

You can attach your screen shot and code in a post on this forum

Frequent Contributor
Posts: 86

Re: Dynamic count

@ Arthur  Sorry I can send an a email or i can show u by Skype, team viewer, join me.

you can reach me @ 4784gsks@gmail.com

Thanks,

Smiley Happy

PROC Star
Posts: 7,363

Re: Dynamic count

: Since the benefit of sites like the forum is helping SAS users, as a group, to better learn and make better and more efficient use of the language, I have always refrained from one-on-one discussions.  If you post your material on the forum, and can describe the problem in sufficient detail, that will be the best chance that you will receive the best answers.

Respected Advisor
Posts: 3,894

Re: Dynamic count

If I understand your question then you're after nothing else than a way to count the number of elements in an array. If so then the dim() function will do.

data have;

  array vars {18} 8.;

  nArrayElements=dim(vars);

  put nArrayElements=;

run;

Ask a Question
Discussion stats
  • 9 replies
  • 324 views
  • 2 likes
  • 5 in conversation