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

Hi all,  

 

I have data where in some records visit 1 is missing, meaning I need SAS to set the baseline value to the first observation that has an actual value. Here is my data +code:

data WORK.test;

input record visit orange apple;
CARDS;
1 1  1 .        
1 2  1 1
1 3  1 1
1 4  0 1
1 5  0 3
1 6  0 0 
1 7  0 0
2 1  2 .
2 2  0 .
2 3  0 1
2 4  0 0
;
run;
 
data test2; set test; by record visit; retain baseline_apple; if (first.record) and apple ne . then baseline_apple=apple;

When I run this, it only sets baseline when the value isn't missing which is good, but I can't figure out how to tell it to use the next observation (if also not missing ) as the baseline and retain that baseline value for the rest of the visits.  (baseline gets set for each record ).

output: 

schatr2_0-1637250407740.png

What I want is this: 

record visit apple baseline
1 1 . .
1 2 1 1
1 3 1 1
1 4 1 1
1 5 3 1
2 1 . .
2 2 . .
2 3 1 1

if I use "else baseline=apple" it won't retain the value across visits.

 

I dont wan't to use a statement like where apple ne . because it will delete the entire row and I could lose data for other variables in my data set.

Thank you!!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Please show your code - that seems like the result without a RETAIN statement. 
Note that if you have baseline already in your data set and it's not a new variable you may also see this type of behaviour. 

But if you use your sample data and the code provided it seems to generate the exact results expected, is that correct?

 

 

data have;
infile cards dlm='09'x dsd truncover;
input record	visit	apple	baseline;
cards;
1	1	.	.
1	2	1	1
1	3	1	1
1	4	1	1
1	5	3	1
2	1	.	.
2	2	.	.
2	3	1	1
;;;
run;


data want;
set have;
by record visit;
retain baseline_apple;
if first.record then call missing(baseline_apple);

if missing(baseline_apple) then baseline_apple = apple;

run;

proc print;run;

@393310 wrote:

@Reeza 

 

That almost works except it does not retain the baseline across the rest of the visits. Any idea how I could get it to retain? 

 

output with your code:

schatr2_1-1637255009175.png

 


 

View solution in original post

4 REPLIES 4
Reeza
Super User
 data test2;

 set test;

 by record visit;
 
 retain baseline_apple;
 
if first.record then call missing(baseline_apple);

if missing(baseline_apple) then baseline_apple = apple;

run;
 

 

I dont wan't to use a statement like where apple ne . because it will delete the entire row and I could lose data for other variables in my data set.

Thank you!!


I think you're referring to a subsetting IF? You can control the behaviour on the condition so it does not delete rows.

 


@393310 wrote:

Hi all,  

 

I have data where in some records visit 1 is missing, meaning I need SAS to set the baseline value to the first observation that has an actual value. Here is my data +code:

data WORK.test;

input record visit orange apple;
CARDS;
1 1  1 .        
1 2  1 1
1 3  1 1
1 4  0 1
1 5  0 3
1 6  0 0 
1 7  0 0
2 1  2 .
2 2  0 .
2 3  0 1
2 4  0 0
;
run;
 
data test2; set test; by record visit; retain baseline_apple; if (first.record) and apple ne . then baseline_apple=apple;

When I run this, it only sets baseline when the value isn't missing which is good, but I can't figure out how to tell it to use the next observation (if also not missing ) as the baseline and retain that baseline value for the rest of the visits.  (baseline gets set for each record ).

output: 

schatr2_0-1637250407740.png

What I want is this: 

record visit apple baseline
1 1 . .
1 2 1 1
1 3 1 1
1 4 1 1
1 5 3 1
2 1 . .
2 2 . .
2 3 1 1

if I use "else baseline=apple" it won't retain the value across visits.

 

I dont wan't to use a statement like where apple ne . because it will delete the entire row and I could lose data for other variables in my data set.

Thank you!!


 

393310
Obsidian | Level 7

@Reeza 

 

That almost works except it does not retain the baseline across the rest of the visits. Any idea how I could get it to retain? 

 

output with your code:

schatr2_1-1637255009175.png

 

Reeza
Super User

Please show your code - that seems like the result without a RETAIN statement. 
Note that if you have baseline already in your data set and it's not a new variable you may also see this type of behaviour. 

But if you use your sample data and the code provided it seems to generate the exact results expected, is that correct?

 

 

data have;
infile cards dlm='09'x dsd truncover;
input record	visit	apple	baseline;
cards;
1	1	.	.
1	2	1	1
1	3	1	1
1	4	1	1
1	5	3	1
2	1	.	.
2	2	.	.
2	3	1	1
;;;
run;


data want;
set have;
by record visit;
retain baseline_apple;
if first.record then call missing(baseline_apple);

if missing(baseline_apple) then baseline_apple = apple;

run;

proc print;run;

@393310 wrote:

@Reeza 

 

That almost works except it does not retain the baseline across the rest of the visits. Any idea how I could get it to retain? 

 

output with your code:

schatr2_1-1637255009175.png

 


 

393310
Obsidian | Level 7
Sorry I must've left something out. I re ran again and it retained. Thank you so much!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 1354 views
  • 2 likes
  • 2 in conversation