Help using Base SAS procedures

How to use lag function recursively

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

How to use lag function recursively

Hi,

I have a huge data with a lot of missing values for a variable.  For example,

Data a;

     input x y;

     cards;

1 1

1 0

1 .

1 .

2 2

2 3

2 9

2 .

2 .

;

run;

Now I want to fill in the missing y with its lagged value. That is, for x=1, y=0 for the missing ones and x=2, y=9 for the last two observations. But when I use lag function, it only fills in the first missing observation.

Can anyone please tell me how to do this? Please note, the data is large. So it's better to avoid open/close data too many times.

Thanks,

Sunny


Accepted Solutions
Solution
‎11-13-2014 10:50 PM
Trusted Advisor
Posts: 1,204

Re: How to use lag function recursively

data want(drop=y rename=(y1=y));

set a;

retain y1;

if y>. then y1=y;

run;

View solution in original post


All Replies
Respected Advisor
Posts: 4,646

Re: How to use lag function recursively

It is probably simpler than you thought with UPDATE processing:

data want;

update a(obs=0) a;

by x;

output;

run;

PG

PG
Solution
‎11-13-2014 10:50 PM
Trusted Advisor
Posts: 1,204

Re: How to use lag function recursively

data want(drop=y rename=(y1=y));

set a;

retain y1;

if y>. then y1=y;

run;

Valued Guide
Posts: 3,208

Re: How to use lag function recursively

The lag function in SAS is a queue functionality and is quite different then the lag en lead functions in Oracle / MS-Sql

For not getting confused avoid using that same name for that.

What you are possible trying to do is known as "impute missings"  as that is part of the mining steps.

Just putting in the previous value is not the best strategy to do that, there are several more approaches .

---->-- ja karman --<-----
Contributor
Posts: 33

Re: How to use lag function recursively

Many thanks all of you!

Thanks,

Sunny

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 457 views
  • 6 likes
  • 4 in conversation