Create an evenly series variable

Solved
Super Contributor
Posts: 328

Create an evenly series variable

Hello , I have this data;

data a

input b;

cards;

1

3

4

5

8;

run;

I want to create a second variable were I add this term to the first variable: Last observation(8)-first observation (1)/n, the to the second 2*( last observation(8)-first observation (1)/n) to the third 3*( last observation(8)-first observation (1)/n)

where n=total number of observation. I wish to do this within a simulation so will love to have it like this: Last observation-First observation/ n. I will know n but not Last observation and First observation.

data a

input b t;

cards;

1   1+Last observation(8)-first observation (1)/n=5=1.4

3   1+ 2(1.4)

4   1+ 3(1.4)

5   1+4(1.4)

8   1+5(1.4);

run;

Accepted Solutions
Solution
‎01-10-2014 03:22 PM
Super Contributor
Posts: 578

Re: Create an evenly space variable

Presuming your data is sorted by b:

proc sql noprint;

select

min(b) as MinVal,

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

into

:MinVal,

:Interval

from

a;

quit;

data want(drop = i);

set a;

i+1;

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

run;

All Replies
Super Contributor
Posts: 578

Re: Create an evenly space variable

data want(drop=i);

set a;

i+1;

t=1+(i*1.4);

run;

Super Contributor
Posts: 328

Re: Create an evenly space variable

This is not what I want. I want two variable;

In the second variable I am adding Last observation(8)-first observation (1)/n=5, the second I am adding 2 * Last observation(8)-first observation (1)/n=5. etc. So I will have two variables, where the second is a a series

Solution
‎01-10-2014 03:22 PM
Super Contributor
Posts: 578

Re: Create an evenly space variable

Presuming your data is sorted by b:

proc sql noprint;

select

min(b) as MinVal,

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

into

:MinVal,

:Interval

from

a;

quit;

data want(drop = i);

set a;

i+1;

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

run;

Super Contributor
Posts: 328

Re: Create an evenly space variable

Great response Sir

Super Contributor
Posts: 328

Re: Create an evenly space variable

Tanks DBailey

Regular Contributor
Posts: 180

Re: Create an evenly series variable

Try this:

data a;

input b;

cards;

1

3

4

5

8

;

run;

data _null_;

if 0 then set a nobs=nobs;

set a point=nobs;

call symputx('B',b);

stop;

run;

data want;

set a nobs=nobs;

retain k;

if _N_=1 then do;

k=(&b-b)/nobs;

end;

t=1+_N_*k;

drop k;

run;

Regards,

PROC Star
Posts: 8,164

Re: Create an evenly series variable

I'd go about it a bit differently, namely:

data want (keep=b t);

do until(last);

set a end=last;

obs+1;

if last then m=b;

end;

n=0;

do until(last);

set a end=last;

n+1;

if n eq 1 then x=(m-b)/obs;

t=1+n*x;

output;

end;

run;

Frequent Contributor
Posts: 106

Re: Create an evenly series variable

data a ;

input b;

obs=_N_;

cards;

1

3

4

5

8

;

run;

proc sort data =a out =test(rename=(b=b1));

by descending obs;

data test2(keep=b b1);

merge a test;

obs_new=_N_;

run;

data test2(drop=b_first b1_first b1);

set test2 nobs=nobs;

retain b_first b1_first;

if _n_=1 then do ;b_first=b;b1_first=b1;end;

t=1+_N_*(b1_first-b_first)/nobs;

run;

🔒 This topic is solved and locked.