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

Hello, 

 

I have an UNBALANCED PANEL  of clients (i.e. not all clients are present on every year) for four years (2010-2013). I would like to calculate the change in costs over the years. The problem is that when I lag the variable "costs", SAS lags over all client numbers (i.e. 2013 of client1 will become 2010 for client2). How do I prevent this from happening?

 

I can't just take out year=2010, because for some clients 2011 was the first year.

 

This is what I have so far:

 

data costchange;

set sample;

by cient;

lcost=lag(cost);

cost_score = (cost - lcost)/lcost;

where treated=1;

 

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

When using LAG, it's important to make sure it executes on every observation.  You're doing that ... so far so good.

 

The trick is to wipe out the value for observations that begin a new CLIENT.  So:

 

data costchange;

set sample;

by cient;

lcost=lag(cost);

if first.client then lcost = . ;

cost_score = (cost - lcost)/lcost;

where treated=1;

run;

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

As always, some test data in the form of a datastep would help clarify what you mean.  At a guess, maybe use retain'd variables and set them once per group, then when missing use the retained variable.

Astounding
PROC Star

When using LAG, it's important to make sure it executes on every observation.  You're doing that ... so far so good.

 

The trick is to wipe out the value for observations that begin a new CLIENT.  So:

 

data costchange;

set sample;

by cient;

lcost=lag(cost);

if first.client then lcost = . ;

cost_score = (cost - lcost)/lcost;

where treated=1;

run;

GKati
Pyrite | Level 9

Perfect! Just what I was looking for. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1213 views
  • 0 likes
  • 3 in conversation