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;
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;
data want(drop=i);
set a;
i+1;
t=1+(i*1.4);
run;
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
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;
Great response Sir
Tanks DBailey
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,
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.