DATA Step, Macro, Functions and more

counting observations in by group

Reply
Regular Contributor
Posts: 218

counting observations in by group

Hi All,

I want to know how many times a by group occured in a table and write that on a separate dataset (has to be a character number as I am concatenating with character as well). Can someone help please.

 

data have;
input Student_ID $ Course_ID $ Score;
cards;
M101 Eng 20
M101 Bio 20
M101 Eng 10
M101 Math 18
F103 Bio 18
F103 Math 99
F103 Eng 16
F103 Che 15
F103 Math 20
F103 Phy 17
RUN;

 

DATA want;
line='Totalcount'||Number of M101 by group occurance in have table||Number of F103 by group occurance in have table;
RUN;

 

Expected output:

line=Totalcount004006;

Super User
Posts: 5,516

Re: counting observations in by group

Probably many ways to skin this cat ...

 

data want;

length line $ 16;

retain line 'Totalcount';

count=0;

do until (last.Student_ID);

   set have end=done;

   by Student_ID;

   count + 1;

end;

line = cats(line, put(count, z3.));

keep line;

if done;

run;

 

It's untested, but definitely headed in the right direction.

 

Are you sure you don't want to keep Student_ID as part of the output line?

Regular Contributor
Posts: 218

Re: counting observations in by group

Posted in reply to Astounding
Hi Astounding,
Thanks for your reply. Your code is working, but I want to put that 2 count in between other character variable. Like:

Line='ABCD'||'M4U'||count1count2||'AAAAAA';

Expected Output: ABCDM4U004006AAAAAA

Can you please tell me how it is possible. Thanks.
Super User
Posts: 19,851

Re: counting observations in by group

That doesn't make sense, how would you parse the numbers back out.

 

Wouldn't you at least want a delimiter between the counts? Or a standard format, ie 8 digits?

 

Are you assuming the Z3 format, so no counts greater than 999?

Super User
Posts: 11,343

Re: counting observations in by group

What happens if you have more than two values for student_id such as M101 M102 M103 F101 F102 and F103?

Ask a Question
Discussion stats
  • 4 replies
  • 281 views
  • 0 likes
  • 4 in conversation