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

Hello,

Can someone help me decipher what is wrong with this code? The work dataset has about half a million observation but its not been outputted to the overlapped dataset as that always comes up as zero observation.

Thank you for your help.

Please see below code with associated error

data Overlapped; set WORK.Final; by DE_id ondate; *type class ; retain typa typb;
if first.ondate then do; count=0; typa=0; typb=0; end;
if CLASS='OPIOID' then typa+1;
if CLASS='BENZO' then typb+1;
count+1;
if last.ondate and count gt 1 then do;
if typa and typb then do; olap='O&B'; output; end;
if typa gt 1 then do; olap='O'; output; end;
if typb gt 1 then do; olap='B'; output; end;
end;
drop CLASS typa typb;

proc sort DATA= Overlapped; by DE_id olap ondate;
proc print DATA= Overlapped (OBS=10); id DE_id olap ondate;
title 'Collapse by date and type where multiple drugs taken'; run;


ERROR CODE: ALL IN BLACK (NO RED)

 data Overlapped; set WORK.Final; by DE_id ondate; *type class ; retain typa typb;

 if first.ondate then do; count=0; typa=0; typb=0; end;

if CLASS='OPIOID' then typa+1;

 if CLASS='BENZO' then typb+1;

*count+1;

 if last.ondate and count gt 1 then do;

 if typa and typb then do; olap='O&B'; output; end;

 if typa gt 1 then do; olap='O'; output; end;

 if typb gt 1 then do; olap='B'; output; end;

 end;

 drop CLASS typa typb;

NOTE: There were 54550121 observations read from the data set WORK.FINAL.
NOTE: The data set WORK.OVERLAPPED has 0 observations and 24 variables.
NOTE: DATA statement used (Total process time):
real time 59.19 seconds
cpu time 16.32 seconds

 proc sort DATA= Overlapped; by DE_id olap ondate;

NOTE: Input data set is empty.
NOTE: The data set WORK.OVERLAPPED has 0 observations and 24 variables.
NOTE: PROCEDURE SORT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

 proc print DATA= Overlapped (OBS=10); id DE_id olap ondate;

 title 'Collapse by date and type where multiple drugs taken'; run;
NOTE: No observations in data set WORK.OVERLAPPED.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

> Can someone help me decipher what is wrong with this code? 

One thing that is wrong is that it is badly formatted.

That makes it hard to read, to understand the logic, and to find errors.

Code should be well presented.

This takes seconds to implement, makes you look professional, and saves you hours of debugging.

Consider the below equivalent data step. Which one is easier to read? 

 

data OVERLAPPED; 
  set WORK.FINAL; 
  by DE_ID ONDATE; 
  if first.ONDATE then call missing( COUNT, TYPA, TYPB);
  if CLASS='OPIOID' then TYPA+1;
  if CLASS='BENZO ' then TYPB+1;
  COUNT+1;
  if last.ONDATE and COUNT gt 1 then do;
    if TYPA and TYPB then do; OLAP='O&B'; output; end;
    if TYPA gt 1     then do; OLAP='O  '; output; end;
    if TYPB gt 1     then do; OLAP='B  '; output; end;
  end;
  drop CLASS TYPA TYPB;
run;

Now as to why you get no records, the reason is that your data does not match you code, as @Reeza said.

The values sought in the code seem to be never found in the data.

Maybe they are not uppercase, or they have leading spaces. Look at your data.

 

View solution in original post

5 REPLIES 5
Reeza
Super User

Can you post the output from the following:

 

proc freq data=final;
table class;
run;

data demo;
set final;
where class in ('OPIOID', 'BENZO');
run;

I suspect you're not matching the conditions in the data step for some reason, likely case differences but this would tell you that. 

OPIOID and BENZO would have to be in the data set as exactly those values.

Anita3
Fluorite | Level 6

Thank you for your prompt response Reeza. Please see below.

 

2525039546.432525039546.43
2913658653.5754386981100.00

 

Log:

 

proc freq data=final;
 table class;
 run;

NOTE: Writing HTML Body file: sashtml1.htm
NOTE: There were 54550121 observations read from the data set WORK.FINAL.
NOTE: PROCEDURE FREQ used (Total process time):
real time 1:02.01
cpu time 10.42 seconds


 data demo;set final;where class in ('OPIOID', 'BENZO');run;

NOTE: There were 0 observations read from the data set WORK.FINAL.
WHERE class in ('BENZO', 'OPIOID');
NOTE: The data set WORK.DEMO has 0 observations and 23 variables.
NOTE: DATA statement used (Total process time):
real time 13.57 seconds
cpu time 7.21 second

ChrisNZ
Tourmaline | Level 20

> Can someone help me decipher what is wrong with this code? 

One thing that is wrong is that it is badly formatted.

That makes it hard to read, to understand the logic, and to find errors.

Code should be well presented.

This takes seconds to implement, makes you look professional, and saves you hours of debugging.

Consider the below equivalent data step. Which one is easier to read? 

 

data OVERLAPPED; 
  set WORK.FINAL; 
  by DE_ID ONDATE; 
  if first.ONDATE then call missing( COUNT, TYPA, TYPB);
  if CLASS='OPIOID' then TYPA+1;
  if CLASS='BENZO ' then TYPB+1;
  COUNT+1;
  if last.ONDATE and COUNT gt 1 then do;
    if TYPA and TYPB then do; OLAP='O&B'; output; end;
    if TYPA gt 1     then do; OLAP='O  '; output; end;
    if TYPB gt 1     then do; OLAP='B  '; output; end;
  end;
  drop CLASS TYPA TYPB;
run;

Now as to why you get no records, the reason is that your data does not match you code, as @Reeza said.

The values sought in the code seem to be never found in the data.

Maybe they are not uppercase, or they have leading spaces. Look at your data.

 

Astounding
PROC Star
As character strings, these are all different:

OPIOID
OPIOID
Opioid
opioid

Your program has to match the values that are in your data. Freeze started you in the right direction by suggesting PROC FREQ. However, try switching the WHERE statement to a more informative version:

where left(upcase(class)) in ('BENZO', 'OPIOID');
Anita3
Fluorite | Level 6

Thank you so much!!! Your suggestions worked!!!

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