I have code like this (see below) where I get the top 25 diagnoses:
@hein68 wrote:
I have code like this (see below) where I get the top 25 diagnoses:
proc freq data=&dataems order=freq;
tables diag1CCS/noprint out=counts;
run;data state;
set counts (obs=25);
drop percent;
run;Then I want to write another program to go back to my original data (&dataems) and keep only the records with the top 25 diag1ccs categories. How could I do that without having to copy and paste the top 25 diag1ccs categories into another data step? I want a more automated process, because I will end up having to do this over 100 times.Thanks for your help.
How about PROC SQL
proc sql;
create table new as select * from &dataems
where diag1ccs in (Select distinct diag1ccs from state);
quit;
@hein68 wrote:
I have code like this (see below) where I get the top 25 diagnoses:
proc freq data=&dataems order=freq;
tables diag1CCS/noprint out=counts;
run;data state;
set counts (obs=25);
drop percent;
run;Then I want to write another program to go back to my original data (&dataems) and keep only the records with the top 25 diag1ccs categories. How could I do that without having to copy and paste the top 25 diag1ccs categories into another data step? I want a more automated process, because I will end up having to do this over 100 times.Thanks for your help.
How about PROC SQL
proc sql;
create table new as select * from &dataems
where diag1ccs in (Select distinct diag1ccs from state);
quit;
This is great! Thanks!!
Is diagnosis a single file or do you need to search all 25 (usual number) of diagnosis fields?
@hein68 wrote:
I have code like this (see below) where I get the top 25 diagnoses:
proc freq data=&dataems order=freq;
tables diag1CCS/noprint out=counts;
run;data state;
set counts (obs=25);
drop percent;
run;Then I want to write another program to go back to my original data (&dataems) and keep only the records with the top 25 diag1ccs categories. How could I do that without having to copy and paste the top 25 diag1ccs categories into another data step? I want a more automated process, because I will end up having to do this over 100 times.Thanks for your help.
Take advantage of BY processing, instead of macro processing. It's more efficient and easier to code. Here is an example calculating the median weight of students from the top three age groups, for each sex :
proc sort data=sashelp.class out=class;
by sex age;
run;
proc freq data=class order=freq;
by sex;
tables age/noprint out=counts;
run;
data keep;
do i = 1 by 1 until(last.sex);
set counts; by sex;
if i <= 3 then output;
end;
drop i percent;
run;
data select;
merge class keep(in=ok);
by sex age;
if ok;
run;
proc means data=select noprint;
by sex;
var weight;
output out=medians median=med;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.