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

Hi everyone, 

 

I am having an issue with my retain statement. Basically I am trying to compare values of my outcome at follow up visits to their baseline (visit 1). The value for each outcome is labeled under the variable "noformat". When I run the following code, it does not retain the baseline value I created when it comes across the same outcome at the follow up visit. 

(this is just some example data for simplicity)

data WORK.NEW2;
input record visit outcome $6. noformat;
CARDS;
1 1 apple 0
1 1 orange 0 
1 2 apple 2
;
run; data work.new3; set work.new2; by record visit outcome ;
retain baseline_outcome;
if first.outcome then baseline_outcome=noformat;
run;

This is the output: 

schatr2_0-1636941996305.png

I would like it to look like this: 

recordvisitnoformatoutcomebaseline_outcome
110apple0
110orange0
121apple0

Why is my code not retaining the value for baseline outcome at follow up visits?

 

1 ACCEPTED SOLUTION

Accepted Solutions
393310
Obsidian | Level 7

@Reeza 

@Tom @andreas_lds @ChrisNZ 

 

Hi all, 

 

Thanks so much for helping me think through this. I actually found the problem! Turns out that sorting by "record visit outcome" was causing issues and sorting by just "record outcome" gave me what I wanted. 

 

Thanks again!!

View solution in original post

9 REPLIES 9
Reeza
Super User

The FIRST. should be on the variable that uniquely identifies each group that you want to reset it. 

 

You're currently resetting it at every time your outcome variable changes, when it seems like you want to retain it for the Record (not Visit if you want it at the second visit as well). 

 

I think you likely need FIRST.RECORD instead but it's hard to tell because you've only posted one record. 

 

data WORK.NEW2;
input record visit outcome $6. noformat;
CARDS;
1 1 apple 0
1 1 orange 0 
1 2 apple 2
;
run;
 
data work.new3;
set work.new2; 
by record visit outcome ; 

retain baseline_outcome;

if first.record  then baseline_outcome=noformat;

run;

@393310 wrote:

Hi everyone, 

 

I am having an issue with my retain statement. Basically I am trying to compare values of my outcome at follow up visits to their baseline (visit 1). The value for each outcome is labeled under the variable "noformat". When I run the following code, it does not retain the baseline value I created when it comes across the same outcome at the follow up visit. 

(this is just some example data for simplicity)

data WORK.NEW2;
input record visit outcome $6. noformat;
CARDS;
1 1 apple 0
1 1 orange 0 
1 2 apple 2
;
run; data work.new3; set work.new2; by record visit outcome ;
retain baseline_outcome;
if first.outcome then baseline_outcome=noformat;
run;

This is the output: 

schatr2_0-1636941996305.png

I would like it to look like this: 

record visit noformat outcome baseline_outcome
1 1 0 apple 0
1 1 0 orange 0
1 2 1 apple 0

Why is my code not retaining the value for baseline outcome at follow up visits?

 


 

393310
Obsidian | Level 7

Hi @Reeza

 

Thanks for your quick response. I added a couple more records to hopefully help clarify. So I tried first.record first but didn't get the right results so I switched to first.outcome. Here is the output when I use first.record:

data WORK.NEW2;
input record visit outcome $6. noformat;
CARDS;
1 1 apple 0         
1 1 orange 1
1 2 apple 1 
1 2 orange 0
2 1 apple 1
2 2 apple 0
2 3 apple 2
3 1 orange 0  
3 2 orange 3
3 3 orange 1      
;
run;
data work.new3;
 set work.new2; 
 by record visit outcome ; 
 retain baseline_outcome;
 if first.record  then baseline_outcome=noformat;
run;

 

schatr2_0-1636944911169.png

so for record 1 when it sees outcome orange at visit 1 it should assign baseline_outcome as 1 but the retain statement gives it the same value as apple at visit 1. I was thinking maybe it would need to be if first.outcome and first.record then baseline_outcome=noformat, but that doesn't seem to work either. 

 

I appreciate your help!!

ChrisNZ
Tourmaline | Level 20

It seems to me that you want this?

if first.record then baseline_outcome=noformat;
393310
Obsidian | Level 7

@ChrisNZ 

 

Hi ChrisNZ, 

 

I tried that already! If you look at my reply to Reeza (I mentioned you in a reply so you know what I'm referring to!),  that method didn't give me exactly what I'm looking for unfotunately.

393310
Obsidian | Level 7
andreas_lds
Jade | Level 19

Sorry, but i don't understand the logic. Please clarify when "baseline_income" should change its value, and it should retain its value.

393310
Obsidian | Level 7


So for this data I have multiple outcomes recorded for each visit and multiple visits for each patient. I want sas to read record 1, visit 1, outcome 1(apple) and then set a baseline value. When it sees the same outcome for the same record, but a new visit, (record 1, visit 2, outcome 1(apple) it retains the value it created in visit 1. The problem that I'm having is that when it comes across a new outcome (orange) it sets the baseline of orange as the baseline of apple despite being two different outcomes.

 

So what I want is this:

recordvisitnoformatoutcomebaseline 
110apple0
121apple0
111orange
122orange1

 

What I'm getting with my code is this:

recordvisitnoformatoutcomebaseline 
110apple0
121apple0
111orange
122orange0
Tom
Super User Tom
Super User

Sort the data by RECORD OUTCOME VISIT

Then you can detect which visit is the first visit for "apple" by using FIRST.OUTCOME.

Make sure the BASELINE is not already a variable in the input dataset, otherwise the value retained will be overwritten when the next observation is read.

 

data have;
  input record visit noformat outcome $ ;
cards;
1 1 0 apple
1 2 1 apple
1 1 1 orange
1 2 2 orange
;

data want;
  set have ;
  by record outcome visit;
  if first.outcome then baseline=noformat;
  retain baseline;
run;
393310
Obsidian | Level 7

@Reeza 

@Tom @andreas_lds @ChrisNZ 

 

Hi all, 

 

Thanks so much for helping me think through this. I actually found the problem! Turns out that sorting by "record visit outcome" was causing issues and sorting by just "record outcome" gave me what I wanted. 

 

Thanks again!!

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
  • 9 replies
  • 775 views
  • 2 likes
  • 5 in conversation