DATA Step, Macro, Functions and more

Help With Adding Up Costs And Visits

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Help With Adding Up Costs And Visits

Hi everybody,

I have data on dates and costs of visits, and I need to add them up over individuals seperated into months.

Dummy data is something like this:

Have:

Id            Date            Cost       Visit_id

100       10sep2005     1000       98

105       20sep2005     3000       103

105       22sep2005     1000       105

200       01oct2005      2000       123

200       03nov2005     1500        201

200       03nov2005      500         202

Want:

Id          Month          Visits          Cost

100       sep2005         1             1000

105       sep2005         2             4000

200       oct2005          1             2000

200       nov2005          2             2000

I've tried using the ".first" and ".last" properties when creating a loop, but I can't figure out exactly how to do it.

Any help would be greatly appreciated.         


Accepted Solutions
Solution
‎11-14-2011 10:32 AM
PROC Star
Posts: 7,467

Re: Help With Adding Up Costs And Visits

While the sql solutions will give you what you want, you did ask how you might be able to do it in a datastep.  Here is one way:

data want;

  set have;

  month=put(date,monyy7.);

run;

data want (drop=date visit_id in_cost);

  do until (lastone);

    set want (rename=(cost=in_cost)) end=lastone;

    by id month notsorted;

    if first.month then do;

      cost=in_cost;

      visits=1;

    end;

    else do;

      cost+in_cost;

      visits+1;

    end;

    if last.month then output;

  end;

run;

View solution in original post


All Replies
Super User
Posts: 5,424

Help With Adding Up Costs And Visits

First convert your data to a month-year column, perhaps using Put(date,monyy.).

Then you use SQL with group by id and month, and using count(*) for visits.

Or proc summary, where the _FREQ_ output column will be your visits (you can rename in the output statement).

/Linus

Data never sleeps
Super Contributor
Posts: 1,636

Re: Help With Adding Up Costs And Visits

data have;

informat date date9. ;

format date date9. month monyy7.;

input id date cost visit_id;

month=input(put(date,monyy7.),monyy7.);

cards;

100 10sep2005     1000       98

105 20sep2005     3000       103

105 22sep2005     1000       105

200 01oct2005     2000       123

200 03nov2005     1500       201

200 03nov2005      500       202

;

run;

proc sql;

create table want as select id, month, count(*) as visits,sum(cost) as cost

from have

group by id,month

order by id,month;

quit;

Solution
‎11-14-2011 10:32 AM
PROC Star
Posts: 7,467

Re: Help With Adding Up Costs And Visits

While the sql solutions will give you what you want, you did ask how you might be able to do it in a datastep.  Here is one way:

data want;

  set have;

  month=put(date,monyy7.);

run;

data want (drop=date visit_id in_cost);

  do until (lastone);

    set want (rename=(cost=in_cost)) end=lastone;

    by id month notsorted;

    if first.month then do;

      cost=in_cost;

      visits=1;

    end;

    else do;

      cost+in_cost;

      visits+1;

    end;

    if last.month then output;

  end;

run;

Super User
Posts: 10,018

Re: Help With Adding Up Costs And Visits

data have;
input id date : date9. cost visit_id;
cards;
100 10sep2005     1000       98
105 20sep2005     3000       103
105 22sep2005     1000       105
200 01oct2005     2000       123
200 03nov2005     1500       201
200 03nov2005      500       202
;
run;
proc summary data=have nway;
 class id date;
 format date monyy7.;
 var cost ;
 output out=want(drop=_:) n=visit sum=;
run;


Ksharp

Occasional Contributor
Posts: 8

Re: Help With Adding Up Costs And Visits

Thanks for all the help, guys. Highly appreciated, I see there are many ways to do this. I have a lot to learn!

Valued Guide
Posts: 2,177

Re: Help With Adding Up Costs And Visits

like the art297 way ....

... to "gild that lilly"

data want (keep= id month visits cost );

   call missing( cost ) ;

   do visits=1 by 1 until (last.month);

      set want (rename=(cost=in_cost))  ;

      by id month notsorted;

      cost + in_cost  ;

end;

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 200 views
  • 10 likes
  • 6 in conversation