楼主: melody21
8416 13

请教:macro 定义下的global variable 问题 [推广有奖]

  • 4关注
  • 6粉丝

讲师

49%

还不是VIP/贵宾

-

威望
0
论坛币
3199 个
通用积分
462.0354
学术水平
36 点
热心指数
48 点
信用等级
36 点
经验
138 点
帖子
349
精华
0
在线时间
544 小时
注册时间
2007-7-14
最后登录
2023-2-4

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
原题是adv130中的一题,程序为:

%macro location;
data _null_;
call symput('dept','sales');
run;
%let country=Germany;
%put _global_;
%mend;
%let company=ABC;
%location

-----题目问:被写入SAS log的宏变量有哪些?
我run的结果确实与答案一致,被写入的宏变量有 dept 和company
company好理解。
但定义macro location的时候,利用symput创建的宏变量 dept怎么也是global呢?
求解,谢谢各位~

搭车问跟这一题很类似的另一问题:

%macro location(country);
proc sql;
select 'sales',* into:dept
from sashelp.class;
quit;
%put _global_;
%mend;
%location(US)
程序RUN的时候有warning:INTO clause specifies fewer host variables than columns listed in the SELECT clause.
这个意思是说,创建的宏变量数量过少,对不?
这里的 select‘sales’,* ---怎么理解呢?sales是啥?为什么要加‘’,后面为啥是* ?

谢谢~~



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Variable Global Macro lobal ABLE company country null 程序

回帖推荐

pobel 发表于2楼  查看完整内容

Call SYMPUT() 会将创建的宏变量存入到最近的非空的symbol table 中,如果运行到call symput时宏location里已经有了局部宏变量,那call symput创建的宏变量就是local的,否则就会存到更外层。 SAS HELP: "SYMPUT puts the macro variable in the most local nonempty symbol table. A symbol table is nonempty if it contains a value a computed %GOTO (A computed %GOTO contains % or & and resolves to a la ...
沙发
pobel 在职认证  发表于 2013-1-14 10:09:38 |只看作者 |坛友微信交流群
Call SYMPUT() 会将创建的宏变量存入到最近的非空的symbol table 中,如果运行到call symput时宏location里已经有了局部宏变量,那call symput创建的宏变量就是local的,否则就会存到更外层。

SAS HELP:
"SYMPUT puts the macro variable in the most local nonempty symbol table. A symbol table is nonempty if it contains
     a value
     a computed %GOTO (A computed %GOTO contains % or & and resolves to a label.)
     the macro variable &SYSPBUFF, created at macro invocation time.
"
下面的程序就会把call symput创建的变量存到local里面:
  1. %macro location(parameter);
  2. data _null_;
  3. call symput('dept1','sales');
  4. run;
  5. %put _local_;
  6. %put;
  7. %put _global_;
  8. %let country=Germany;
  9. %mend;
  10. %let company=ABC;
  11. %location(whatever)
复制代码
  1. %macro location;
  2. %let country=Germany;
  3. data _null_;
  4. call symput('dept2','sales');
  5. run;
  6. %put _local_;
  7. %put;
  8. %put _global_;
  9. %mend;
  10. %let company=ABC;
  11. %location
复制代码
已有 3 人评分学术水平 热心指数 信用等级 收起 理由
420948492 + 1 + 1 观点有启发
melody21 + 1 + 1 热心帮助其他会员
webgu + 1 + 1 + 1 pobel大侠一如既往的热心

总评分: 学术水平 + 3  热心指数 + 3  信用等级 + 1   查看全部评分

和谐拯救危机

使用道具

藤椅
pobel 在职认证  发表于 2013-1-14 10:15:17 |只看作者 |坛友微信交流群
后面那个问题:

用select into 创建宏变量的时候,会将SELECT中第一个变量的值放到INTO所指定的第一个宏变量中,第二个变量的值放到第二个宏变量中, ... ...

因此,变量的个数应该是一致的,例子中into后面只跟了一个变量,而却SELECT了多个,因此会有warning。
该例子执行的结果应该是将宏变量dept的值设定为sales,而与from的数据集没有关系。
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
melody21 + 1 + 1 + 1 热心帮助其他会员

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

和谐拯救危机

使用道具

板凳
melody21 发表于 2013-1-15 00:06:52 |只看作者 |坛友微信交流群
谢谢指教~ :)

使用道具

报纸
elynxue 发表于 2014-8-13 10:46:35 |只看作者 |坛友微信交流群
那为什么没有COUNTRY呢

使用道具

地板
elynxue 发表于 2014-8-13 10:51:00 |只看作者 |坛友微信交流群
pobel 发表于 2013-1-14 10:09
Call SYMPUT() 会将创建的宏变量存入到最近的非空的symbol table 中,如果运行到call symput时宏location里 ...
大侠写的程序多次重复
%put _local_;
%put;
%put _global_;
是起到什么作用呢?多谢了!

使用道具

7
elynxue 发表于 2014-8-13 10:51:44 |只看作者 |坛友微信交流群
那为什么没有COUNTRY呢

使用道具

8
pobel 在职认证  发表于 2014-8-13 11:45:54 |只看作者 |坛友微信交流群
elynxue 发表于 2014-8-13 10:51
大侠写的程序多次重复
%put _local_;
%put;
就是要在log里显示_local_或_global_的宏变量

使用道具

9
420948492 发表于 2014-8-13 12:39:23 |只看作者 |坛友微信交流群
pobel 发表于 2013-1-14 10:09
Call SYMPUT() 会将创建的宏变量存入到最近的非空的symbol table 中,如果运行到call symput时宏location里 ...
觉得这个问题是不是应该分解来看。
在没有宏参数的情况下,这样创建的就是dept2就是全局宏变量
  1. %macro location;
  2. data _null_;
  3. call symput('dept2','sales');
  4. run;
  5. %put _local_;
  6. %put;
  7. %put _global_;
  8. %mend;
  9. %let company=ABC;
  10. %location;
复制代码
在有宏参数的情况下,宏参数会存放到局部宏表中,此时局部宏表非空,此时通过call symput产生的就是宏变量就是dept2就是local。
  1. %macro location(parameter);
  2. data _null_;
  3. call symput('dept2','sales');
  4. run;
  5. %put _local_;
  6. %put;
  7. %put _global_;
  8. %mend;
  9. %let company=ABC;
  10. %location;
复制代码
宏内部的LET起到了宏参数的作用

使用道具

10
pobel 在职认证  发表于 2014-8-13 12:50:19 |只看作者 |坛友微信交流群
420948492 发表于 2014-8-13 12:39
觉得这个问题是不是应该分解来看。
在没有宏参数的情况下,这样创建的就是dept2就是全局宏变量在有宏参 ...
还需要考虑在global symbol table中,call symput要创建的宏变量是否存在。

%let dept2=dept2 in open code;

%macro location(parameter);
data _null_;
call symput('dept2','sales');
run;
%put _local_;
%put;
%put _global_;
%mend;
%let company=ABC;

%location()
%put dept2: &dept2;

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 17:09