BookmarkSubscribeRSS Feed
Grandhi4
Calcite | Level 5

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

9 REPLIES 9
art297
Opal | Level 21

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;

Linlin
Lapis Lazuli | Level 10

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

art297
Opal | Level 21

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;

Grandhi4
Calcite | Level 5

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

LinusH
Tourmaline | Level 20

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
art297
Opal | Level 21

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

Grandhi4
Calcite | Level 5

@ 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

art297
Opal | Level 21

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

Patrick
Opal | Level 21

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 784 views
  • 2 likes
  • 5 in conversation