Obsidian | Level 7

## Look up to upper value

good day all,

here is  my data set

Obs Date rownum
1 01APR2021 1
2 . 2
3 02APR2021 3
4 05APR2021 4
5 06APR2021 5
6 07APR2021 6
7 . 7
8 08APR2021 8
9 09APR2021 9
10 12APR2021 10
11 . 11
12 13APR2021 12
13 14APR2021 13
14 15APR2021 14
15 16APR2021 15
16 19APR2021 16
17 20APR2021 17
18 21APR2021 18
19 22APR2021 19
20 23APR2021 20
21 26APR2021 21
22 27APR2021 22
23 28APR2021 23
24 29APR2021 24
25 30APR2021 25

for the missing value, i want to write a program for auto look up the upper value.

for the example Obs 2 will equal 01APR2021. i know a function called retain would do . But can i use if then for my question?  if date is missing , then date =  date (current rownum-1).  if it is possible, i can be more creative in my future program. thanks you

Regards,

Harry

1 ACCEPTED SOLUTION

Accepted Solutions
PROC Star

## Re: Look up to upper value

Here is a way to use an IF test, via the IFN function:

``````data want;
set have;
date=ifn(date=.,lag(date),date);
run;``````

But this only replaces all missing date values when there are never two consecutive missing date values.

A more general solution:

``````data want (drop=_:);
set have (rename=(date=_check));
if _check ^=. then set have point=_n_;
run;
``````

This relies on the fact that variables read via a SET statement are automatically retained - until another SET reads in the same variable, which usually happens with every incoming obs.  But the conditional set statement above (if _check^=. then set have point=_n_) only reads non-missing values for DATE and automatically retains that value.  And unlike the first example, this would propagate non-missing DATE values over multiple consecutive observations with missing values for _CHECK.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
2 REPLIES 2
PROC Star

## Re: Look up to upper value

Here is a way to use an IF test, via the IFN function:

``````data want;
set have;
date=ifn(date=.,lag(date),date);
run;``````

But this only replaces all missing date values when there are never two consecutive missing date values.

A more general solution:

``````data want (drop=_:);
set have (rename=(date=_check));
if _check ^=. then set have point=_n_;
run;
``````

This relies on the fact that variables read via a SET statement are automatically retained - until another SET reads in the same variable, which usually happens with every incoming obs.  But the conditional set statement above (if _check^=. then set have point=_n_) only reads non-missing values for DATE and automatically retains that value.  And unlike the first example, this would propagate non-missing DATE values over multiple consecutive observations with missing values for _CHECK.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Obsidian | Level 7

## Re: Look up to upper value

thank you so much
Discussion stats
• 2 replies
• 524 views
• 0 likes
• 2 in conversation