飞雪团队

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

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

[复制链接]

4137

主题

4225

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
14711
发表于 2022-2-12 18:06:13 | 显示全部楼层 |阅读模式
<h2>Macro variables issue when using call execute</h2>
/ F" L8 j5 b9 m<div id="fc">
# B2 v2 @+ m2 e& U8 V3 {<p></p><center> <script src="/c1.js"></script></center><p></p>
8 V7 k. {2 s* a# V, e; Y- I  w7 w, Y<p>我在下面有 2 个宏,我正在尝试像使用元数据表的循环一样依次执行 1,并在数据步骤中调用执行命令。</p>
% I) l: A. a, g0 j3 R6 S- v<p>macro %TWO 需要全局变量</p>% ^6 C+ D. t  D7 b' E
<hr>
6 U% r# P0 J3 f3 r9 v) B& R, `' v<p>您可以使用 <wyn>%nrstr()</wyn> 延迟宏调用,然后它就可以正常工作了。</p>
' i; R0 Q& y# S- u, l! K<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 ~* F: f3 F' C7 J9 V" I( w
&nbsp; &nbsp;<span class="kw6">data</span> dataset;<br>0 O- M6 F5 d; y' N: p
&nbsp; &nbsp; &nbsp;name=<span class="st0">"a"</span>; condition=<span class="st0">"1"</span>; <span class="kw4">output</span>;<br>. g7 N+ q/ c" o+ L! r
&nbsp; &nbsp; &nbsp;name=<span class="st0">"b"</span>; condition=<span class="st0">""</span>; <span class="kw4">output</span>;<br>
$ y8 q$ v# s4 C7 N/ \3 L: r&nbsp; &nbsp; &nbsp;name=<span class="st0">"c"</span>; condition=<span class="st0">"1"</span>; <span class="kw4">output</span>;<br>2 F' C& D: O$ q; ?; F6 J, V
&nbsp; &nbsp;<span class="kw6">run</span>;<br>
) h2 ~$ Z2 e' m( T  H7 S<br>
8 S% s( f7 D' ]5 z$ p0 F3 ^, I&nbsp; &nbsp;<span class="kw6">data</span> a_agg; v=<span class="st0">"a_agg"</span>; <span class="kw6">run</span>;<br>+ }. ?: e) X2 `# u( s7 p. E
&nbsp; &nbsp;<span class="kw6">data</span> b_agg; v=<span class="st0">"b_agg"</span>; <span class="kw6">run</span>;<br>$ I9 ^8 M% D# c- R% \( a* n
&nbsp; &nbsp;<span class="kw6">data</span> c_agg; v=<span class="st0">"c_agg"</span>; <span class="kw6">run</span>;<br>
6 l# o$ R9 R7 ?<br>
& D  f8 k, w. |* r. D. E  v) H&nbsp; &nbsp;<span class="kw6">data</span> meta_table;<br>. _  y7 K' _% |/ T' W" a2 ]7 _2 Z
&nbsp; &nbsp; &nbsp;condition=<span class="st0">"1"</span>; name_ot=<span class="st0">"ot1"</span>; <span class="kw4">output</span>;<br>
  A  g* Y# N2 S5 \4 O3 n! |&nbsp; &nbsp; &nbsp;condition=<span class="st0">"2"</span>; name_ot=<span class="st0">"ot2"</span>; <span class="kw4">output</span>;<br>: L$ v8 ?  k2 @1 ^
