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

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

My97_0-1613083317256.png

Could you please help me to sort it out?

Many thanks and warm regards.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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.  

--------------------------
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

4 REPLIES 4
ballardw
Super User

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.

Phil_NZ
Barite | Level 11
Hi @ballardw,

You are totally correct about that part.
Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
mkeintz
PROC Star

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.  

--------------------------
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

--------------------------
Phil_NZ
Barite | Level 11

Hi @mkeintz 

An outstanding move, thank you very much for optimizing my code.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 799 views
  • 7 likes
  • 3 in conversation