BookmarkSubscribeRSS Feed
mesujit
Calcite | Level 5

I have a small querry on below data set:
- where ever there are multiple entries of siteid, i have to keep the only one entry which has sitestatus as 'open' and siterole as 'PI', other rows of same siteid i have to delete.
Please help me to code it.
i am trying , but loosing some or the other required lines.

 

==== input data set====
siteidsiteStatussiterole
544openPI
544BusyPI
565closed or BusyPI
567openPI
543closedPI
543openPI
   
==== output data set should be====
siteidsiteStatussiteRrole
544openPI
565closed or BusyPI
567openPI
543openPI
3 REPLIES 3
novinosrin
Tourmaline | Level 20

Hi @mesujit  Can you please clarify why the following is in the output?

565 closed or Busy PI

 

data have;
input siteid	(siteStatus	siterole) ( & $15.);
cards;
544	open	PI
544	Busy	PI
565	closed or Busy	  PI
567	open	PI
543	closed	PI
543	open	PI
;

proc sql;
 create table want(drop=_n_) as
 select *,(siteStatus='open' and siterole='PI') as _n_
 from have
 group by siteid
 having _n_ or max(_n_)=0;
quit;

 

mesujit
Calcite | Level 5

Hi Novinosrin,

its because the site status is not confirmed.. cant be considered as open.

PeterClemmensen
Tourmaline | Level 20
data have;
input siteid siteStatus $ 5-19 siterole $;
datalines;
544 open           PI
544 Busy           PI
565 closed or Busy PI
567 open           PI
543 closed         PI
543 open           PI
;

data want;
   do _N_ = 1 by 1 until (last.siteid);
      set have;
      by siteid notsorted;
   end;
   do until (last.siteid);
      set have;
      by siteid notsorted;
      if _N_ = 1 | siteStatus = "open" then output;
   end;
run;

Result:

 

siteid siteStatus     siterole 
544    open           PI 
565    closed or Busy PI 
567    open           PI 
543    open           PI 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 725 views
  • 0 likes
  • 3 in conversation