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

I have a data set as follows:

 

Date    Var1 Var2 Var3 .... Var3000

10/25   5        2       1             8

10/24

10/23

10/22

10/21   2       4        2             6

10/20

10/19

10/18

10/17   4       5        1             7

 

I would like to replace the missing values with the previous values across all 3000 variables. I have successfully done it for one variable with the following code:

 

 

data want;
 set have;
 retain temp;
 if not missing(Var1) then temp = Var1;
 else Var1 = temp;
 drop temp;
run;

 

 

Can one maybe do this in an array?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

You're thinking along the right lines.  It's easier if you define a temporary array.  Then the elements are automatically retained, and never become part of the output.  For example:

 

data want;

set have;

array real {3000} var1-var3000;

array latest {3000} _temporary_;

do _n_=1 to 3000;

   if real{_n_} > . then latest{_n_} = real{_n_};

   else real{_n_} = latest{_n_};

end;

run;

View solution in original post

3 REPLIES 3
Astounding
PROC Star

You're thinking along the right lines.  It's easier if you define a temporary array.  Then the elements are automatically retained, and never become part of the output.  For example:

 

data want;

set have;

array real {3000} var1-var3000;

array latest {3000} _temporary_;

do _n_=1 to 3000;

   if real{_n_} > . then latest{_n_} = real{_n_};

   else real{_n_} = latest{_n_};

end;

run;

StephanDup
Fluorite | Level 6

Thank you so much. It worked!! I can't tell you how long I struggled with that.

Ksharp
Super User


data have;
infile cards truncover;
retain id 1;
input Date $   Var1 Var2 Var3  Var3000;
cards;
10/25   5        2       1             8
10/24
10/23
10/22
10/21   2       4        2             6
10/20
10/19
10/18
10/17   4       5        1             7
;
run;

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4232 views
  • 1 like
  • 3 in conversation