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
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.
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.
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!
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.