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

Hi PFA attached sample data.

 

I am trying to impute some values (name of output variable 'desired_Imp_val') based on input variable  'Value' . 

 

I need to derive two imputations as below. Please advise. 

 

1.Within a section, If a ID mastered an item and received score of 2 and all items prior to this item have no scores, all prior scores will be imputed as 2 for this visit.

 

2. If a ID had 3 consecutive 0 scores in a visit and no values for higher number items in this section for this visit, the higher number items will be imputed with score of 0. 

 

Above imputations must be visit and section specific. 

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

See this:

data want1;
set comm_q;
by id visit section;
retain three_zero counter;
if first.section
then do;
  three_zero = 0;
  counter = 0;
end;
if value = 0
then do;
  counter + 1;
  if counter ge 3 then three_zero = 1;
end;
else counter = 0;
if value = .
then do;
  if three_zero then desired = 0;
end;
else do;
  desired = value;
  if value ne 0
  then do;
    three_zero = 0;
    counter = 0;
  end;
end;
drop counter three_zero;
run;

proc sort data=want1;
by id visit section descending section_id;
run;

data want2;
set want1;
by id visit section;
retain had_two;
if first.section then had_two = 0;
if value = 2 then had_two = 1;
if value = .
then do;
  if had_two and desired = . then desired = 2;
end;
else if value ne 2 then had_two = 0;
drop had_two;
run;

proc sort data=want2;
by id visit section section_id;
run; 

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Post usable example data, as shown here https://communities.sas.com/t5/Statistical-Procedures/Assign-value-from-any-row-by-ID/m-p/704640/hig... in an answer to one of your previous questions. Excel files are mostly useless for representing SAS datasets, and many of us can't even download them to our SAS environments because our corporate firewalls block them for security reasons.

bharath86
Obsidian | Level 7

I have uploaded the data as a sas dataset. Please let me know if this helps. 

Kurt_Bremser
Super User

See this:

data want1;
set comm_q;
by id visit section;
retain three_zero counter;
if first.section
then do;
  three_zero = 0;
  counter = 0;
end;
if value = 0
then do;
  counter + 1;
  if counter ge 3 then three_zero = 1;
end;
else counter = 0;
if value = .
then do;
  if three_zero then desired = 0;
end;
else do;
  desired = value;
  if value ne 0
  then do;
    three_zero = 0;
    counter = 0;
  end;
end;
drop counter three_zero;
run;

proc sort data=want1;
by id visit section descending section_id;
run;

data want2;
set want1;
by id visit section;
retain had_two;
if first.section then had_two = 0;
if value = 2 then had_two = 1;
if value = .
then do;
  if had_two and desired = . then desired = 2;
end;
else if value ne 2 then had_two = 0;
drop had_two;
run;

proc sort data=want2;
by id visit section section_id;
run; 
bharath86
Obsidian | Level 7
Thank you very much.

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

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
  • 4 replies
  • 1051 views
  • 1 like
  • 2 in conversation