Help using Base SAS procedures

Getting the real LAG of a certain variable!

Reply
N/A
Posts: 0

Getting the real LAG of a certain variable!

How can I resolve the pifall of the lag function?
I mean I want the real lag not not the one created by lag_var = lag (var) !
SAS Super FREQ
Posts: 8,743

Re: Getting the real LAG of a certain variable!

Hi:
Given this data (SASHELP.CLASS):
[pre]
Before Lag

Obs Name Sex Age Height Weight

1 Alice F 13 56.5 84.0
2 Barbara F 13 65.3 98.0
3 Carol F 14 62.8 102.5
4 Jane F 12 59.8 84.5
5 Janet F 15 62.5 112.5
6 Joyce F 11 51.3 50.5
7 Judy F 14 64.3 90.0
8 Louise F 12 56.3 77.0
9 Mary F 15 66.5 112.0
10 Alfred M 14 69.0 112.5
11 Henry M 14 63.5 102.5
12 James M 12 57.3 83.0
13 Jeffrey M 13 62.5 84.0
14 John M 12 59.0 99.5
15 Philip M 16 72.0 150.0
16 Robert M 12 64.8 128.0
17 Ronald M 15 67.0 133.0
18 Thomas M 11 57.5 85.0
19 William M 15 66.5 112.0
[/pre]

...and this program:
[pre]
options ls=80 nocenter nodate nonumber;
proc print data=sashelp.class;
title 'Before Lag';
run;

data lagclass;
set sashelp.class;
by sex;
lagage = lag(age);
if first.sex then lagage = .;
run;

ods listing;
proc print data=lagclass;
title 'After Lag';
var name sex age lagage height weight;
run;
[/pre]

I don't understand what you mean by the "pitfall of the lag function". The above program produces this output:
[pre]
After Lag

Obs Name Sex Age lagage Height Weight

1 Alice F 13 . 56.5 84.0
2 Barbara F 13 13 65.3 98.0
3 Carol F 14 13 62.8 102.5
4 Jane F 12 14 59.8 84.5
5 Janet F 15 12 62.5 112.5
6 Joyce F 11 15 51.3 50.5
7 Judy F 14 11 64.3 90.0
8 Louise F 12 14 56.3 77.0
9 Mary F 15 12 66.5 112.0
10 Alfred M 14 . 69.0 112.5
11 Henry M 14 14 63.5 102.5
12 James M 12 14 57.3 83.0
13 Jeffrey M 13 12 62.5 84.0
14 John M 12 13 59.0 99.5
15 Philip M 16 12 72.0 150.0
16 Robert M 12 16 64.8 128.0
17 Ronald M 15 12 67.0 133.0
18 Thomas M 11 15 57.5 85.0
19 William M 15 11 66.5 112.0

[/pre]

These Tech Support notes and user-group paper have more information:
http://support.sas.com/kb/24/694.html
http://support.sas.com/kb/24/665.html
http://support.sas.com/resources/papers/proceedings09/055-2009.pdf

Perhaps your questions about the LAG function will be answered by these documents.

cynthia
N/A
Posts: 0

Re: Getting the real LAG of a certain variable!

Thanks Cynthia!

Since I have quarterly data, that means I have nearly 44 rows for each company in my sample. For each quarter I need to get the (lag4), which means that I need to set the first 4 rows of each company as (.). I did it now by "if first.company then lag4=."

Appreciate your help though Smiley Happy
Ask a Question
Discussion stats
  • 2 replies
  • 118 views
  • 0 likes
  • 2 in conversation