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-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 12649 views
  • 3 likes
  • 3 in conversation