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:
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?
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!!
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:
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?
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;
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!!
It seems to me that you want this?
if first.record then baseline_outcome=noformat;
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.
Sorry, but i don't understand the logic. Please clarify when "baseline_income" should change its value, and it should retain its value.
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:
record | visit | noformat | outcome | baseline |
1 | 1 | 0 | apple | 0 |
1 | 2 | 1 | apple | 0 |
1 | 1 | 1 | orange | 1 |
1 | 2 | 2 | orange | 1 |
What I'm getting with my code is this:
record | visit | noformat | outcome | baseline |
1 | 1 | 0 | apple | 0 |
1 | 2 | 1 | apple | 0 |
1 | 1 | 1 | orange | 0 |
1 | 2 | 2 | orange | 0 |
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;
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!!
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.
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.