BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Peter_C
Rhodochrosite | Level 12
> How can I do this in data step? Declare variable (not
> column in table) out of the data step.
>
> sum = 0;
> data example;
> set example;
> sum = sum + height
> run;
>
* not complex to place a set statement within a do loop terminating when there is no more data provided by that set statement
data ........... ;
..........
sum=0 ;
do while( not end_of_this );
set example(where=( relevant where clause)) end= end_of_this ;
sum+height ;
end ;
............
........
stop ;
run;
of course then the data step iteration needs to be managed carefully and you'll probably find need for the STOP statement when appropriate;
Reeza
Super User
Why have the sum declared outside the datastep?

Its easy to solve the problems you're proposing, but then you say it might be more complex so without you being clear enough it's kind of a moot point.

A random guess into what you're doing looks like BY processing with RETAIN statements needed to calculate some kind of running total, but if you don't provide a more clear example of what you have and what you want its almost impossible to help you. The following may help.
http://www2.sas.com/proceedings/sugi31/246-31.pdf

Message was edited by: Reeza
LAP
Quartz | Level 8 LAP
Quartz | Level 8

If I'm understanding what you want: sum some value across all rows in you table.

then I think this will work.

data example;
set example;
if _n_ = 1 then sum=0; *I do not think you need this line but I put in for example;

sum + height; increments sum by value of height; 

run;



I'm not sure of what you are thinking about when you say use sum function over height column. The sum function works on columns in a single row ie
something like this.... sum = sum(ht1, ht2, ht3).

This syntax sums across rows. So when i = linescount, Height will be the sum of height over all lines.

You certainly can get more creative with what you increment - as you say some more complex calculation

for example : sum + (a + b*(c^2)*d/e); where a-e are all columns.

Hope this helps

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!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 17 replies
  • 84486 views
  • 1 like
  • 11 in conversation