BookmarkSubscribeRSS Feed
mlogan
Lapis Lazuli | Level 10

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;

4 REPLIES 4
Astounding
PROC Star

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?

mlogan
Lapis Lazuli | Level 10
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.
Reeza
Super User

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?

ballardw
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 768 views
  • 0 likes
  • 4 in conversation