sas宏(2),运行中创建宏与使用宏,多个宏触发器的引用规则、proc sql创建宏, scl中宏处理(暂缺)...

1:在程序运行中进行宏定义

CALL routines that enable you to transfer information between an executing DATA step and the macro processor.

You can use the SYMPUT routine to create a macro variable and to assign to that variable any value that is available in the DATA step.  

When you use the SYMPUT routine to create a macro variable in a DATA step, the macro variable is not actually created and assigned a value until the DATA step is executed.

options symbolgen pagesize=30;  
%let crsnum=3; 
data revenue;    
set sasuser.all end=final; 
where course_number=&crsnum;     
total+1;       
if paid='Y' then paidup+1;    
if final then do;     
    if paidup<total then do;    
        call symput('foot','Some Fees Are Unpaid');     /*symput函数能达到运行时给宏赋值的效果*/
        /*%let foot=Some Fees Are Unpaid; */ /*使用let不能达到想要的效果,let会被宏处理器先执行,优先于data步*/
    end;          
    else do;      
        call symput('foot','All Students Have Paid');   
        /*%let foot=All Students Have Paid;*/  
    end;    
end;   
run;

 

symput函数里面的参数为表达式的情况

里面引用的函数不需要使用%这种宏函数的记号

trim去右尾 left去左尾

call symput('numpaid',trim(left(paidup))); 
CALL SYMPUTX(macro-variable, value <,symbol-table> ); 

 默认去双尾空白,其他效果和symput一样 

随后一个参数表示宏储存的位置'L'=local 'G'=global

 

The SYMPUT routine and the SYMPUTX routine can only create a local macro variable if a local symbol table already exists. If no local symbol table exists when the SYMPUT routine or SYMPUTX routine executes, it will create a global macro variable.

 

依据变量名建立宏

data _null_;
set sasuser.courses;
call symput(course_code, trim(course_title));/*每一个observation中的两个变量分别对于宏值与宏变量*/
run;
%put _user_;

 

多个宏触发器的扫描规则

宏处理器将两个&当做一个看待,所以第一次扫描&&&lv2被处理成&(&lv2)->&lv1,第二次扫描就得出res,先将重复的两个变成1个,然后记住扫描的位置,继续扫面后面的。

而&&lv2第一次扫描被处理成&lv2,结果依然为lv1

The Forward Re-Scan Rule

1:When multiple ampersands or percent signs precede a name token, the macroprocessor resolves two ampersands (&&) to one ampersand (&), and re-scans thereference.

2: To re-scan a reference, the macro processor scans and resolves tokens from left to right from the point where multiple ampersands or percent signs are coded, untilno more triggers can be resolved.

/*想要用lv2来得到res*/
data _null_;
%let lv1 = res; %let lv2 = lv1; %put &lv2; /*一个红触发器无须解释,结果为lv1*/ %put &&lv2; /*这个看上去有理,但是得出的结果还是lv1*/ %put &&&lv2;/*这个得出的结果为res*/ run;

 

2:在程序运行中进行宏值得获取

在程序中进行宏值得获取用&是不行的,这个是在编译前就被执行的语句。

data teachers;
set sasuser.register;
length Teacher $ 20;
teacher=symget('teach'||left(course_number));
run;

 

3:利用proc sql创建宏

This form of the INTO clause does not trim leading or trailing blanks

当时let创建的宏会去掉空白

/*在行数未知的情况下创建多个宏变量,*/
proc
sql noprint; select count(*) into :numrows from sasuser.schedule where year(begin_date)=2002; %let numrows=&numrows; /*消除首尾空白*/ %put There are &numrows courses in 2002; select course_code, location, begin_date format=mmddyy10. into :crsid1-:crsid&numrows, :place1-:place&numrows, :date1-:date&numrows from sasuser.schedule where year(begin_date)=2002 order by begin_date; %put _user_; quit;

crsid1-crsid3 are assigned values of the data set variable Course_code from each of the first three rows,他们分别对应数据集的前三行的观测的值

如果规定的group数大于实际的数量,那么按照实际多少个来创建宏

 

 

create one macro variable that will hold all values of a certain data set variable.

proc sql noprint;
    select distinct location into :sites separated by ' '
        from sasuser.schedule;
quit;

 Global Symbol Table(符号表中,宏与宏值)
Sites Boston Dallas Seattle

 

Proc sql中不会进行自动的数据类型转换。对于你想要使用的数据类型要进行手动转换

 

转载于:https://www.cnblogs.com/yican/p/4106954.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据分析,数据科学及AI算法是当前最热门的职业。这些职业有着共同的特点:面向数字的,针对编程的以及采取分析手段的。 这些当代热点特性使得在就业市场上对以上职位需求激增也就不足为奇了。但是,市场上提供这方面的大型综合的培训课程是有限,如果说有,大多是知识范围狭窄且非综合性的,而且大多培训都缺乏方法论与实务结合。一般的情况是讲师讲述某种语言的一堆代码,学生听完后甚至连使用方法及代码的前提都不清楚,更别提实际应用场景了。这里,掌握一门数据分析软件本身没错,但仅通过单一的编程培训很难获得聘用为数据分析师或数据科学家所需的技能。那我的解决方案是什么呢?首先,我把所有数据分析的典型问题都归类总结出来,再结合相应的实际问题,数据以及案例,同时采用世界上最流行的两种数据分析软件:PYTHON 和 SAS去解决这些问题,并将这些解决方法传授给学生。学生在完成培训后更重要的收获是知道每一问题从产生直至解决的前因后果和应用场景,这是因为我在每一课程章节最前都会交代方法论,知识要点及应用场合。SAS和PYTHON可以一起学吗?当然可以。因为我就是这样做到的。具体步骤是,我在课程当安排了一系列主题,然后使用两种编程语言解决同样的问题。我总结出这样做的好处是边学习边比较,最后在不知不觉当掌握了两门语言的精华和数据分析的通用方法或模式。过程虽有点长,但十分有趣。最后,为了巩固已学的知识和技能,我还专门安排了针对PYTHON 和 SAS的小型项目及详细代码讲解。另外,课程当使用的全部编程代码及数据文件都将免费地提供给注册的学生。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值