Iterative calculation

Accepted Solution Solved
Reply
Contributor LRN
Contributor
Posts: 57
Accepted Solution

Iterative calculation

Hi,

I have upto 30 datasets. The number of variables changes everymonth, which ranges from 240-260. I have to create a total column  for every eight columns/variables. How can I do that?

Thanks a lot


Accepted Solutions
Solution
‎12-12-2012 04:03 PM
Trusted Advisor
Posts: 1,908

Re: Iterative calculation

Probably a typo on my part

should read

     %let start=%left(%eval((&ii-1)*8+2));

     %let endd=%left(%eval(&ii*8+1));

View solution in original post


All Replies
Trusted Advisor
Posts: 1,908

Re: Iterative calculation

Sounds like using macros would be the way to go.

Contributor LRN
Contributor
Posts: 57

Re: Iterative calculation

Posted in reply to PaigeMiller

Hi,

I tried, But having hard time to figure out how to go about. Any guidance will be greatly helpful.

Thanks

Trusted Advisor
Posts: 1,908

Re: Iterative calculation

Without seeing your data, its probably not possible for me to write meaningful examples.

But what you can do is post your own code, and SASLOG, and the data, and I'm sure people will help you.

Contributor LRN
Contributor
Posts: 57

Re: Iterative calculation

Posted in reply to PaigeMiller

Hi,

Right now, I just hard coded it.

%macro daily_total (dsn=);

data daily_tot_&dsn (keep=Category day1 day2 day3 day4 day5 day6 day7 day8 day9 day10

                    day11 day12 day13 day14 day15 day16 day17 day18 day19 day20 day21 day22 day23 day24

                    day25 day26 day27 day28 day29 day30 day31);

length total 8.;

format total 29.;

set work.&dsn(rename=(f1=Category));

total=sum(of f2-f249);

Day1=sum(OF f2-f9);

Day2=sum(OF f10-f17);

Day3=sum(of f18-f25);

Day4=sum(of f26-f33);

Day5=sum(of f34-f41);

Day6=sum(of f42-f49);.....

Day31=sum(of f242-f249);

Total=sum(of f2-f249);

run;

Not sure, which would be the best approach to go with that.

Thanks

Trusted Advisor
Posts: 1,908

Re: Iterative calculation

%macro daily_total(dsn=,days_in_month=);

data daily_tot_&dsn(keep=category total day1-day&days_in_month);

length total 8; /* this line is completely unnecessary */

format total 29.; /* That's a very wide format */

total=sum(of f2-f%left(%eval(8*&days_in_month+1)));

%do ii=1 %to &days_in_month;

     %let start=%left(%eval((ii-1)*8+2));

     %let endd=%left(%eval(ii*8+1));

     day&ii=sum(of f&start-f&endd);

%end;

run;

%mend;

Contributor LRN
Contributor
Posts: 57

Re: Iterative calculation

Posted in reply to PaigeMiller

Hi,

Thanks a lot for the code.

I am getting the following error.

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. Does it mean that 'ii' is not resolving to number?

Thanks

Solution
‎12-12-2012 04:03 PM
Trusted Advisor
Posts: 1,908

Re: Iterative calculation

Probably a typo on my part

should read

     %let start=%left(%eval((&ii-1)*8+2));

     %let endd=%left(%eval(&ii*8+1));

Contributor LRN
Contributor
Posts: 57

Re: Iterative calculation

Posted in reply to PaigeMiller

Hi,

Thanks a lot.

It worked great.

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 377 views
  • 3 likes
  • 2 in conversation