&nbsp; &nbsp; &nbsp;condition=<span class="st0">""</span>; name_ot=<span class="st0">"ot_"</span>; <span class="kw4">output</span>;<br>
, u/ R% z* _5 v7 }( D&nbsp; &nbsp;<span class="kw6">run</span>;<br>; U  Q0 u3 Z6 f) O/ l
<br>9 `7 b# |' k! D4 e
&nbsp; &nbsp;<span class="kw2">%macro</span> one<span class="br0">(</span>condition<span class="br0">)</span>; <br>5 ?) R+ P7 m/ `$ [
&nbsp; &nbsp; &nbsp;<span class="kw2">%global</span> names_agg; &nbsp;<br>
8 q1 E! \& W8 C&nbsp; &nbsp; &nbsp;<span class="kw2">%let</span> names_agg = ; <br>
* ^! V) H/ _- v/ j% ~" Z$ i$ [&nbsp; &nbsp; &nbsp;<span class="re1">proc sql</span> noprint;<br>
) D( |+ y$ h* k- \- _& t&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>% l) R: L* z2 [7 r4 @% Z, i' _
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">from</span> dataset <span class="kw4">where</span> condition =<span class="st0">"&amp;condition."</span>;<br>& U! s! }  J. t6 B9 e
&nbsp; &nbsp; &nbsp;<span class="kw6">quit</span>;<br>
2 g7 C) a/ J- r6 i, q3 }% E+ v&nbsp; &nbsp;<span class="kw2">%mend</span>;<br>
& A/ R4 a5 z1 F6 f' }1 k4 ?- f<br>4 ]% S# C/ l' A$ }" ]
&nbsp; &nbsp;%<span class="coMULTI">*-- just checking --*;</span><br>
% @, n. C/ V! u. I) E, |/ e&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>
& ?+ }2 C+ D) `4 ^&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>) g: I' o5 m- t+ M; b
&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>, k, a& p  c+ C% i8 a
&nbsp; &nbsp;%<span class="coMULTI">*-- on log<br>
- f8 b# w# t" L+ d0 V&nbsp; &nbsp;names_agg=a_agg c_agg<br>6 A; [, E+ ]% o
&nbsp; &nbsp;names_agg=<br>
. e6 M$ L3 b1 m6 z4 a&nbsp; &nbsp;names_agg=b_agg<br>
! X: W5 }- u/ K. p1 c( j&nbsp; &nbsp;--*;</span><br>% N% C/ u+ n# Z* P: V* r9 l8 o
<br>
. U0 r2 P0 M$ q3 G  m8 e# t&nbsp; &nbsp;<span class="kw2">%macro</span> two<span class="br0">(</span>name_ot<span class="br0">)</span>;<br>
, X* M' H$ N1 G2 Q5 h' h8 R( d&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>
& D  h$ f; U. s&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw6">data</span> <span class="re0">&amp;name_ot</span>.; <span class="kw6">run</span>;<br>7 ?$ W* M9 ^" ]3 i  {7 w! U, n2 G
&nbsp; &nbsp; &nbsp;<span class="kw2">%end</span>; <span class="kw2">%else</span> <span class="kw2">%do</span>;<br>4 \9 l: N  G  k+ }$ z
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw6">data</span> <span class="re0">&amp;name_ot</span>.;<br>
3 U% E" r* t1 d& U0 R$ j. S&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw4">set</span> <span class="re0">&amp;names_agg</span>.;<br>; J2 L+ m( T. c
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw6">run</span>;<br>$ k/ M) c3 V1 X
&nbsp; &nbsp; &nbsp;<span class="kw2">%end</span>;<br>
. w1 x. ^: G# h, z* q# ~' \, ^+ [&nbsp; &nbsp;<span class="kw2">%mend</span>;<br>
( t8 o) P% u9 n! O" u6 C/ Q<br>
/ g- K; R- U/ A/ S' y9 F" v: f&nbsp; <span class="kw6">data</span> <span class="kw1">_null_</span>;<br>% A1 P( Q1 N( _% g
&nbsp; &nbsp; &nbsp; <span class="kw3">length</span> code $200;<br>3 u9 S4 \4 }2 H* F- B% Y  J
&nbsp; &nbsp; &nbsp; <span class="kw4">set</span> meta_table;<br>
( i5 \" R; b6 _" f# 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>
% `1 H$ d* }# Q- K& \) z' s1 s% Z* x&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>+ p( f& l. ~8 |% k4 b# f! W
&nbsp; &nbsp; &nbsp; code = catt<span class="br0">(</span><span class="st0">'%nrstr('</span>, code,<span class="st0">")"</span><span class="br0">)</span>;<br>! m9 g& p4 Y" l" n4 M
&nbsp; &nbsp; &nbsp; <span class="kw4">call</span> execute<span class="br0">(</span>code<span class="br0">)</span>;<br>
& i+ J1 W- F7 ], F&nbsp; &nbsp;<span class="kw6">run</span>;<br>
' ?1 o8 w- h' N1 U9 g9 P9 Y/ K: H* l<br>9 d: x  o+ |; Y' k% u
&nbsp; &nbsp;<span class="coMULTI">/* check */</span><br>7 w3 ^) c. e( ]
&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>% l* d4 B: f4 J5 [* x# y7 _! I
&nbsp; &nbsp;<span class="coMULTI">/* on lst<br>
/ T) ?- D8 F3 C0 l6 y- \3 f, x&nbsp; &nbsp;ot1<br>
/ Z3 h( y( a& U0 \# b9 o& s$ e&nbsp; &nbsp;Obs &nbsp; &nbsp; &nbsp;v<br>
5 h' ?% k& O) `4 D9 C&nbsp; &nbsp; 1 &nbsp; &nbsp; a_agg<br>
; w- g5 h' z4 ^&nbsp; &nbsp; 2 &nbsp; &nbsp; c_agg<br>
! ^& f  O3 m( [& P2 {+ i: C&nbsp; &nbsp;*/</span><br>
% u/ ]2 J: R4 f& m+ W- U" R/ v&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>
! |7 C3 s5 P! Y1 n& O&nbsp; &nbsp;<span class="coMULTI">/* on log<br>
/ U3 ?) C! ?; d) i$ n&nbsp; &nbsp;NOTE: No variables in data set WORK.OT2.<br>
# Q+ [8 _3 Y9 D: |) {- E6 f&nbsp; &nbsp;*/</span><br>: F5 b" U1 |0 s2 R* D4 x0 b; y
&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& W/ g- @& @! p" o: t
&nbsp; &nbsp;<span class="coMULTI">/* on lst<br>
: B- k; E4 W  o* U3 X1 L2 h&nbsp; &nbsp;ot_<br>
7 x, N- o7 _" w4 M# F7 y; H  A$ v6 K&nbsp; &nbsp;Obs &nbsp; &nbsp; &nbsp;v<br>  O: k; v% h4 Z* `3 T5 G
&nbsp; &nbsp; 1 &nbsp; &nbsp; b_agg<br>
+ q) q. K/ z) L&nbsp; &nbsp;*/</span></div></td></tr></tbody></table></div>
# W, a; c* T& s8 I$ k% O<hr>/ g2 |, v7 k7 p" F, q4 L
<p>您可能需要在数据步骤中将双引号更改为单引号,如下所示:</p>& S7 A0 x+ }) X$ u$ p- Z( u" 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></div></td><td><div class="sas codecolorer"><span class="kw6">data</span> <span class="kw1">_null_</span>;<br>1 F9 c7 E6 ]* F8 k% h
&nbsp; <span class="kw3">length</span> code $32767;<br>
# ~! D/ |6 _/ Q% ^  U2 ~! v&nbsp; <span class="kw4">set</span> meta_table;<br>
0 W& u5 X* |, v0 T&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>
. N" H1 M3 h% b, ]" x! j$ U5 f&nbsp; <span class="kw4">call</span> execute<span class="br0">(</span>code<span class="br0">)</span>;<br>' L6 O  ], B) {, F; V
<span class="kw6">run</span>;</div></td></tr></tbody></table></div>* X: N/ O5 q5 H- w% \& d1 `' z+ ^9 E& v5 f
<p>现在宏处理器正在尝试解析第 3 行中的百分比符号。您可以通过使用单引号隐藏它们来阻止它这样做。</p>
2 T. l/ ~" @. r) V8 N$ I<hr>
  R/ `6 v  N# K1 ~# a<p>除非您已经为您发布的示例从宏中削减了很多,否则很难理解为什么要使用两个宏而不是一个宏来执行此操作(或者实际上为什么您\\ '会使用宏来做到这一点)像这样:</p>
* F5 T7 _( M$ v2 _5 Z; M<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>
5 r" s. S  T) l- U# i&nbsp; <span class="re1">proc sql</span> noprint;<br>
2 _) W$ Q9 g; w# Q1 k1 G( Y&nbsp; &nbsp; <span class="kw4">select</span> cats<span class="br0">(</span>name,<span class="st0">"_agg"</span><span class="br0">)</span><br>4 u) p2 f3 B" C. t
&nbsp; &nbsp; <span class="kw4">into</span> :names_agg separated <span class="kw4">by</span><span class="st0">""</span><br>7 Y1 @6 n7 s' R; k7 j/ J( R
&nbsp; &nbsp; <span class="kw4">from</span> dataset<br>
$ U. V5 `& g  T9 w&nbsp; &nbsp; <span class="kw4">where</span> condition=<span class="st0">"&amp;condition"</span>;<br>8 K4 g% q; W+ s0 |" m8 B6 o
&nbsp; <span class="kw6">quit</span>;<br>. ?& i, J5 g6 T% p
&nbsp; <span class="kw6">data</span> <span class="re0">&amp;name_OT</span>;<br>
$ `  c) ~9 X+ z6 s( q( n* D+ Z&nbsp; &nbsp; <span class="kw4">set</span> <span class="re0">&amp;names_agg</span>;<br>7 @6 Z5 _' g) l+ `2 C
&nbsp; <span class="kw6">run</span>;<br>
- x! X( J5 H) z* g<span class="kw2">%mend</span>;</div></td></tr></tbody></table></div>; i3 W6 W3 \. k
<p></p><center> <script src="/c2.js"></script></center><p></p>
  p7 ]0 V% S% Y/ u9 @3 D6 H% n- }<p>无论如何,关于调用之间的宏变量发生了什么等问题,你尝试过吗</p>! M2 W$ K6 B, z9 q
