BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
waterjelly
Fluorite | Level 6

Hi, I want to make 2 tables out of one original table.

How can I make these tables? 

 

<IN>

Patient no.

DRUG_CODE2

1

SMR

1

SMR

1

other

1

NARCOTIC

2

NARCOTIC

2

other

2

other

3

SMR

3

other

4

other

4

other

 

<WANT>

<Table 1. SMR + NARCOTIC plus SMR+other(Non-NARCOTIC)>

Patient no.

DRUG_CODE2

1

SMR

1

SMR

1

other

1

NARCOTIC

3

SMR

3

other

 

<Table 2. Non SMR + NARCOTIC plus Other>

Patient no.

DRUG_CODE2

2

NARCOTIC

2

other

2

other

4

other

4

other

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
LENGTH Patient_no 8 DRUG_CODE2 $ 10;
input Patient_no DRUG_CODE2 $;
cards;
1 SMR
1 SMR
1 other
1 NARCOTIC
2 NARCOTIC
2 other
2 other
3 SMR
3 other
4 other
4 other
;
run;

proc sql;
create table want1 as
select * from have
 group by Patient_no
  having sum(DRUG_CODE2='SMR') ne 0;

create table want2 as
select * from have
 group by Patient_no
  having sum(DRUG_CODE2='SMR') eq 0;

quit;

View solution in original post

4 REPLIES 4
sbxkoenk
SAS Super FREQ

Hello,

 

Your question is not very clear.

Do you mean:

  • table WANT_1 should contain all patients having "<SMR + NARCOTIC> OR <SMR + other(Non-NARCOTIC)>" and
  • table WANT_2 should contain all patients having "<Non SMR + NARCOTIC> OR just <other>".

Please clarify.

 

Anyway, it may help you to transpose your table. That way, it becomes a one-row based query.

 

data have;
LENGTH Patient_no 8 DRUG_CODE2 $ 10;
input Patient_no DRUG_CODE2 $;
cards;
1 SMR
1 SMR
1 other
1 NARCOTIC
2 NARCOTIC
2 other
2 other
3 SMR
3 other
4 other
4 other
;
run;

PROC TRANSPOSE data=have out=have_trp;
 by Patient_no;
 var DRUG_CODE2;
run;

Koen

 

waterjelly
Fluorite | Level 6

Hi, I want 

  • table WANT_1 should contain all patients having "<SMR + NARCOTIC> AND <SMR + other(Non-NARCOTIC)>" and
  • table WANT_2 should contain all patients having "<Non SMR(other) + NARCOTIC> AND just <other>".

Thank you! 

Ksharp
Super User
data have;
LENGTH Patient_no 8 DRUG_CODE2 $ 10;
input Patient_no DRUG_CODE2 $;
cards;
1 SMR
1 SMR
1 other
1 NARCOTIC
2 NARCOTIC
2 other
2 other
3 SMR
3 other
4 other
4 other
;
run;

proc sql;
create table want1 as
select * from have
 group by Patient_no
  having sum(DRUG_CODE2='SMR') ne 0;

create table want2 as
select * from have
 group by Patient_no
  having sum(DRUG_CODE2='SMR') eq 0;

quit;

ballardw
Super User

Programming works on rules.

What are the rules involved for which records end up in which?

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
  • 4 replies
  • 598 views
  • 2 likes
  • 4 in conversation