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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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