<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[SAS 宏]]></title><description><![CDATA[<pre><code>/*2 宏Macro*/
/*2.1 宏变量*/
%let name1=name;
%let name2=‘name’;
proc print data=xie.class;
where height=67;
title “&amp;name1 ‘s height is 67.”;
title2 “&amp;name2 ‘s height is 67.”;
run;
</code></pre>
<pre><code>/*2.2  定义宏*/
/*参数宏*/
%macro try(proc,dsn);
proc &amp;proc data=&amp;dsn;
run;
%mend try;
%try(print,xie.class);
</code></pre>
<pre><code>/*无参数宏*/
%macro plot;/*开始定义宏：PLOT*/
proc plot;
plot income*age;
run;
%mend plot; /*结束宏的定义*/
data newdata; /*创建数据集*/
  input  name $ age income;
cards;
xie 26 3200
wang 23 4400
wu 33 3872
zhou 19 5400
meng 22 5580
; 
run;
%plot/*调用宏，绘制收入关于年龄的散点图*/
proc print;run;
data newdata2; /*筛选年龄大于20的记录，建立新数据集*/
set newdata;
if age&gt;20;Run;
%plot/*再次调用宏，绘制收入关于年龄的散点图*/
proc print;
run;
</code></pre>
<pre><code>/*2.3 宏函数*/
/*2.4 宏表达式*/
/*2.5 宏语句*/
/*2.6 案例*/

/* The Following macro function can find the first position of "exception" in "source" string  */
%macro indexc(source, exception,delimiter=%str( ));
/* the leading and tailling blanks will be delete by way of the following statement */
%let exception =%trim(%left(&amp;exception));
%local count word result temp;
%let count=1;
%let temp=0;
%let result=0;
%let word=%qscan(&amp;exception,&amp;count,&amp;delimiter);

%do %while (&amp;word ne);
	%if (%index(&amp;source,&amp;word) ne 0) %then %do;
	%let temp=%index(&amp;source,&amp;word);
	%if (&amp;temp &lt; &amp;result) or (&amp;result = 0) %then %let result=&amp;temp;%end;
	%let count=%eval(&amp;count+1);
	%let word=%qscan(&amp;exception,&amp;count,&amp;delimiter);
%end;
&amp;result
%mend indexc;

%indexc(am, I am a teacher,delimiter=%str( ));
%macro a;
%local z;
%let z=%indexc( xie18 - xie6, 1 2 3 4 5 6 7 8 9 0,delimiter=%str( ));
%put &amp;z;
%mend a;
%a;
</code></pre>
<pre><code>/*案例二*/
/*  comma split  */
%macro split(indata,outdata);
data &amp;outdata ;
	set &amp;indata;
	length str $10;
	str=trim(left(etiology)); 
	%do i=1 %to 2;
		len=length(str); 
		if trim(left(str))='' then	len=0;
sep_xie=index(str,","); 
		if sep_xie=0  then do;
			if &amp;i=1 then do;
				etiology_&amp;i=str+0;
				str='';end;
			else etiology_&amp;i=.;
			end;
if sep_xie=1 then do;
			etiology_&amp;i=.;str=left(substr(str,sep_xie+1)); 
		end;
		if sep_xie&gt;1 then do; 
		   etiology_&amp;i=left(substr(str,1,sep_xie-1))+0;

str=left(substr(str,sep_xie+1)); 
		end;
	%end;
	rename str=etiology_code;
	drop len sep_xie;
run;
%mend split;
%split(xie.study,study);
</code></pre>
]]></description><link>https://actuarygarden.com/topic/18/sas-宏</link><generator>RSS for Node</generator><lastBuildDate>Wed, 18 Nov 2026 14:38:52 GMT</lastBuildDate><atom:link href="https://actuarygarden.com/topic/18.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 25 May 2020 01:01:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to SAS 宏 on Mon, 25 May 2020 06:17:16 GMT]]></title><description><![CDATA[<pre><code>/*2 宏Macro*/
/*2.1 宏变量*/
%let name1=name;
%let name2=‘name’;
proc print data=xie.class;
where height=67;
title “&amp;name1 ‘s height is 67.”;
title2 “&amp;name2 ‘s height is 67.”;
run;
</code></pre>
<pre><code>/*2.2  定义宏*/
/*参数宏*/
%macro try(proc,dsn);
proc &amp;proc data=&amp;dsn;
run;
%mend try;
%try(print,xie.class);
</code></pre>
<pre><code>/*无参数宏*/
%macro plot;/*开始定义宏：PLOT*/
proc plot;
plot income*age;
run;
%mend plot; /*结束宏的定义*/
data newdata; /*创建数据集*/
  input  name $ age income;
cards;
xie 26 3200
wang 23 4400
wu 33 3872
zhou 19 5400
meng 22 5580
; 
run;
%plot/*调用宏，绘制收入关于年龄的散点图*/
proc print;run;
data newdata2; /*筛选年龄大于20的记录，建立新数据集*/
set newdata;
if age&gt;20;Run;
%plot/*再次调用宏，绘制收入关于年龄的散点图*/
proc print;
run;
</code></pre>
<pre><code>/*2.3 宏函数*/
/*2.4 宏表达式*/
/*2.5 宏语句*/
/*2.6 案例*/

/* The Following macro function can find the first position of "exception" in "source" string  */
%macro indexc(source, exception,delimiter=%str( ));
/* the leading and tailling blanks will be delete by way of the following statement */
%let exception =%trim(%left(&amp;exception));
%local count word result temp;
%let count=1;
%let temp=0;
%let result=0;
%let word=%qscan(&amp;exception,&amp;count,&amp;delimiter);

%do %while (&amp;word ne);
	%if (%index(&amp;source,&amp;word) ne 0) %then %do;
	%let temp=%index(&amp;source,&amp;word);
	%if (&amp;temp &lt; &amp;result) or (&amp;result = 0) %then %let result=&amp;temp;%end;
	%let count=%eval(&amp;count+1);
	%let word=%qscan(&amp;exception,&amp;count,&amp;delimiter);
%end;
&amp;result
%mend indexc;

%indexc(am, I am a teacher,delimiter=%str( ));
%macro a;
%local z;
%let z=%indexc( xie18 - xie6, 1 2 3 4 5 6 7 8 9 0,delimiter=%str( ));
%put &amp;z;
%mend a;
%a;
</code></pre>
<pre><code>/*案例二*/
/*  comma split  */
%macro split(indata,outdata);
data &amp;outdata ;
	set &amp;indata;
	length str $10;
	str=trim(left(etiology)); 
	%do i=1 %to 2;
		len=length(str); 
		if trim(left(str))='' then	len=0;
sep_xie=index(str,","); 
		if sep_xie=0  then do;
			if &amp;i=1 then do;
				etiology_&amp;i=str+0;
				str='';end;
			else etiology_&amp;i=.;
			end;
if sep_xie=1 then do;
			etiology_&amp;i=.;str=left(substr(str,sep_xie+1)); 
		end;
		if sep_xie&gt;1 then do; 
		   etiology_&amp;i=left(substr(str,1,sep_xie-1))+0;

str=left(substr(str,sep_xie+1)); 
		end;
	%end;
	rename str=etiology_code;
	drop len sep_xie;
run;
%mend split;
%split(xie.study,study);
</code></pre>
]]></description><link>https://actuarygarden.com/post/51</link><guid isPermaLink="true">https://actuarygarden.com/post/51</guid><dc:creator><![CDATA[谢远涛]]></dc:creator><pubDate>Mon, 25 May 2020 06:17:16 GMT</pubDate></item></channel></rss>