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

 

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

One way to handle that:

 

if first.hpbpid then do;
   baseline_value = glucose;
   count = 1;
end;
else count + 1;

 

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

what is your expected/wanted output for the sample? plz

Astounding
PROC Star

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.)

 

 

superbibi
Obsidian | Level 7

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.

 

 

Astounding
PROC Star

One way to handle that:

 

if first.hpbpid then do;
   baseline_value = glucose;
   count = 1;
end;
else count + 1;

 

superbibi
Obsidian | Level 7

Excellent! Thank you very much!

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
  • 5 replies
  • 1901 views
  • 1 like
  • 3 in conversation