BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jonate_H
Quartz | Level 8

For the attached sample data, I would like to generate the variable "frequency", its value is calculated by id and by year. what is the simply way to get that? I know that the simple way to get the variable "count" is something like:

data want;

  set sampledata;

  count + 1;

  by id year;

  if first.id or first.year then count = 1;

run;

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Add some other variables other than group variable into sql.

Proc SQL ;

     Create table want as

          select *

               ,     count (id) as count

          From have

          Group by id, year

     ;

Quit ;

Xia Keshan

View solution in original post

4 REPLIES 4
RichardinOz
Quartz | Level 8

You can do counts in SQL

Proc SQL ;

     Create table want as

          select id

               ,     year

               ,     count (id) as count

          From have

          Group by id, year

     ;

Quit ;


Or

Proc Freq data = have ;

     tables id * year  / out = want (drop = percent) ;

run ;

You could also use Summary or Univariate if there is another numeric variable you can use for counting (use the N statistic).

Proc Report or Tabulate are also options


Richard

Jonate_H
Quartz | Level 8

Thank you Richard!

In that way, I will have a summarized table as below first image shows.

so, in order to make the exact same data set as below second image shows, I will have to merge original data with the summarized table.

Is it possible that I can create the variable "frequency" without using merge?

This is the table I get by Richard's first code:

frequency.png

I hope to create the variable "frequency" directly in the original data set as below showed:

results.png

Ksharp
Super User

Add some other variables other than group variable into sql.

Proc SQL ;

     Create table want as

          select *

               ,     count (id) as count

          From have

          Group by id, year

     ;

Quit ;

Xia Keshan

Jonate_H
Quartz | Level 8

That works. Thank you, Keshan!

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
  • 12034 views
  • 3 likes
  • 3 in conversation