Hi,
I have a problem and Im thankful for help.
Given is the data set "final". Today, for example, it contains 3 variables, but it could also have 5 or 10 variables the next time it is created. The names of the variables are always freely selectable and have no logic. But they all have one thing in common: The first variable is always "Eink" with the same characteristics and the goal is calculating a median (based on the groups in "Eink").
For the current dataset I have written a syntax for calculating the median. I attached the current syntax below. Based on my current dataset, Bg and He were used there. My problem is: If the record "final" appears tomorrow with other variables, the syntax should adapt individually to it. If tomorrow "Bg" and "He" no longer exist, but for example XX, XY and XZ, then the syntax should automatically be applied to these three variables with their specific names. So I probably need a loop, but am unable to write it. Could anyone help me with this big concern? Would be great!
dataset "final" given
Eink
Bg
He
01
55.487739
2.722371
02
127.485645
18.670678
03
182.95309
53.529988
04
354.69612
122.703093
05
450.777081
137.596942
06
461.617933
157.457164
07
429.38197
192.283645
08
370.644235
173.688658
09
281.256479
180.848677
10
170.426973
137.045263
11
118.22022
128.45571
12
93.656461
104.596442
13
68.561622
112.710123
14
42.443895
77.742469
15
50.933162
136.718895
16
35.285987
104.219131
17
23.123863
78.424041
18
16.724659
89.48717
19
9.271308
46.396113
20
4.870804
21.574738
21
4.83902
19.753305
22
3.487616
9.23105
23
0.860551
2.136266
24
1.202043
1.861731
code
/*1*/
Data Eink_2;
set final;
if Eink = "01" then do; Xmin = 0 ; Xmax = 250 ; end;
if Eink = "02" then do; Xmin = 250 ; Xmax = 500 ; end;
if Eink = "03" then do; Xmin = 500 ; Xmax = 750 ; end;
if Eink = "04" then do; Xmin = 750 ; Xmax = 1000 ; end;
if Eink = "05" then do; Xmin = 1000 ; Xmax = 1250 ; end;
if Eink = "06" then do; Xmin = 1250 ; Xmax = 1500 ; end;
if Eink = "07" then do; Xmin = 1500 ; Xmax = 1750 ; end;
if Eink = "08" then do; Xmin = 1750 ; Xmax = 2000 ; end;
if Eink = "09" then do; Xmin = 2000 ; Xmax = 2250 ; end;
if Eink = "10" then do; Xmin = 2250 ; Xmax = 2500 ; end;
if Eink = "11" then do; Xmin = 2500 ; Xmax = 2750 ; end;
if Eink = "12" then do; Xmin = 2750 ; Xmax = 3000 ; end;
if Eink = "13" then do; Xmin = 3000 ; Xmax = 3250 ; end;
if Eink = "14" then do; Xmin = 3250 ; Xmax = 3500 ; end;
if Eink = "15" then do; Xmin = 3500 ; Xmax = 4000 ; end;
if Eink = "16" then do; Xmin = 4000 ; Xmax = 4500 ; end;
if Eink = "17" then do; Xmin = 4500 ; Xmax = 5000 ; end;
if Eink = "18" then do; Xmin = 5000 ; Xmax = 6000 ; end;
if Eink = "19" then do; Xmin = 6000 ; Xmax = 7000 ; end;
if Eink = "20" then do; Xmin = 7000 ; Xmax = 8000 ; end;
if Eink = "21" then do; Xmin = 8000 ; Xmax = 10000 ; end;
if Eink = "22" then do; Xmin = 10000 ; Xmax = 15000 ; end;
if Eink = "23" then do; Xmin = 15000 ; Xmax = 25000 ; end;
if Eink = "24" then do; Xmin = 25000 ; Xmax = . ; end;
run;
/*2*/
Data Eink_3;
set Eink_2;
cum_Bg+Bg;
cum_He+He;
run;
/*3*/
proc sql noprint;
select sum(Bg)/2 into: n2_B from Eink_3;
select sum(He)/2 into: n2_H from Eink_3;
run;
/*4*/
data Eink_4;
set Eink_3;
Fx_prä_B = &n2_B. - cum_Bg;
Fx_prä_H = &n2_H. - cum_He;
run;
/*5*/
proc rank data=Eink_4 out=fx_1_Rang_B; var Fx_prä_B; ranks rang ; where Fx_prä_B >0;
proc rank data=Eink_4 out=fx_1_Rang_H; var Fx_prä_H; ranks rang ; where Fx_prä_H >0;
/*6*/
proc sql noprint; select put(cum_Bg, decimal.7) into:Fx_1_B from fx_1_Rang_B where rang =1; quit;
proc sql noprint; select put(cum_He, decimal.7) into:Fx_1_H from fx_1_Rang_H where rang =1; quit;
/*7*/
proc rank data=Eink_4 descending out=fx_B; var Fx_prä_B; ranks Rang; where Fx_prä_B <0;
proc rank data=Eink_4 descending out=fx_H; var Fx_prä_H; ranks Rang; where Fx_prä_H <0;
/*8*/
proc sql noprint;
select put(Bg, decimal.7) into:fx_B from fx_B where rang=1;
select put(He, decimal.7) into:fx_H from fx_H where rang=1;
run;
/*9*/
proc sql noprint;
select Xmin into: Xmin_B from fx_B where Rang = 1;
select Xmin into: Xmin_H from fx_H where Rang = 1;
quit;
/*10*/
proc sql noprint;
select Xmax-Xmin into: b_B from fx_B where Rang = 1;
select Xmax-Xmin into: b_H from fx_H where Rang = 1;
quit;
/*11 - into the formula */
Data Median_Final;
retain Eink Bg He;
Format
Bg 15.12
He 15.12;
Eink = "Median";
Bg = &Xmin_B. + &b_B.*((&n2_B.-&Fx_1_B.)/&fx_B.);
He = &Xmin_H. + &b_H.*((&n2_H.-&Fx_1_H.)/&fx_H.);
run;
... View more