BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

  id    pin
  C1    P1
  C1    P2
  C3    P3
  C4    P4
  C5    P4
 

if the id's are same and pin are different, then group= "Same-Id,Diff-Pin"
  if id's are different and have same pin , then group= "Same-Pin,Diff-Id"


  
  id    pin   group
  C1    P1    Same-Id,Diff-Pin
  C1    P2    Same-Id,Diff-Pin
  C3    P3    Other
  C4    P4    Same-Pin,Diff-Id
  C5    P4    Same-Pin,Diff-Id

3 REPLIES 3
stat_sas
Ammonite | Level 13

proc sql;
create table want as
select *,count(id) as cnt_id from have
group by id;
quit;

proc sql;
create table final as
select *,count(pin) as cnt_pin from want
group by pin;
quit;

proc sql;
select id,pin,case
when cnt_id>1 and cnt_pin=1 then 'Same-Id, Diff-Pin'
when cnt_id=1 and cnt_pin=1 then 'Other'
when cnt_id=1 and cnt_pin>1 then 'Same-Pin, Diff-Id'
end as group
from final
order by id,pin;
quit;

jimbobob
Quartz | Level 8

data y;

input id $ pin $;

cards;

C1    P1

C1    P2

C3    P3

C4    P4

C5    P4

C6   P5

C7    P6

C7    P7

C9    P7

;

run;

data y;

set y;

by id pin;

if first.id =last.id then a=0;

  else a+1;

if a ne 0 then a=1;

  else a=a;

run;

proc sort data=y;

    by pin a;

run;

data y;

set y;

by pin a;

if first.pin =last.pin then b=0;

  else b+1;

if b ne 0 then b=1;

  else b=b;

if a=1 and b=0 then group = "Same-Id,Diff-Pin";

if a=0 and b=0 then group = "Other";

if a=0 and b=1 then group = "Same-Pin,Diff-Id";

if a=1 and b=1 then group = "Other?";

drop a b;

run;

Tom
Super User Tom
Super User

You need to make sure that your input does not have multiple rows for the same ID*PIN combination for these queries to work.

proc sql ;

create table want as

  select id,pin,'Same-Id,Diff-Pin' as group

    from have

  group by id

  having count(*)> 1

  union

  select id,pin,'Same-Pin,Diff-Id' as group

    from have

  group by pin

  having count(*)> 1

  union

  select id,pin,'Other ' as group

    from (select id,pin

        from have

        group by id

        having count(*)= 1

       )

    group by pin

  having count(*)=1

  order by 1,2,3

;

quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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