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

Please see below for the code I used to calculate AUC. The code works fine and the AUC calculation is accurate. The issue is however, I was expecting a "." for the first observation of each ID. However, from the second ID, the first observation of each ID returns a negative value instead of missing ("."). Does anyone know how to fix this issue without me having to give this statement 'if auc<0 then auc=.' ? Thank you so much.

 

SAS Code I used:

data newdata.trial1ab;
set check4;
retain StudyID_Old;
if (_N_ = 1) then StudyID_Old = study_ID;
if (study_ID ne StudyID_Old) then
do;
x_1=.;
y_1 = .;
StudyID_Old = study_ID;
end;
x = dep_new_days;
y = Mean_Hf;
xx = x;
lny = log(y);
x_1 = lag(x);
y_1 = lag(y);
x_xx = x - x_1;
cc = ( y + y_1 ) / 2;
auc = cc * x_xx;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Because the lag function is a queue  update function, not a "lookback", you need to update the queue with every observations, but use  a missing value when at the beginning of a by  group.   But your code only updates the queue when not at the start of a new id.

 

You need something like:

 

data want;

   set have;

   by studyid;

   x1=ifn(first.studyid,.,lag(x);

   y1=ifn(first.studyid,.,lag(y);

 

The ifn function will always update the lag queue, but  will return a dot when at the start of an id.

 

You should  be able to take it from there.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
mkeintz
PROC Star

Because the lag function is a queue  update function, not a "lookback", you need to update the queue with every observations, but use  a missing value when at the beginning of a by  group.   But your code only updates the queue when not at the start of a new id.

 

You need something like:

 

data want;

   set have;

   by studyid;

   x1=ifn(first.studyid,.,lag(x);

   y1=ifn(first.studyid,.,lag(y);

 

The ifn function will always update the lag queue, but  will return a dot when at the start of an id.

 

You should  be able to take it from there.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Tom
Super User Tom
Super User

You are very close, but pay attention to the order of  your statements in the data step.

You need to calculate the lagged values, then make the decision of whether they are valid or not because of by group transition.

data newdata.trial1ab;
  set check4;
  retain StudyID_Old;
  if (_N_ = 1) then StudyID_Old = study_ID;
  x = dep_new_days;
  y = Mean_Hf;
  xx = x;
  lny = log(y);
  x_1 = lag(x);
  y_1 = lag(y);
  if (study_ID ne StudyID_Old) then do;
    x_1=.;
    y_1 = .;
    StudyID_Old = study_ID;
  end;
  else do;
    x_xx = x - x_1;
    cc = ( y + y_1 ) / 2;
    auc = cc * x_xx;
  end;
run;

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
  • 1769 views
  • 1 like
  • 3 in conversation