Re: [程式] 輸出符合條件式的變數答案

看板Statistics作者時間7年前 (2018/09/23 22:23), 編輯推噓1(102)
留言3則, 1人參與, 7年前最新討論串2/2 (看更多)
※ 引述《linda841004 (水凝結)》之銘言: : [軟體程式類別]:SAS : : [程式問題]:增加一個變數,能夠輸出條件句符合的變數答案 : : [軟體熟悉度]: 熟悉 : [問題敘述]: : 我有一筆資料(如下圖) : http://i.imgur.com/zmclQvw.jpg
: 因為id=2時,a1=1,a3=3通過條件且 : id=8時,a1=1,a2=3通過條件 : 就會輸出成下圖的樣子http://i.imgur.com/2dpYSf2.jpg
: 但是紅框框select欄,要輸出通過的條件 : (我不知道除了寫多個if條件式以外的方法)---因為真實資料有很多個if要做挑選 : 請大神協助! : [程式範例]: : data aa; : input id a1 a2 a3; : cards; : 1 1 1 2 : 2 1 2 3 : 5 2 1 1 : 8 1 3 1 : ; : run; : data bb; : set aa; : if a1=1 & (a2^in(1 2) | a3^in(1 2))then : do; : wrong="有錯"; : output; : end; : proc print; : run; : ----- : Sent from JPTT on my HTC_U-3u. 這種需要留下判斷過程的case通常不要寫成複合判斷式會比較簡單 調整邏輯 step1: 對個別判斷式留下註記,個別判斷式 step2: 對累積的註記作最後判斷 if a1=1 then do; select='a1=1'; if a2^in(1 2) then select=cats(select,',','a2=',a2); if a3^in(1 2) then select=cats(select,',','a3=',a3); end; ........(先把所有邏輯判斷完) if select ^=' ' then do; wrong='有錯'; output; end; p.s.上面只是示意,if的結構還有最終判斷的策略會取決於實際邏輯有多複雜而有所 變化 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.227.162.191 ※ 文章網址: https://www.ptt.cc/bbs/Statistics/M.1537712617.A.BA3.html

09/24 13:48, 7年前 , 1F
謝謝解答,這個方法我有想到(只是覺得運用起來很...
09/24 13:48, 1F

09/24 13:48, 7年前 , 2F
....繁雜...)
09/24 13:48, 2F

09/24 13:48, 7年前 , 3F
感謝w大,中秋節快樂!
09/24 13:48, 3F
文章代碼(AID): #1Rfw7fkZ (Statistics)
文章代碼(AID): #1Rfw7fkZ (Statistics)