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

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
mkeintz
PROC Star

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

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

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

--------------------------
harrylui
Obsidian | Level 7
thank you so much

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 588 views
  • 0 likes
  • 2 in conversation