BookmarkSubscribeRSS Feed
teamlinerek
Fluorite | Level 6

Hi All,

 

I am wanting to keep only observations where this is both a CEO and a CFO for every GVKEYYEAR.

 

I HAVE the following:

 

NameCEOANNCFOANNGVKEYYEAR
BatmanCEO 2008185
Robin CFO2008185
MickeyCEO 2001053
Mikey CFO2088087
LeoCEO 2088087
Goofy CFO2109087
SupermanCEO 2089171
Peanut ButterCEO 1089721
Jelly CFO1089721

 

What I WANT is the following:

NameCEOANNCFOANNGVKEYYEAR
BatmanCEO 2008185
Robin CFO2008185
Mikey CFO2088087
LeoCEO 2088087
Peanut ButterCEO 1089721
Jelly CFO1089721

 

What code may help me do this?

 

Thank you!

4 REPLIES 4
yabwon
Onyx | Level 15
data have;
infile cards dlm="|";
input Name :$32. (CEOANN CFOANN) (:$3.) GVKEYYEAR;
cards;
Batman|CEO| |2008185
Robin| |CFO|2008185
Mickey|CEO| |2001053
Mikey| |CFO|2088087
Leo|CEO| |2088087
Goofy| |CFO|2109087
Superman|CEO| |2089171
Peanut Butter|CEO| |1089721
Jelly| |CFO|1089721
;
run;
proc print;
run;

data want;
  call missing(check1,check2);
  do _N_=1 by 1 until(last.GVKEYYEAR);
    set have;
    by GVKEYYEAR notsorted;
    check1+(CEOANN='CEO');
    check2+(CFOANN='CFO');
  end;

  do _N_=1 to _N_;
    set have;
    if check1 & check2 then output;
  end;
  drop check1 check2; 
run; 
proc print;
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



ballardw
Super User

This works for example data provided

proc sql;
   create table want as
   select b.* 
   from 
     ( select gvkeyyear 
      from  have
      where not missing(ceoann) or not missing(cfoAnn)
      group gvkeyyear
      having count(gvkeyyear)=2
     ) as a
     left join have as b
     on a.gvkeyyear=b.gvkeyyear
   ;  
quit;

The Where may not be needed but might if  you have other observations in your data for the gvkeyyear related to other data not shown in your example.

If you built that set to have two separate variables for Ceoann and CfoAnn you might consider a single variable with the position value, such as CEO and CFO. That is quite often more flexible in the long run than adding a bunch of variables for each level of meaning.

mkeintz
PROC Star

Your data appears to be already presented either as a single record for a given GVKEYYEAR, or a pair of records for a GVKEYYEAR.  In the latter case, one CEO appears and one CFO appears.  If that is the case, then just delete all the singletons and keep all the pairs.

 

data have;
infile cards dlm="|";
input Name :$32. (CEOANN CFOANN) (:$3.) GVKEYYEAR;
cards;
Batman|CEO| |2008185
Robin| |CFO|2008185
Mickey|CEO| |2001053
Mikey| |CFO|2088087
Leo|CEO| |2088087
Goofy| |CFO|2109087
Superman|CEO| |2089171
Peanut Butter|CEO| |1089721
Jelly| |CFO|1089721
run;
data want;
  set have ;
  by gvkeyyear notsorted;
  if not(first.gvkeyyear=1 and last.gvkeyyear=1); 
run;

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
data have;
infile cards dlm="|";
input Name :$32. (CEOANN CFOANN) (:$3.) GVKEYYEAR;
cards;
Batman|CEO| |2008185
Robin| |CFO|2008185
Mickey|CEO| |2001053
Mikey| |CFO|2088087
Leo|CEO| |2088087
Goofy| |CFO|2109087
Superman|CEO| |2089171
Peanut Butter|CEO| |1089721
Jelly| |CFO|1089721
;
run;

proc sql;
create table want as
select * from have 
 group by GVKEYYEAR
  having sum(CEOANN='CEO') and sum(CFOANN='CFO');
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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