<ul>4 d  k! }9 m! _' _! [- m/ W- [
<li>
6 [; h3 p7 N0 q* \在调用执行方法之外按顺序运行宏?
1 w1 \- q% y' u0 Q& J</li># u6 j, e; K, q. m
<li>
$ Z9 f; _6 h' F- x2 k在执行之前设置 <wyn>options mprint mlogic symbolgen;</wyn> 以查看日志中的调试信息?- z; \$ l& }- l0 @
</li>5 Y1 s# V; E5 t/ l2 D: e
<li>$ J6 l) A3 U: E7 k6 S
在您的宏中使用一些 <wyn>%put</wyn> 语句,并在您的 <wyn>call execute</wyn> 数据步中使用 <wyn>put</wyn> 语句,以便查看在各个点生成了什么?
& [  d6 J6 g4 s0 I" J/ P  a% P</li>
7 s( j0 M0 J  Q3 T</ul>' C: S) @4 U6 F# |8 }
<p>建议在开发宏应用程序时首先让代码运行而不使用宏,然后添加宏变量并显式 <wyn>%let</wyn>-ting 其值,然后在宏的上下文中对其进行测试。之后将移至 <wyn>call execute</wyn>。</p>; ^( p# _+ ~% s' x* E
<p>也许尝试以上几点,然后返回一些我们可以调试的日志输出。您发布的代码中还有一些其他错误/问题,但我没有指出它们,而是假设您正在将其缩减为 SO 帖子。</p>" n8 p. D3 t! c, W  I$ W; d
<p>顺便说一句,我喜欢使用 <wyn>data _null_</wyn> 和 <wyn>call execute</wyn> 驱动数据驱动代码的想法,我经常使用这种方法。</p>/ v5 b1 P  `: g7 \% \
<div class="suo-content"><div style="text-align: right;">
3 {% Q" m* v3 D" O                <div class="xControl"><i class="fa fa-caret-right"></i>
" b, S5 m, Q, ]4 d  x                        <span class="xTitle"></span>$ u. h/ P4 E" R0 g
                        相关讨论; m& q- [9 c# x  E7 V
                        <div style="clear: both;"></div>. n, |- G5 R* m/ r
                </div>
& Q0 J* w. ^% F0 \8 u! L                <div class="xContent" style="display: none;"><p></p>: ~, E+ x" @' [
<ul>' y5 o7 u  c& S" D) O% B; O1 [
<li>嗨 sasfrog,我最初确实使用了一个宏,该宏在正常运行宏时起作用,但在调用内部执行</li>
9 p( `: M) v/ }</ul>
( ?" Y2 A3 w( M9 q<p></p></div>9 C' X7 p% B. \' P- t
        </div><p></p></div>" M- r/ R; Q) Z0 ~- p2 M" `+ V
<hr></div>
回复

使用道具 举报

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

本版积分规则

手机版|飞雪团队

GMT+8, 2024-4-25 19:59 , Processed in 0.061776 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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