Re: [程式] sas 迴圈
剛剛又卡住一個問題 希望大家不要棄嫌幫我看一下T^T
從剛剛的迴圈中 我再抽出來sex=1的資料:
data maledr ; age=15 ; run ;
%macro mdivorce ;
%DO K=1958 %TO 1969 ;
DATA mdr ; SET wholedr ;
IF SEX^=1 THEN DELETE ;run ;
IF COHORT^=&K THEN DELETE ;
C&K=DR ;
KEEP AGE C&K ;
where 19<age<41 ;
RUN ;
DATA maledr ; MERGE mdr maledr ; BY AGE ; RUN ;
%END ;
%MEND ;
%mdivorce ;
可是log訊息卻跑出:
RROR 180-322: Statement is not valid or it is used out of proper order.
ERROR: BY variables are not properly sorted on data set WORK.MDR.
SEX=1 COHORT=1958 AGE=51 _TYPE_=0 _FREQ_=479 DR=0.0557446592 FIRST.AGE=1
LAST.AGE=1 _ERROR_=1 _N_=2
應該主要是上頭的訊息顯示我code錯誤的主要來源
可是 我還是按照我之前的code打出來的....
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 2 observations read from the data set WORK.MDR.
NOTE: There were 1 observations read from the data set WORK.MALEDR.
WARNING: The data set WORK.MALEDR may be incomplete. When this step was
stopped there were 1
observations and 6 variables.
WARNING: Data set WORK.MALEDR was not replaced because this step was stopped.
NOTE: DATA statement used:
real time 0.01 seconds
cpu time 0.01 seconds
NOTE: Line generated by the macro variable "K".
1 C1969
-----
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
感謝上次問題幫我解答的各位板友
問題已經解決
可是...
這次又碰上新的問題..
LIBNAME MUS 'D:\MUS\' ; run ;
libname family 'D:\research' ; run ;
data wholedr ; sex=0 ; run ;
%MACRO div ;
%DO K=1978 %TO 2009 ;
DATA divorce ; SET MUS.L&K ;
COHORT=YEAR-AGE ;
if mrg=3 then div=1 ; else div=0 ;
if cohort<1958 then delete ;
if cohort>1969 then delete ;
KEEP COHORT AGE SEX EDU KIND_EDU YEAR MRG div WEIGHT ; RUN ;
proc means noprint ; var div ; by sex cohort age ; weight weight ;
output out=div1 mean=DR ; run ;
data wholedr ; set div1 family.wholedr ;
%END ;
%MEND ;
%div ;
與之前的迴圈一樣
想說如果變數MGR=2 則變數div=1 ;
其他的div=0
然後算其平均數
可是 東西跑不出來
我去log裡頭去看
出現以下的資訊:
ERROR: Data set WORK.DIVORCE is not sorted in ascending sequence. The current
by-group has COHORT =
1969 and the next by-group has COHORT = 1962.
---------------以上為以棕色顯示出--------
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 4 observations read from the data set WORK.DIVORCE.
---------------以上以藍色顯示出-----------
WARNING: The data set WORK.DIV1 may be incomplete. When this step was
stopped there were 2
observations and 6 variables.
WARNING: Data set WORK.DIV1 was not replaced because this step was stopped.
---------以上為綠色顯示出-----------------
NOTE: PROCEDURE MEANS used:
real time 0.01 seconds
cpu time 0.01 seconds
第一個棕色看起來好像是我的檔案沒有被排進迴圈裡頭
可是 為什麼呢?
之後好像是我的指令問題
可是我已經盡量debug 但還是看不出哪有問題....
還請各位幫我解答ˊˋ
※ 引述《a3709417 (項肥)》之銘言:
: [軟體熟悉度]:
: 新手(不到1個月)
: 低(1~3個月)
: 以下是根據老師所給的code修改:
: %MACRO marrage ;
: %DO K=1978 %TO 2009 ;
: DATA marrage ; SET MUS.L&K ;
: COHORT=YEAR-AGE ;
: IF MRG=1 THEN marrage=0 ; ELSE marrage=1 ;
: if cohort<1958 then delete ;
: if cohort>1969 then delete ;
: KEEP COHORT AGE SEX EDU KIND_EDU YEAR MRG MARRAGE WEIGHT ; RUN ;
: PROC SORT ; BY SEX COHORT AGE ; RUN ;
: PROC MEANS NOPRINT ; VAR marrage ; BY SEX COHORT AGE ; WEIGHT WEIGHT ;
: OUTPUT OUT=marrage1 MEAN=MR ; RUN ;
: %END ;
: %MEND ;
: %marrage ;
: 原本應該每個COHORT(1958~1969)應該會跑出其在1978~2009年的mr平均值
: 可是我跑出來的好像卻只有最後一年 2009的平均值
: 是我哪部分有遺漏掉嗎??
: 還懇請大家幫我解惑!
--
推
02/08 13:59,
02/08 13:59
→
02/08 13:59,
02/08 13:59
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 175.181.145.156
推
06/27 16:15, , 1F
06/27 16:15, 1F
→
06/27 16:16, , 2F
06/27 16:16, 2F
→
06/27 16:16, , 3F
06/27 16:16, 3F
→
06/27 16:16, , 4F
06/27 16:16, 4F
→
06/27 16:19, , 5F
06/27 16:19, 5F
※ 編輯: a3709417 來自: 122.126.179.163 (06/27 21:41)
討論串 (同標題文章)