SAS Advanced programming 정리- SAS Macro (11)

Hoya Jaeho Lee·2022년 4월 18일
0

SAS Advanced programming

목록 보기
11/17

Defining a Basic Macro

%macro prtlast;
proc print data=&syslast (obs=5);
title "Listing of &syslast data set";
run;
%mend;

코드 설명:
&syslast: 마지막 최근 데이터 셋!!

Defining a Macro with Positional Parameters

%macro printdsn(dsn,vars);
proc print data=&dsn;
var &vars;
title "Listing of %upcase(&dsn) data set";
run;
%mend;

%let dsn=sasuser.courses;
%let vars=course_code course_title days;
%printdsn

The %GLOBAL Statement

%macro printdsn;
%global dsn vars;
%let dsn=sasuser.courses;
%let vars=course_title course_code days;
proc print data=&dsn;
var &vars;
title "Listing of &dsn data set";
run;
%mend;

%printdsn

Global statement:)
1. creates one or more macro variables in the global symbol table and assigns null values to them
2. can be used either inside or outside a macro definition
3. has no effect on variables that are already in the global symbol table.

The %LOCAL Statement

Local 매크로는 macro내에서 local만 적용!
1. can appear only inside a macro definition
2. creates one or more macro variables in the local symbol table and assigns null values to them
3. has no effect on variables that are already in the local symbol table

%let dsn=sasuser.courses;
이렇게 외부 매크로에 명시해도 적용 X
%macro printdsn;
%local dsn;
%let dsn=sasuser.register;
%put The value of DSN inside Printdsn is &dsn;
%mend;

Local statement의 결과
%printdsn
The value of DSN inside Printdsn is sasuser.register
%put The value of DSN outside Printdsn is &dsn;
The value of DSN outside Printdsn is sasuser.courses

Conditional Processing of Parts of Statements

%macro counts (cols=all,rows=,dsn=&syslast);
title "Frequency Counts for %upcase(&dsn) data set";
proc freq data=&dsn;
tables
%if &rows ne %then &rows *;
&cols;
run;
%mend counts;

%counts(dsn=sasuser.all, cols=paid, rows=course_number)
rows와 cols를 두개다 명시했으므로 two way table을 형성
%if ~ %then ~;를 해서 조건을 만족하지 않았을 때 one way table 형성하게 끔 조건:)

만약, rows를 명시하지 않으면 one way table을 형성
%counts(dsn=sasuser.all,cols=paid)

Case Sensitivity in Macro Comparisons

%macro prtlast;
%if &syslast=null %then %do;
%put No data sets created yet.;
%end;
%else %do;
proc print;
title "Last Created Data Set is &syslast";
run;
%end;
%mend;

options mprint mlogic symbolgen;
%prtlast

만약 데이터가 null일때 나오는 log창

Processing Statements Iteratively

data null;
set sasuser.schedule end=nomore;
call symput('teach'||left(_n
),(trim(teacher)));
if nomore then call symput('count',_n);
run;

%macro putloop;
%local i;
%do i=1 %to &count;
%put TEACH&i is &&teach&i;
%end;
%mend putloop;

%putloop

%macro hex(start=1,stop=10,incr=1);
%local i;
data null;
%do i=&start %to &stop %by &incr;
value=&i;
put "Hexadecimal form of &i is " value hex6.;
%end;
run;
%mend hex;

options mprint mlogic;
%hex(start=20,stop=30,incr=2)

Generating Complete Steps

%macro readraw(first=1999,last=2005);
%local year;
%do year=&first %to &last;
data year&year;
infile "raw&year..dat";
input course_code $4.
location $15.
begin_date date9.
teacher $25.;
run;

proc print data=year&year;
title "Scheduled classes for &year";
format begin_date date9.;
run;
%end;
%mend readraw;

%readraw(first=2000,last=2002)

The %EVAL Function

conditional 일땐,
values=0 (거짓), values=1 (참)

마지막 예에선, the decimal value of the real variable causes an error message to be written to the SAS log

이 문제(integer만 호환하고 소수점 호환하지 못하는 문제)를 해결하기 위해서, sysevalf 사용

the %EVAL function generates ERROR messages in the log when it encounters an expression that contains non-integer values. In order to avoid these ERROR messages, you can use the %SYSEVALF function

sysevalf

evalf의 문제를 해결하기 위해: 정수형+소수점도 같이 호환 가능

%macro figureit(a,b);
%let y=%sysevalf(&a+&b);
%put The result with SYSEVALF is: &y;
%put BOOLEAN conversion: %sysevalf(&a +&b, boolean);
%put CEIL conversion: %sysevalf(&a +&b, ceil);
%put FLOOR conversion: %sysevalf(&a +&b, floor);
%put INTEGER conversion: %sysevalf(&a +&b, integer);
%mend figureit;

%figureit(100,1.59)

profile
Biostatistics researcher Github: https://github.com/hoyajhl

0개의 댓글