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.
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;
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
hello Art, Thank you so very much, its very educational.
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;
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?
Yes. Always.
thank you, it worked perfectly, thank you again like always.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.