DATA Step, Macro, Functions and more

Phyid and Flag

Reply
Super Contributor
Posts: 673

Phyid and Flag

The phyid's have flag 'C' or 'P'. How do to display phyid that have flag 'C' and 'P'.

for instance:

Phyid flag
1001 C
1002 P
1003 C
1003 P
1004 C
1004 P

display only 1003 and 1004 as they have flag 'C' and 'P'.
Regular Contributor
Posts: 165

Re: Phyid and Flag

If flag can only have those two values then you could sort by phyid and flag. Then use by group processing with first.flag and last.flag to exclude observations that are both first and last within a group.
Super Contributor
Posts: 673

Re: Phyid and Flag

Thanks.
Phyid flag
1001 C
1002 P
1003 C
1003 P
1004 C
1004 P

if a phyid has C and P and we need only P to be displayed like 1003 and 1004 only P are displayed.
1001 C
1002 P
1003 P
1004 P
SAS Super FREQ
Posts: 8,869

Re: Phyid and Flag

Hi:
Do you want an output dataset or do you want a report??? And if you want a report, what is your destination of interest? If you want a dataset, do you just want to drop the obs for the phyid that has both C and P???
cynthia
Super Contributor
Posts: 673

Re: Phyid and Flag

Posted in reply to Cynthia_sas
Need dataset.If a phyid has a flag 'C' and 'P' i need only that record for 'P'.
1001 C
1002 P
1003 C
1003 P
1004 P
1004 C

the output:
1001 C
1002 P
1003 P
1004 P
Regular Contributor
Posts: 165

Re: Phyid and Flag

You could do this with two sorts. The first sort by phyid and descending flag. The second sort just by phyid and use the nodupkey option.
Regular Contributor
Posts: 171

Re: Phyid and Flag

[pre]

data test;
input phyid flag $;
datalines;
1001 C
1002 P
1003 C
1003 P
1004 C
1004 P
;
run;

proc sql;
select phyid from test where flag = 'C'
intersect
select phyid from test where flag = 'P';
quit;

[/pre]
Regular Contributor
Posts: 241

Re: Phyid and Flag

@polingjw: Here is another way, taking advantage of proc sql's automatic re-merging of the summary statistics:
[pre]
/* test data by polingjw */
data test;
input phyid flag $;
datalines;
1001 C
1002 P
1003 C
1003 P
1004 C
1004 P
;
run;

proc sql;
create table test2 as
select phyid, flag, sum(flag="C") as c, sum(flag="P") as p
from test
group by phyid;

/* print out an obs if the id has only C or P;
the flag="P" obs if the id has both */
select * from test2
where (c and not p) or (not c and p) or (c and p and flag="P");
/* on lst
phyid flag c p
--------------------------------------
1001 C 1 0
1002 P 0 1
1003 P 1 1
1004 P 1 1
*/
quit;
[/pre]
Super Contributor
Posts: 673

Re: Phyid and Flag

Posted in reply to chang_y_chung_hotmail_com
This is interesting.Thanks for the help.
Ask a Question
Discussion stats
  • 8 replies
  • 178 views
  • 0 likes
  • 5 in conversation