Hi SAS Users,
Happy Lunar New Year!
This morning I try to perform lag of a variable called "s39" in my dataset. However, when I do proc means to cross check, the results seems not properly.
It is my code
data merge2_fix;
set merge2_sample;
lags39=ifn(first.type,.,lag(s39));
if n(s39,lags39)=2 then
del_exc_rat= s39-lags39;
run;
My data is attached below, but I will show a small part here for visualization
Type Year s39
131566 1988 0.0008752604
131566 1989 0.0423339608
131566 1990 0.4875890833
131566 1991 0.9535544167
131566 1992 0.9906416667
131566 1993 0.9989458333
131566 1994 0.9990083333
131566 1995 0.99975
131566 1996 0.9996625
131566 1997 0.9995
131566 1998 0.9995
131566 1999 0.9995
131566 2000 0.9995
131566 2001 0.9995
131566 2002 3.0632566667
131566 2003 2.9006291667
131566 2004 2.9233008189
131566 2005 2.9036575
131566 2006 3.0543133333
131566 2007 3.0956488492
131566 2008 3.1441645599
131566 2009 3.7101068305
131566 2010 3.8962951545
131566 2011 4.1101395762
131566 2012 4.5369343602
131566 2013 5.4593526647
131566 2014 .
131566 2015 .
131566 2016 .
131566 2017 .
131566 2018 .
131566 2019 .
131879 1988 0.0008752604
131879 1989 0.0423339608
131879 1990 0.4875890833
131879 1991 0.9535544167
131879 1992 0.9906416667
So, Type and Year are character variables when s39 is a numeric variable. The year is from 1988 to 2019.
From what I performed above, the lag39 variable should have less observation than that of s39. However, when I run PROC MEANS, the number of observation of these two variables are totally the same
proc means data=merge2_fix n nmiss mean min max;
var s39 lags39 del_exc_rat;
title " Merge2_fix";
run;
The result is here, with the abnormal result is yellowed
Could you please help me to sort it out?
Many thanks and warm regards.
BTW, you don't need the lag function if you are really only looking for the deltas:
Instead of:
lags39=ifn(first.type,.,lag(s39));
if n(s39,lags39)=2 then
del_exc_rat= s39-lags39;
you can
del_exc_rat=ifn(first.type,.,dif(s39));
DIF(x) is defined as X-lag(X). It will not generate notes when S39 and/or lag(S39) is missing.
You are attempting to use the First.type without a BY Type statement.
So First.type is NEVER true.
The log likely shows:
NOTE: Variable first.type is uninitialized.
Or show the LOG with the Merge2_fix code executed.
BTW, you don't need the lag function if you are really only looking for the deltas:
Instead of:
lags39=ifn(first.type,.,lag(s39));
if n(s39,lags39)=2 then
del_exc_rat= s39-lags39;
you can
del_exc_rat=ifn(first.type,.,dif(s39));
DIF(x) is defined as X-lag(X). It will not generate notes when S39 and/or lag(S39) is missing.
Hi @mkeintz
An outstanding move, thank you very much for optimizing my code.
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: