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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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