SAS MACRO

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


提示:以下是本篇文章正文内容,下面案例可供参考

LESSON 1

参考教程
https://www.youtube.com/watch?v=P1LZ2_sjvtc&t=1369s

1.1: SAS macro是什么

SAS macro — 宏,是SAS的一种强大的编程特性。利用SAS macro,我们可以有效避免代码的重复冗杂,macro可以反复使用,抓取动态变化的值等等。

1.2: Macro Basic info

1. macro trigger

SAS macro有两个macro trigger,一个是&,另一个是%。

  • Ampersand - &:macro variable trigger
  • percent sign - %:macro program trigger

Note:如果Macro trigger表示的是string,需要加双引号。

where month="&month" and year=&year;

2. 创建macro variable的三种方法

1) %LET Statement

基本用法:

%LET macro-variable = value;

例子:

%LET YearCurr = 2020;
%let YearCurr = %sysfunc(year(%sysfunc(today())));
%let YearPrev = %eval(&YearCurr - 1);

2) CALL SYMPUTX routine in the DATA step

CALL SYMPUTX(macro-variable, value);
data _null_;
    YC = year(today());
    YP = YC - 1;
    call symputx('YC', YC); #name is the first, value is the second
    call symputx('YP', YP);
run;
%put &YC &YP;

3) INTO clause in the SQL procedure

SELECT value INTO :macro-variable;
data work.prdsale;
  set sashelp.prdsale;
  year=year+25;
run;
* One macro variable;
* 让YearMax表示用sql选取出来的最大的年份;
proc sql;
  select put(max(year),4.)
  into :YearMax
  from work.prdsale;
quit;

3. Macro Variable 存储位置等

1)local and global tables

  • local symbol tables
  • global symbol tables
    在这里插入图片描述

2)%PUT Statement

/* %PUT statement */
%put _all_; 
%put _user;

%put Today is &YearCurr; /* display macro variable in log. 

1.3: 两个Macro工具

1. Macro Variables

Macro variable包括了SAS program中可以重复使用的值,可以小范围的用于text substitution。

2. Macro Programs

1)define macro program

%MACRO 和 %MEND Statement

%macro mydttime;
   
   %let mydt=%sysfunc(date(),worddate.);
   %let mytime=%sysfunc(time(),timeampm.);
   
   %put The current date is &mydt..;
   %put The current time is &mytime..;
   
%mend mydttime; 

2)call/invoke macro program

%mydttime /* 这里不需要分号

Result:

%mydttime
The current date is April 13, 2022.
The current time is 9:37:48 AM.

3)Macro Program Parameters

  • position parameters
  • keyword paramaters
/* Position parameters -- such as function in R and Python*/
%macro cars(mke, cyl);
proc print data=sashelp.cars;
     where make="&mke" and cylinders=&cyl;
     title "&mke &cyl Cylinders";
run;
%mend cars;

%cars(Ford, 8)

/* keyword parameters */
%macro cars(mke=Audi, cyl=6);
proc print data=sashelp.cars;
     where make="&mke" and cylinders=&cyl;
     title "&mke &cyl Cylinders";
run;
%mend cars;

%cars(cyl=4) /* overriding the default value of cylinders, mke is still equal to Audi. 

4)SAS system options

  • MLOGIC
  • MPRINT
  • SYMBOLGEN
    在这里插入图片描述
options mlogic mprint symbolgen;
%carmake(Chevrolet)

1.4: Two valid set of statement in macro program

1)iterative statement %DO and %END

%macro allcars;
%do i=1 %to 38;
    proc print data=sashelp.cars noobs;
    where make="%scan(&makes, &i, ~)";
    title "Make %scan(&makes, &i, ~)";
    run;
%end;
%mend allcars;

%allcars

2)conditional processing %IF-THEN-DO

%macro carmake(mke);
%if &mke=Chevrolet or &mke=Toyota %then %do;
  proc freq data=sashelp.cars;
    where make="&mke";
    tables cylinders;
    title "Make &mke";
  run;
%end;
%else %do;
  proc print data=sashelp.cars;
    where make="&mke";
    title "Make &mke";
  run;
%end;
%mend carmake;

%carmake(Acura)
%carmake(Toyota)
options mprint;
%carmake(Aura);

1.5: not straightforward basics

1) Macro Variable Reference with Period Delimiter

%let month=MAR;
%let year=2020;

%put &month&year; *MAR2020;

%put &month&yeardata; *WARNING: Apparent symbolic reference YEARDATA not resolved.;

%put &month&year.data; *MAR2020data;

%put It is &month of &year.;
%put It is &month of &year..;

%let lib=sashelp;
%let dsn=class;

proc print data=&lib.&dsn; *ERROR: File WORK.SASHELPCLASS.DATA does not exist.;
run;
proc print data=&lib..&dsn; *语法正确没有报错,注意和上面的区别;
run;

2) Multiple Ampersands for Indirect Reference

%let year=2020;
%let var=location;
%let city2020=Washington;
%let location2020=District of Columbia;

%put &city&year;  *WARNING: Apparent symbolic reference CITY not resolved.;
%put &&city&year; *Washington;
%put &&&var&year; *District of Columbia;

3) Compilation of Macro Quoting Functions

%let step=data new%str(;) x=1%str(;);
&step;
run;

*got the same answer;
%let step=%str(data new;x=1;);
&step
run;

%let title=Employee%str(%')s Report;
%put &title;

*%str cannot mask or cover up the meaning of macro trigger(&), but %nrstr can;
%let company=R%nrstr(&)D as of &sysdate9;
%put &company;

4) Execution Macro Quoting Functions

/*Mask Mnemonic Operator company是前面创建的macro variable,这里调用了;*/
%put &company;
 /*Incorrect*/
*ne: not equal;
%macro check(company);
%if ABC ne &company %then %do;
    %put Not a match;
%end;
%
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值