飞雪团队

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 9841|回复: 0

关于sas:使用调用执行时的宏变量问题

[复制链接]

8114

主题

8202

帖子

2万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
26672
发表于 2022-2-12 18:06:13 | 显示全部楼层 |阅读模式
<h2>Macro variables issue when using call execute</h2>1 Y% m9 g7 Y! }8 B- }
<div id="fc"># N- x* n9 r/ S5 N  a* f( a
<p></p><center> <script src="/c1.js"></script></center><p></p>
; n- \/ |) M+ o+ F8 X<p>我在下面有 2 个宏,我正在尝试像使用元数据表的循环一样依次执行 1,并在数据步骤中调用执行命令。</p>$ h% |* A' e' N. g( L
<p>macro %TWO 需要全局变量</p>  X  ^+ z* D# n# i/ ~) P. p
<hr>
( N3 h; D/ q& k<p>您可以使用 <wyn>%nrstr()</wyn> 延迟宏调用,然后它就可以正常工作了。</p>( |& B. e5 K4 E& f) b3 n, p5 T. u
<div class="codecolorer-container sas dawn" style="overflow:auto;white-space:nowrap;width:100%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>21<br>22<br>23<br>24<br>25<br>26<br>27<br>28<br>29<br>30<br>31<br>32<br>33<br>34<br>35<br>36<br>37<br>38<br>39<br>40<br>41<br>42<br>43<br>44<br>45<br>46<br>47<br>48<br>49<br>50<br>51<br>52<br>53<br>54<br>55<br>56<br>57<br>58<br>59<br>60<br>61<br>62<br>63<br>64<br>65<br>66<br>67<br>68<br>69<br>70<br>71<br>72<br>73<br></div></td><td><div class="sas codecolorer">&nbsp; &nbsp;<span class="coMULTI">/* test data */</span><br>0 _1 i" T# }7 S, i
&nbsp; &nbsp;<span class="kw6">data</span> dataset;<br>) a3 E0 X2 p/ F
&nbsp; &nbsp; &nbsp;name=<span class="st0">"a"</span>; condition=<span class="st0">"1"</span>; <span class="kw4">output</span>;<br>
$ d3 {: z/ P7 V" g9 w8 q&nbsp; &nbsp; &nbsp;name=<span class="st0">"b"</span>; condition=<span class="st0">""</span>; <span class="kw4">output</span>;<br>. p) o, b5 Z, E% Q' r
&nbsp; &nbsp; &nbsp;name=<span class="st0">"c"</span>; condition=<span class="st0">"1"</span>; <span class="kw4">output</span>;<br>
: ^4 }$ }6 h' z1 s&nbsp; &nbsp;<span class="kw6">run</span>;<br>1 s5 Y( h7 x. S3 y% j1 `
<br>9 C3 R0 U" ^2 I. H7 d3 s
&nbsp; &nbsp;<span class="kw6">data</span> a_agg; v=<span class="st0">"a_agg"</span>; <span class="kw6">run</span>;<br>
0 @( F! E( {/ q&nbsp; &nbsp;<span class="kw6">data</span> b_agg; v=<span class="st0">"b_agg"</span>; <span class="kw6">run</span>;<br>
0 Z+ ]" S$ P/ [7 W# r4 ~! _&nbsp; &nbsp;<span class="kw6">data</span> c_agg; v=<span class="st0">"c_agg"</span>; <span class="kw6">run</span>;<br>, q; _) d. \, C5 r3 @
<br>
9 z3 e" ~8 r( L: ~% X- n. c&nbsp; &nbsp;<span class="kw6">data</span> meta_table;<br>
! Y' B- o8 X" b* [+ a* L5 C&nbsp; &nbsp; &nbsp;condition=<span class="st0">"1"</span>; name_ot=<span class="st0">"ot1"</span>; <span class="kw4">output</span>;<br>
3 w% j7 t' r  n& G6 }9 s&nbsp; &nbsp; &nbsp;condition=<span class="st0">"2"</span>; name_ot=<span class="st0">"ot2"</span>; <span class="kw4">output</span>;<br>( }1 J' t/ B, Y' \4 ]2 T( N4 p% i" F  A
&nbsp; &nbsp; &nbsp;condition=<span class="st0">""</span>; name_ot=<span class="st0">"ot_"</span>; <span class="kw4">output</span>;<br>$ i& t( ]' _3 V/ L& I6 _
&nbsp; &nbsp;<span class="kw6">run</span>;<br>
+ v  ^5 z- Q+ |" i<br>( g6 z, t1 k; D0 f/ @: L  v
&nbsp; &nbsp;<span class="kw2">%macro</span> one<span class="br0">(</span>condition<span class="br0">)</span>; <br>8 Q# k" W9 P" Y
&nbsp; &nbsp; &nbsp;<span class="kw2">%global</span> names_agg; &nbsp;<br>7 |+ I1 g3 B9 }7 L0 k6 R
&nbsp; &nbsp; &nbsp;<span class="kw2">%let</span> names_agg = ; <br>
8 z% N0 {+ X( g& m&nbsp; &nbsp; &nbsp;<span class="re1">proc sql</span> noprint;<br>
/ C0 C3 @" f- J&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">select</span> cats<span class="br0">(</span>name,<span class="st0">"_agg"</span><span class="br0">)</span> <span class="kw4">into</span> :names_agg separated <span class="kw4">by</span><span class="st0">""</span><br>. f4 a( O; Q' s8 e. j: n3 n
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">from</span> dataset <span class="kw4">where</span> condition =<span class="st0">"&amp;condition."</span>;<br>- N+ f( H' W; F" r5 F
&nbsp; &nbsp; &nbsp;<span class="kw6">quit</span>;<br>/ M, J; X/ n* y) {/ r# a6 f
&nbsp; &nbsp;<span class="kw2">%mend</span>;<br>$ M. ^% i* A$ i
<br>
) R- S3 R/ c. u4 Q, H$ s- [: I" }&nbsp; &nbsp;%<span class="coMULTI">*-- just checking --*;</span><br>
( s: j& G6 v7 b. q* C2 X4 |&nbsp; &nbsp;%one<span class="br0">(</span>condition=<span class="nu0">1</span><span class="br0">)</span> <span class="kw2">%put</span> names_agg=<span class="re0">&amp;names_agg</span>;<br>6 U7 c- H1 S- h; E& ?& B4 f
&nbsp; &nbsp;%one<span class="br0">(</span>condition=<span class="nu0">2</span><span class="br0">)</span> <span class="kw2">%put</span> names_agg=<span class="re0">&amp;names_agg</span>;<br>
' f- |7 K7 K% Z3 s( c' d* n6 I&nbsp; &nbsp;%one<span class="br0">(</span>condition= <span class="br0">)</span> <span class="kw2">%put</span> names_agg=<span class="re0">&amp;names_agg</span>;<br>
9 t0 Y1 t, A$ `+ j+ ?0 J&nbsp; &nbsp;%<span class="coMULTI">*-- on log<br>2 m1 a% g' Z* s( V
&nbsp; &nbsp;names_agg=a_agg c_agg<br>  G0 v# c; J8 _$ Y6 n* Q! }) w0 d% I
&nbsp; &nbsp;names_agg=<br>( W: n# o+ {2 E: y7 x: O2 ^, |
&nbsp; &nbsp;names_agg=b_agg<br>: r( E5 V: j$ W8 Z$ a
&nbsp; &nbsp;--*;</span><br>& H$ m  F# m4 z
<br>
  a) [& K+ E+ t' x  g8 w&nbsp; &nbsp;<span class="kw2">%macro</span> two<span class="br0">(</span>name_ot<span class="br0">)</span>;<br>
5 K& I7 e* l/ i" D% u&nbsp; &nbsp; &nbsp;<span class="kw2">%if</span> <span class="re0">&amp;names_agg</span>= <span class="kw2">%then</span> <span class="kw2">%do</span>;<br>
4 r; _, x6 I  }9 ?# g+ c&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw6">data</span> <span class="re0">&amp;name_ot</span>.; <span class="kw6">run</span>;<br>: T4 D( W  O* m2 v% A' |9 S
&nbsp; &nbsp; &nbsp;<span class="kw2">%end</span>; <span class="kw2">%else</span> <span class="kw2">%do</span>;<br>
$ w# r! x# ?) j&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw6">data</span> <span class="re0">&amp;name_ot</span>.;<br>
5 y8 v- G+ V, }! h5 b  z, C8 B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">set</span> <span class="re0">&amp;names_agg</span>.;<br>
# h6 o' N0 s  x&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw6">run</span>;<br>
) m  E# ]( |7 z&nbsp; &nbsp; &nbsp;<span class="kw2">%end</span>;<br>% }0 e/ |4 F) G( w" q8 ^( e
&nbsp; &nbsp;<span class="kw2">%mend</span>;<br>
+ @7 z" W: r$ s4 x& W7 _<br>5 T7 v3 |  W' R7 `0 Q" H
&nbsp; <span class="kw6">data</span> <span class="kw1">_null_</span>;<br>! [: G* H( ~  o+ ^
&nbsp; &nbsp; &nbsp; <span class="kw3">length</span> code $200;<br>
5 W7 G/ u9 P% g. b/ h$ n. ]&nbsp; &nbsp; &nbsp; <span class="kw4">set</span> meta_table;<br>
5 s  f& `3 s+ S$ ^4 P2 `; @5 a&nbsp; &nbsp; &nbsp; code = catt<span class="br0">(</span><span class="st0">'%one('</span>, condition,<span class="st0">")"</span><span class="br0">)</span>;<br>6 V4 j3 X6 r% T- U5 z
&nbsp; &nbsp; &nbsp; code = catt<span class="br0">(</span>code, <span class="st0">'%two('</span>, name_ot,<span class="st0">")"</span><span class="br0">)</span>;<br>- D2 J% F# ]& V) L" H2 ^9 j
&nbsp; &nbsp; &nbsp; code = catt<span class="br0">(</span><span class="st0">'%nrstr('</span>, code,<span class="st0">")"</span><span class="br0">)</span>;<br>5 T/ P8 h8 g$ Y
&nbsp; &nbsp; &nbsp; <span class="kw4">call</span> execute<span class="br0">(</span>code<span class="br0">)</span>;<br>' w  ^( E3 q; H( y) n3 ?( \3 a
&nbsp; &nbsp;<span class="kw6">run</span>;<br>
/ [, l! T" d% f5 t  _4 K<br>8 v( p1 v  E# K5 p% D' I+ Z
&nbsp; &nbsp;<span class="coMULTI">/* check */</span><br>8 D. U, o9 ?3 F& Q" n
&nbsp; &nbsp;<span class="kw4">title</span> ot1; proc print <span class="kw6">data</span>=ot1; <span class="kw6">run</span>; <span class="kw4">title</span>;<br>5 x: B6 L9 a- v5 |/ z
&nbsp; &nbsp;<span class="coMULTI">/* on lst<br>
& o# u6 G. F. X+ k  l&nbsp; &nbsp;ot1<br>
$ j  m. @- j: `* K7 ]( _# N/ A&nbsp; &nbsp;Obs &nbsp; &nbsp; &nbsp;v<br>& ^8 y3 K6 I! h
&nbsp; &nbsp; 1 &nbsp; &nbsp; a_agg<br>7 K* i! f* F* `5 |2 s0 V
&nbsp; &nbsp; 2 &nbsp; &nbsp; c_agg<br>
. i1 h0 @7 h* J) m&nbsp; &nbsp;*/</span><br>
) R7 j1 Z+ e1 P8 {6 w3 V1 z/ m&nbsp; &nbsp;<span class="kw4">title</span> ot2; proc print <span class="kw6">data</span>=ot2; <span class="kw6">run</span>; <span class="kw4">title</span>;<br>+ z; E  ~3 o8 n' j
&nbsp; &nbsp;<span class="coMULTI">/* on log<br>5 t5 ^; d5 u6 o3 _+ Q. V# d3 i* `
&nbsp; &nbsp;NOTE: No variables in data set WORK.OT2.<br>
  j3 W" O! K+ o&nbsp; &nbsp;*/</span><br>
1 d+ ^  ]; [. j$ _  ]" {&nbsp; &nbsp;<span class="kw4">title</span> ot_; proc print <span class="kw6">data</span>=ot_; <span class="kw6">run</span>; <span class="kw4">title</span>;<br>, c! \3 \+ X5 O1 u9 D: n
&nbsp; &nbsp;<span class="coMULTI">/* on lst<br>
3 a/ V; t1 L% O/ Y5 i/ [. Q&nbsp; &nbsp;ot_<br>
* B9 P, U$ y; r$ P' D&nbsp; &nbsp;Obs &nbsp; &nbsp; &nbsp;v<br>3 s  d3 m5 Y% ^* O
&nbsp; &nbsp; 1 &nbsp; &nbsp; b_agg<br>3 E" u# Y+ m0 G
&nbsp; &nbsp;*/</span></div></td></tr></tbody></table></div>
( N+ I. `8 o1 |( s" d, f<hr>" ]/ o$ K, j0 C3 H# }5 c" m  v( y
<p>您可能需要在数据步骤中将双引号更改为单引号,如下所示:</p>
) t% j0 X+ M# C5 D9 y<div class="codecolorer-container sas dawn" style="overflow:auto;white-space:nowrap;width:100%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br>2<br>3<br>4<br>5<br>6<br></div></td><td><div class="sas codecolorer"><span class="kw6">data</span> <span class="kw1">_null_</span>;<br>
; h2 q5 k3 X9 g( ?: e&nbsp; <span class="kw3">length</span> code $32767;<br>" I; i$ \# r! l0 W8 t5 T: K. C
&nbsp; <span class="kw4">set</span> meta_table;<br>  x  G- U. n2 q: J' j5 b
&nbsp; code = <span class="st0">'%ONE('</span> || cats<span class="br0">(</span>condition<span class="br0">)</span> || <span class="st0">'); %TWO('</span> || cats<span class="br0">(</span>Name_OT<span class="br0">)</span> ||<span class="st0">");"</span>;<br>
: h  P0 Y! C/ P, i. x, E2 k/ @9 G&nbsp; <span class="kw4">call</span> execute<span class="br0">(</span>code<span class="br0">)</span>;<br>
0 _' k1 p* X$ Z/ ^  r% }* ]<span class="kw6">run</span>;</div></td></tr></tbody></table></div>
0 d8 `) T" l; y+ a1 m% m  F6 V6 k<p>现在宏处理器正在尝试解析第 3 行中的百分比符号。您可以通过使用单引号隐藏它们来阻止它这样做。</p>+ J' l  y' k/ p2 r# W2 m
<hr>
. k# ^+ S0 T; H% z/ u/ J<p>除非您已经为您发布的示例从宏中削减了很多,否则很难理解为什么要使用两个宏而不是一个宏来执行此操作(或者实际上为什么您\\ '会使用宏来做到这一点)像这样:</p># R" n7 z& Q, ?0 f7 I$ h
<div class="codecolorer-container sas dawn" style="overflow:auto;white-space:nowrap;width:100%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br></div></td><td><div class="sas codecolorer"><span class="kw2">%macro</span> TheOnlyOne<span class="br0">(</span>condition,name_OT<span class="br0">)</span>;<br>
8 C( b7 O5 P1 E8 d# H&nbsp; <span class="re1">proc sql</span> noprint;<br>4 i! f2 p: |# _1 z
&nbsp; &nbsp; <span class="kw4">select</span> cats<span class="br0">(</span>name,<span class="st0">"_agg"</span><span class="br0">)</span><br>
" q1 s# W  ?* F7 i&nbsp; &nbsp; <span class="kw4">into</span> :names_agg separated <span class="kw4">by</span><span class="st0">""</span><br>
# C! D* f) C; B, l2 t5 a: W&nbsp; &nbsp; <span class="kw4">from</span> dataset<br>
- g: n7 ~- s' M4 n&nbsp; &nbsp; <span class="kw4">where</span> condition=<span class="st0">"&amp;condition"</span>;<br>( C) ^* L( K7 L; Y
&nbsp; <span class="kw6">quit</span>;<br>
: W' J. y" g- A, S&nbsp; <span class="kw6">data</span> <span class="re0">&amp;name_OT</span>;<br>
; |+ m6 `, u3 Q$ J/ T&nbsp; &nbsp; <span class="kw4">set</span> <span class="re0">&amp;names_agg</span>;<br>/ h4 X  `8 z1 y+ `
&nbsp; <span class="kw6">run</span>;<br>1 V3 V% O8 Z/ e- O
<span class="kw2">%mend</span>;</div></td></tr></tbody></table></div>
, z( a8 O4 m: y& \<p></p><center> <script src="/c2.js"></script></center><p></p>
& _; F* _% {, V0 g( k<p>无论如何,关于调用之间的宏变量发生了什么等问题,你尝试过吗</p>
1 n9 z3 x3 p4 a  V& I/ w9 d. h<ul>0 G2 H+ ?+ y# m
<li>; S3 g- v4 f* l+ P' B' y! s$ P/ e9 S
在调用执行方法之外按顺序运行宏?. k5 @' y+ X. s5 s$ }4 x
</li>
6 K  a; L2 ^) f) m# o<li>
8 P' N2 R/ B* y+ _: J. Z* ~' C在执行之前设置 <wyn>options mprint mlogic symbolgen;</wyn> 以查看日志中的调试信息?2 S+ B. j8 N# K
</li>$ F/ S: p; \9 T0 O+ C% M' c/ f7 |
<li># ^" p) @! n- e
在您的宏中使用一些 <wyn>%put</wyn> 语句,并在您的 <wyn>call execute</wyn> 数据步中使用 <wyn>put</wyn> 语句,以便查看在各个点生成了什么?0 {: P9 b7 [$ O
</li>
0 G4 l% F1 D1 s( v* G" T9 S6 ?</ul>$ W- N$ X, @$ A3 d& r
<p>建议在开发宏应用程序时首先让代码运行而不使用宏,然后添加宏变量并显式 <wyn>%let</wyn>-ting 其值,然后在宏的上下文中对其进行测试。之后将移至 <wyn>call execute</wyn>。</p>
% I8 H/ i' O. m5 C3 ?/ ^. y& b1 K) v<p>也许尝试以上几点,然后返回一些我们可以调试的日志输出。您发布的代码中还有一些其他错误/问题,但我没有指出它们,而是假设您正在将其缩减为 SO 帖子。</p>1 @) S. X4 m0 m; w/ `8 C8 H
<p>顺便说一句,我喜欢使用 <wyn>data _null_</wyn> 和 <wyn>call execute</wyn> 驱动数据驱动代码的想法,我经常使用这种方法。</p>4 {) ^$ V( S# B9 z+ x
<div class="suo-content"><div style="text-align: right;">/ D9 S2 [- G# s1 Z. D9 O  ^
                <div class="xControl"><i class="fa fa-caret-right"></i>
4 U5 g( P: Y; }  p: T7 ]6 W                        <span class="xTitle"></span>
2 j% f* I, q9 _. C                        相关讨论
2 a& P* S' m! y( Q) P  Q/ ^                        <div style="clear: both;"></div>) g, g- I3 j3 T& h6 F% H2 J" r
                </div>6 S. H4 ]3 i* S" a7 y' c5 y
                <div class="xContent" style="display: none;"><p></p>
  X5 k! o9 `) ]' y* s$ {7 C<ul>1 \9 [3 i- v6 o4 Y. B6 h! o
<li>嗨 sasfrog,我最初确实使用了一个宏,该宏在正常运行宏时起作用,但在调用内部执行</li>
$ \6 i- d- K% \3 K( r) k2 D</ul>
) f3 q  o6 n  G$ _<p></p></div>' x% a3 J" C& O7 f
        </div><p></p></div>
! t9 x& |: N4 L5 X  B7 Z7 t" g% ^5 d<hr></div>
回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|飞雪团队

GMT+8, 2025-12-19 14:47 , Processed in 0.116802 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表