creating Geometric series

Accepted Solution Solved
Reply
Super Contributor
Posts: 297
Accepted Solution

creating Geometric series

Hello  I have this data

data new;

input monset;

cards;

1

2

3

4

and want to create this data;

data news;

input tmonset;

1+1*((4-1)/4)

1+2*((4-1)/4)

1+3*((4-1)/4)

1+4*((4-1)/4)

1+4*((4-1)/4) +2

NOTE, I added 2 to the last observation. How can I modify the code below  to do this.

proc sql noprint;

select

    min(monset) as MinVal,

    (max(monset) - min(monset))/count(*) as Interval

into

    :MinVal,

     :Interval

from

    new;

quit;

data news(drop = i);

set new;

i+1;

T_monset=&MinVal + (i * &Interval);

run;


Accepted Solutions
Solution
‎01-14-2014 02:11 PM
Respected Advisor
Posts: 4,660

Re: creating Geometric series

New meaning to the geometric series...


data new;
input m;
cards;
1
2
3
4
;


proc sql;
create table stats as
select min(m) as mm, range(m)/count(m) as r from new;

quit;

data want;
set stats;
do until (endNew);

     set new end=endNew;
     t_monset = mm + m*r;
     output;
     end;
t_monset = t_monset+2;
output;
run;

proc print data=want noobs; run;

PG

PG

View solution in original post


All Replies
Super User
Posts: 17,958

Re: creating Geometric series

You can use the END data set option to denote the end of file to determine if a record is the last record and then do selective processing on that record.

http://sascanada.ning.com/profiles/blogs/adding-records-to-the-end-of-a-data-set?xg_source=activity

From an old blog post of mine:

Sometimes I find when you learn something, suddenly it'll come up everywhere. Today someone asked a question about how to add a record to the end of a data set. There are multiple options to do this, append the information from another data set using a SQL or DATA step.  But sometimes you want to use the information in your current data set and add a total or some other calculation based on the data. An easy way to do this is to use the SET <data> END=end_of_file option.

*Create sample dataset;
DATA have;
input region sales;
cards;
1 2000
2 3000
3 4000
4 5000
;

*Create a format to be used for the region labels;
proc format;
  value region_fmt
     1 = 'North'
    2 = 'South'
    3 = 'West'
    4 = 'East'
    99 = 'All';

run;

*Add the total sales to the end of the table;
DATA want;

  SET have END=eof;

  retain total_sales 0;
  format region region_fmt.;

  output;

  total_sales=total_sales+sales;

  if eof then do;
    sales=total_sales;
    region=99;

    output;

  end;
  drop total_sales;

run;

Not the best example in the world but it adds the fifth region at the end as the total sales in the dataset. This can come in useful for generating more complex reports, when the end total might not be straight addition, but division or a ratio or some other calculation. I ended up using this one trick three times in one day and now I probably won't use it again for a few years Smiley Happy.

Super Contributor
Posts: 297

Re: creating Geometric series

this is different from what I want

Super User
Posts: 17,958

Re: creating Geometric series

You need something besides adding that additional record at the end?

If so, please clarify what you want.

Super Contributor
Posts: 297

Re: creating Geometric series

No, I just want to add the additional record

Solution
‎01-14-2014 02:11 PM
Respected Advisor
Posts: 4,660

Re: creating Geometric series

New meaning to the geometric series...


data new;
input m;
cards;
1
2
3
4
;


proc sql;
create table stats as
select min(m) as mm, range(m)/count(m) as r from new;

quit;

data want;
set stats;
do until (endNew);

     set new end=endNew;
     t_monset = mm + m*r;
     output;
     end;
t_monset = t_monset+2;
output;
run;

proc print data=want noobs; run;

PG

PG
Super Contributor
Posts: 297

Re: creating Geometric series

Thanks

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 358 views
  • 0 likes
  • 3 in conversation