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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

6 REPLIES 6
art297
Opal | Level 21

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

 

RoWa
Calcite | Level 5

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

Ksharp
Super User

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;
art297
Opal | Level 21

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?

 

Ksharp
Super User

Yes. Always.

RoWa
Calcite | Level 5

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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
  • 6 replies
  • 956 views
  • 0 likes
  • 3 in conversation