Hi Friends,
I have been trying to find the baseline value (say glucose) within one ID (hpbpid) using retain, by, etc. I am using SAS studio. The program just kept running without producing an output. I am wondering if there is endless loop in the code as below.
I have searched the sas communities, google, etc. I know there are many other ways to get the baseline value such as pro sql. I just want to know why my method does not work.
Thank you.
Obs | hpbpid | date | glucose |
1 | 19270818FM-B | 16MAR2009 | 5 |
2 | 19270818FM-B | 18JAN2011 | 5 |
3 | 19390310FMGL | 07APR2008 | 5 |
4 | 19390310FMGL | 24NOV2008 | 5 |
5 | 19390310FMGL | 11MAR2009 | 6 |
6 | 19390325FCLB | 01MAY2008 | 5 |
7 | 19390325FCLB | 17MAR2009 | 5 |
8 | 19390507MJEM | 14MAY2008 | 5 |
9 | 19390507MJEM | 22SEP2008 | 0 |
10 | 19391013ME-D | 11MAR2008 | 5 |
proc sort data = hpbp.lab_bio_demo;
by hpbpid date;
run;
data visit;
set hpbp.lab_bio_demo;
by hpbpid;
retain baseline_value;
if first.hpbpid then baseline_value = glucose;
do until (last.hpbpid = 1);
baseline_value +0;
end;
run;
One way to handle that:
if first.hpbpid then do;
baseline_value = glucose;
count = 1;
end;
else count + 1;
what is your expected/wanted output for the sample? plz
Yes, your DO loop is endless. Nothing inside the loop reads in any observations, so you can never get to the last.hpbpid observation.
The solution is simple. Get rid of the DO loop and everything related to it. The last statement in the DATA step should be:
if first.hpbpid then baseline_value = glucose;
(Don't remove the RUN statement, however.)
Hi Astounding,
The code works great! Thank you so so much!
May I ask one more question regarding the do loop? If I want to generate a count column from 1 to the number of the last.id using the do loop, how should I modify the do loop code? For example, in the example dateset, there are two duplicates in the first ID, then the count should be 1 and 2. Thank you.
One way to handle that:
if first.hpbpid then do;
baseline_value = glucose;
count = 1;
end;
else count + 1;
Excellent! Thank you very much!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.