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!
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
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
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:
I hope to create the variable "frequency" directly in the original data set as below showed:
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
That works. Thank you, Keshan!
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.
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.
Ready to level-up your skills? Choose your own adventure.