/*F3/F8, Ctrl+[, Alt+[*/
data _null_;
call symput('macvar','macro variable value or dataset var');
run;
%put 'macvar=' &macvar;
data callerHour;
input gapSec;
datalines;
234
123
456
;
run;
/*create macro vars by data step symput */
/*macro value from a dataset*/
data _null_;
set callerHour;
call symput('gapSecs','gapSec');
%put 'gapSec=' &gapSecs;
run;
data _null_;
call execute('proc print data=callerHour;');
call execute('run;');
run;
/*create macro vars by proc SQL */
proc sql noprint;
select memlabel into :dslabel
from dictionary.tables
where libname='SASHELP' and memname='RETAIL';
quit;
proc sql noprint;
select gapSec into :gaps separated by ','
from dictionary.tables
where libname='WORK' and memname='CallerHour';
quit;
proc sql ;
select *
from dictionary.tables
where libname='WORK' and memname='CallerHour' ;
quit;
proc sql noprint;
select gapSec into :mGapSec
from callerHour;
quit;
proc sql noprint;
select gapSec into :m1-:m3
from callerHour;
quit;
proc sql noprint;
select gapSec into :gapList separated by ','
from callerHour;
quit;
%put &gapList;
%put &gapList;
%put 'gapSec=' &mGapSec;
%put 'gapSec=' &m1 ;
%put 'gapSec=' &m2 ;
%put 'gapSec=' &m3 ;
%put &dslabel;
Label var1='Patient #'
var2='Date of Visit'
var3='Date of Birth';
/*dynamic reference of data by var name of a dataset */
libname sdata "E:\ChenyxFiles\2014.Study.TrainingCertificate\SAS.Victoria\SAS.DS2\SASUniversityEdition\myfolders\sData";
/*
Min,Yr,agent,cDay,cFullTime,card_no,charge,gapSec,region
var
table--> get from dataset, not from input.
sql macro var
output
*/
/*********************************************/
data mVar;
input var $ 1-9 label $ 11-19;
cards;
Mn Mn1
Yr Yr1
agent agent1
cDay cDay
cFullTime cFullTime
card_no card_no
charge charge
gapSec gapSec
region region
;
run;
proc sql noprint;
select var into :var1-:var999 from mVar;
select label into :lbl1-:lbl999 from mVar;
select count(*) into :numVar from mVar;
quit;
/*
data smBill;
set sData.smBill;
run;
*/
%macro runit;
proc datasets lib=work;
modify smBill;
label
%do a=1 %to &numVar;
&&var&a ="&&lbl&a."
%end;
;
quit;
%mend runit;
%runit;
proc contents data=smBill;
run;
/*********************************************/
%macro printit;
proc print data=smBill;
var
%do a=1 %to %eval(&numvar);
&&var&a
%end;;
quit;
%mend;
%printit;
%macro printit;
proc print data=smBill;
var
%do a=1 %to &numvar;
&&var&a
%end;;
quit;
%mend;
%printit;
/*Check the varibles in dataset work.smbill*/
proc sql noprint;
select name into :billVar1-:billVar10 from dictionary.columns where
Libname="WORK" and memname=upcase("smbill");
select count(name) into :billVarCount from dictionary.columns where
Libname="WORK" and memname=upcase("smbill");
quit;
%put &billVarCount;
%macro mPrint;
%do a=1 %to %eval(&billVarCount);
%put &&billVar&a;
%end;
%mend;
%mPrint;
/************************************************/
/*CHECKING TO SEE IF A DATA SET EXISTS*/
proc sql noprint;
select left(put(count(*),8.)) into :exist
from dictionary.tables
where libname='WORK' and memname=upcase('smBill');
quit;
%put ∃
proc sql;
select * from smbill;
quit;
proc contents data=work.smbill;
run;
/*GENERATING SAS CODE, WRITING IT TO AN EXTERNAL FILE,
AND %INCLUDING IT AT A LATER TIME */
data _null_;
file 'E:\ChenyxFiles\2014.Study.TrainingCertificate\SAS.Victoria\SAS.DS2\SASUniversityEdition\myfolders\Macro.OOP\m.sas';
put @1 'proc datasets lib=work;';
put @4 'modify smBill;';
put @4 'label ';
do until(last);
set mVar end=last;
put @7 var ' =" ' label ' " ';
end;
put @4 ';';
put @1 'quit;';
stop;
run;
%include 'E:\ChenyxFiles\2014.Study.TrainingCertificate\SAS.Victoria\SAS.DS2\SASUniversityEdition\myfolders\Macro.OOP\m.sas';
/*GENERATING AND EXECUTING SAS CODE USING CALL EXECUTE*/
data _null_;
call execute('proc datasets lib=work;');
call execute('modify smbill;');
call execute('label ');
do until(last);
set mVar end=last;
call execute(var||'="'||label||'"');
end;
call execute(';');
call execute('quit;');
stop;
run;
/*CHECKING TO SEE IF A DATA SET IS EMPTY*/
proc sql noprint;
select left(put(nobs,8.)) into :nobs
from dictionary.tables
where libname='WORK' and
memname=upcase('smbill');
quit;
%put &nobs;
/*pass an array or an object in macro*/
%macro interface(char, num, array, macro);
%local major;
%macro breakarray(m, n, p, q);
data _null_;
call symput('major',&m);
run;
%put &m &n &p &q;
%mend breakarray;
%macro submacro(a, b);
%put &a &b;
%mend submacro;
%put &char &num &array ¯o;
* Break up the array;
%breakarray&array.;
%put &major;
* Call the named macro;
%¯o.;
%mend interface;
%interface (char=character, num=2005,
array=(1,2,3,4),
macro=submacro(a=5,b=c));
/*call a method by macro*/
%macro functions(function, array);
%macro subtract(p1,p2,p3);
data _null_;
result = &p1 - &p2 - &p3;
put result=;
run;
%mend subtract;
%macro add(p1,p2,p3);
data _null_;
result = &p1 + &p2 + &p3;
put result=;
run;
%mend add;
%if &function. = subtract or &function. = add %then
%&function.&array.;
%mend functions;
%functions (function=add, array=(p1=45,p2=34,p3=34));
No comments:
Post a Comment