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

Hello all, here is what I have

YearVarValue
20003
20005
2000
200010
20017
20012
2002
20029
2002
20042
2004

I would like to impute the missing values of VarValue for each year with the minimum VarValue for that year. Something like this below. A datastep would be preferable as I do not know SQL (but I am not opposed to SQL as long as I can modify it easily). Thanks!!

YearVarValue
20003
20005
20003
200010
20017
20012
20029
20029
20029
20042
20042
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Here's one fairly standard way to use a DATA step:

data want;

   do until (last.year);

      set have;

      by year;

      minimum_value = min(minimum_value, VarValue);

   end;

   do until (last.year);

      set have;

      by year;

      if VarValue=. then VarValue = minimum_value;

      output;

   end;

   drop minimum_value;

run;

View solution in original post

3 REPLIES 3
Astounding
PROC Star

Here's one fairly standard way to use a DATA step:

data want;

   do until (last.year);

      set have;

      by year;

      minimum_value = min(minimum_value, VarValue);

   end;

   do until (last.year);

      set have;

      by year;

      if VarValue=. then VarValue = minimum_value;

      output;

   end;

   drop minimum_value;

run;

Reeza
Super User

Here's the SQL way.

proc sql;

create table want as

select a.year, a.varvalue, min(a.varvalue) as min, coalesce(a.varvalue, calculated min) as new_varValue

from want a

group by year;

quit;

spirto
Quartz | Level 8

Thank you both

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1568 views
  • 3 likes
  • 3 in conversation