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

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
Obsidian | Level 7

Thank you both

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 880 views
  • 3 likes
  • 3 in conversation