2012-01-13 79 views
1

我有一個名爲* term_table *包含下面列在每個觀測變量的值脫身宏變量

補償,term_type,期限,評分表,排名

我去通過每個觀察和每個obs,我想將變量rank的值存儲到一個名爲'curr_r'的宏變量中。下面我創建的代碼不起作用

Data Work.term_table; 
input Comp $ 
     Term_type $ 
     Term $ 
     Score 
     Rank 
     ; 
datalines; 
comp1 term_type1 A 1 1 
comp2 term_type2 A 2 10 
comp3 term_type3 A 3 20 
comp4 term_type4 B 4 20 
comp5 term_type5 B 5 40 
comp6 term_type6 B 6 100 
; 
Run; 

    %local j; 
    DATA tmp; 
    SET term_table; 
    LENGTH freq 8; 

    BY &by_var term_type term; 
    RETAIN freq; 

    CALL SYMPUT('curr_r', rank); 
    IF first.term THEN DO; 
     %do j = 1 %to &curr_r; 
         do some thing 
        %end; 
    END; 
RUN; 

你能幫我解決這個問題

非常感謝

回答

1

呼叫symput語句也創建宏VAR & curr_r與排名的值,但它是不可用的,直到數據步之後。

但是,我不認爲你需要創建宏var & curr_r。我認爲根本不需要宏。

我認爲以下應工作:(未經測試)

DATA tmp; 
SET term_table; 
LENGTH freq 8; 

BY &by_var term_type term; 
RETAIN freq; 

IF first.term THEN DO; 
    do j = 1 to rank; 
    <do some thing> 
    end; 
END; 
RUN; 

如果你需要使用等級從先前的OBS,使用LAG功能。很多

Proc Sql noprint; 
select count(rank) 
into :cnt 
from term_table; 

%Let cnt=&cnt; 

select rank 
into :curr_r1 - :curr_r&cnt 
from term_table; 
quit; 
+0

感謝:

Start=Lag(rank); 

排名的每個值存儲在宏變量,下面將這樣做。在term_table中,假設術語'A'具有RANK的3個值:1,10,20。現在假定我處於obs term = A和rank = 10。我需要做一個循環:「做J = 2到10」。如果我的學期= A,等級= 20,我需要做一個循環:「Do j = 11 to 20)。這意味着我應該存儲前一個等級和每個obs的當前等級來完成循環。我有任何建議Hung – 2012-01-13 13:13:02

+0

嗨,是的,最後我發現我不需要宏變量來解決我的問題,謝謝了很多 – 2012-01-13 13:31:46

+1

很高興幫助...我用一些示例數據更新了答案,如果需要的話更改。您的原始附加問題,它會使答案變得有意義。示例數據使得它更容易幫助... – 2012-01-13 14:41:30