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

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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