2015-05-14 53 views
1

辨別預測我有一個表:糖尿病:採用主成分分析

col1 col2 
2  20 
2.5  25 
2.67  30 
2.99  40 

我希望得到

varone = 2 x col2, vartwo= 2.5 x col2, varthree= 2.67 x col3, varfour=2.99 x col2 

即從表中
提取特定的值,然後乘以一整按該值列(scalar x vector)。

我試着換位col1

col1a col1b col1c col1d  col2 
2  2.5 2.67 2.99  20 
           25 
           30 
           40 

,然後試圖乘col1a x col2,但它似乎沒有工作。

+0

既然你已經用SQL標記了這個標記,那麼給出一些更詳細的信息可能是有用的,你可能已經嘗試了哪些查詢,以及你正在使用哪種SQL。 – Bruno

回答

0

在SAS,你可以用proc sql

proc sql; 
    select 2*col2 as varone, 2.5*col2 as vartwo, 2.67*col3 as varthree, 2.99*col2 as varfour 
    from atable; 
0

您正在使用SAS,要麼PROC FACTORPROC PRINCOMP那麼你可以使用PROC SCORE假設。

例直接從文檔:

http://support.sas.com/documentation/cdl/en/statug/63347/HTML/default/viewer.htm#statug_score_sect017.htm

/* This data set contains only the first 12 observations */ 
    /* from the full data set used in the chapter on PROC REG. */ 
data Fitness; 
    input Age Weight Oxygen RunTime RestPulse RunPulse @@; 
    datalines; 
44 89.47 44.609 11.37 62 178  40 75.07 45.313 10.07 62 185 
44 85.84 54.297 8.65 45 156  42 68.15 59.571 8.17 40 166 
38 89.02 49.874 9.22 55 178  47 77.45 44.811 11.63 58 176 
40 75.98 45.681 11.95 70 176  43 81.19 49.091 10.85 64 162 
44 81.42 39.442 13.08 63 174  38 81.87 60.055 8.63 48 170 
44 73.03 50.541 10.13 45 168  45 87.66 37.388 14.03 56 186 
; 
proc factor data=Fitness outstat=FactOut 
      method=prin rotate=varimax score; 
    var Age Weight RunTime RunPulse RestPulse; 
    title 'Factor Scoring Example'; 
run; 
proc print data=FactOut; 
    title2 'Data Set from PROC FACTOR'; 
run; 
proc score data=Fitness score=FactOut out=FScore; 
    var Age Weight RunTime RunPulse RestPulse; 
run; 
proc print data=FScore; 
    title2 'Data Set from PROC SCORE'; 
run; 
0

你可以利用陣列來實現這一目標。

下面的程序是動態的。它將適用於任何數量的觀察。

****data we have****; 
data have; 
input col1 col2; 
datalines; 
2  20 
2.5  25 
2.67  30 
2.99  40 
; 
run; 

****Taking Count****; 
****Creating macro "value" to store col1 data****; 
proc sql ; 
    select count(*) into :cnt_rec from have; 
    select col1 into :value1 - :value&SysMaxLong from have; 
quit; 


data want(drop=i); 
    set have; 

    array NewColumn(&cnt_rec); 

    ****processing the array and multiplying col2 data****; 

    do i = 1 to &cnt_rec; 
     NewColumn[i] = symget('value'||left(i)) * col2; 
    end; 
run;