BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
desireatem
Pyrite | Level 9

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;

1 ACCEPTED SOLUTION

Accepted Solutions
DBailey
Lapis Lazuli | Level 10

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;

View solution in original post

8 REPLIES 8
DBailey
Lapis Lazuli | Level 10

data want(drop=i);

set a;

i+1;

t=1+(i*1.4);

run;

desireatem
Pyrite | Level 9

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

DBailey
Lapis Lazuli | Level 10

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;

desireatem
Pyrite | Level 9

Great response Sir

desireatem
Pyrite | Level 9

Tanks DBailey

CTorres
Quartz | Level 8

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,

art297
Opal | Level 21

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;

pradeepalankar
Obsidian | Level 7

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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2224 views
  • 2 likes
  • 5 in conversation