BookmarkSubscribeRSS Feed
France
Quartz | Level 8

Dear all,

 

how can I exclude all 'company_name' variable if one of the 'year' variable( belongs to the same 'company_name' group) smaller than 2009.

 

 

data old;<br />input <br />company_name $50. <br />year 50.<br />;<br />datalines;
A, 1990<br />A, 2011<br />B, 2019
;
run;<code></code>

 

 

 I expect to get the result like

 

 

B, 2019

 as one 'year' variable of company_name 'A' variable is 1990 (which is smaller than 2009), so, I exclude all A company,

 

 

could you please give me some suggestions about this?

thanks in advance

 

2 REPLIES 2
Kurt_Bremser
Super User

Do a subselect for the exclusion values, and use that in a where:

proc sql ;
create table want as
select *
from old
where company_name not in (
  select company_name
  from old
  where min(year) < 2009
  group by company_name
);
quit;

(untested, posted from tablet)

Kurt_Bremser
Super User

Since I'm now back in office, I could test it.

First, it is always a VERY GOOD IDEA to test your own datastep code for example data before posting it here. Testing is not a crime, it won't make you blind or cause your hair to fall out.

Your code would not produce any data, instead it caused an ERROR message.

I fixed it to this:

data old;
infile datalines dlm=',';
input
  company_name $
  year
;
datalines;
A, 1990
A, 2011
B, 2019
;
run;

After that, I found my own code was using the summary function incorrectly, so I had to restructure the subselect to using a HAVING clause:

proc sql ;
create table want as
select *
from old
where company_name not in (
  select company_name
  from old
  group by company_name
  having min(year) < 2009
);
quit;

This creates your expected result.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2 replies
  • 956 views
  • 0 likes
  • 2 in conversation