BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
r3570
Obsidian | Level 7
Hi all,
Please help me with below query
If I have a variable with a test value and corresponding result. I need to copy the same value to other blank observations as below:
Having:
Test Result
A 12.6
A
A
A
A
B 14.9
B
B
B
B
B

Need as below:
Test Result
A 12.6
A 12.6
A 12.6
A 12.6
A 12.6
B 14.9
B 14.9
B 14.9
B 14.9
B 14.9
B 14.9
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Amazingly enough the function is RETAIN, but you want to use it with a new variable.

data have;
  input Test $ Result;
datalines;
A 12.6
A .
A .
A .
A .
B 14.9
B .
B .
B .
B .
B .
;


data want;
   set have;
   by test notsorted;
   retain tempresult;
   if first.test then tempresult=.;
   if not missing(result) then tempresult=result;
   if missing (result) then result=tempresult;
   drop tempresult;
run;

The data step creates a set similar to your description.

The BY statement in the Want data step assumes that your data is grouped by Test value if not actually sorted.

Retain creates a variable that will hold values. If the value is to be character you should define the length before the Retain statement.

The BY statement creates automatic variable that you can test to see if an observation is the first or last of a group. In this case it is used to reset the retained variable when the Test value changes to a new group.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Use a RETAINed variable which you discard later.

data want;
set have;
by test;
retain _result;
if first.test then _result = .;
if result ne .
then _result = result;
else result = _result;
drop _result;
run;
ballardw
Super User

Amazingly enough the function is RETAIN, but you want to use it with a new variable.

data have;
  input Test $ Result;
datalines;
A 12.6
A .
A .
A .
A .
B 14.9
B .
B .
B .
B .
B .
;


data want;
   set have;
   by test notsorted;
   retain tempresult;
   if first.test then tempresult=.;
   if not missing(result) then tempresult=result;
   if missing (result) then result=tempresult;
   drop tempresult;
run;

The data step creates a set similar to your description.

The BY statement in the Want data step assumes that your data is grouped by Test value if not actually sorted.

Retain creates a variable that will hold values. If the value is to be character you should define the length before the Retain statement.

The BY statement creates automatic variable that you can test to see if an observation is the first or last of a group. In this case it is used to reset the retained variable when the Test value changes to a new group.

Ksharp
Super User
data have;
  input Test $ Result;
datalines;
A 12.6
A .
A .
A .
A .
B 14.9
B .
B .
B .
B .
B .
;

data want;
update have(obs=0) have;
by Test;
output;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 495 views
  • 0 likes
  • 4 in conversation