DATA Step, Macro, Functions and more

DIF / LAG function question. Please help

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

DIF / LAG function question. Please help

Hello,

 

What I have here is the date set looks like the following:

ID      Month      Year-to-date       difference

01      201601    100                     100

01      201602    105                     5

01      201603    135                     30

01      201604    205                     70

01      201605    205                     0

01      201606    205                     0

01      201607    205                     0

01      201608    205                     0

01      201609    255                     50

01      201610    255                     0

01      201611    255                     0

01      201612    255                     0

 

02      201601    300                     300

02      201602    405                     105

02      201603    405                     0

02      201604    435                     30

.

.

.

.

01      201611    435                     0

01      201612    455                     20

 

 

as you can see it comes with different ID and different month, so I would like to get the difference from the year-to-date, but could not get any success.

 

my code is:

 

data want;

set have;

if Month='201601' then difference=(year-to-date-0);

else do;

   difference = dif(year-to-date);

end;

run;

 

My result gave me negative number in 201602, and I could not understand it. Please help. Thx.


Accepted Solutions
Solution
‎03-02-2017 12:05 AM
Super User
Posts: 9,681

Re: DIF / LAG function question. Please help

Arthur.T may want say this.

 

data want;
  set have;
by id;
    difference = dif(year-to-date);
    if first.id then difference=(year_to_date);
run;

View solution in original post


All Replies
PROC Star
Posts: 7,363

Re: DIF / LAG function question. Please help

I don't have any data to test it on, but does your variable name really contain dashes? They're invalid unless you have the option set to allow non-conforming variable names.

 

Other than that, I think diff works like lag and shouldn't be used conditionally. Possibly something like the following would produce what you want:

 

data want;
  set have;
    difference = dif(year-to-date);
    if Month='201601' then difference=(year_to_date);
run;

Art, CEO, AnalystFinder.com

 

New Contributor
Posts: 4

Re: DIF / LAG function question. Please help

hello Art, Thank you so very much, its very educational.

Solution
‎03-02-2017 12:05 AM
Super User
Posts: 9,681

Re: DIF / LAG function question. Please help

Arthur.T may want say this.

 

data want;
  set have;
by id;
    difference = dif(year-to-date);
    if first.id then difference=(year_to_date);
run;
PROC Star
Posts: 7,363

Re: DIF / LAG function question. Please help

Xia,

 

In this case they appear to be one and the same. However, I agree, your suggestion would also be correct.

 

Art, CEO, AnalystFinder.com

 

p.s. You're back to working with SAS?

 

Super User
Posts: 9,681

Re: DIF / LAG function question. Please help

Yes. Always.

New Contributor
Posts: 4

Re: DIF / LAG function question. Please help

thank you, it worked perfectly, thank you again like always.

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 112 views
  • 0 likes
  • 3 in conversation