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

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.
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@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;
--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

@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;
--
Paige Miller
hein68
Quartz | Level 8

This is great!  Thanks!!

Reeza
Super User

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.

 

PGStats
Opal | Level 21

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; 
PG

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 1382 views
  • 0 likes
  • 5 in conversation