Arguments to macro string manipulation functions can be any text and/or macro triggers: constant text
macro variable references
macro functions
macro calls
Constant text arguments do not require quotation marks.
%let lower=lowercaseword;
%let upper=%UPCASE(lowercaseword) ;
%let upper2=%upcase(&lower);
%put lower=&lower upper=&upper upper2=&upper2;
%let str1=thisisastringwithoutspaces;
%let sub1=%SUBSTR(thisisastringwithoutspaces,5,2);
%let sub2=%SUBSTR(&str1,8,6);
%put str1=&str1 sub1=&sub1 sub2=&sub2
%put date9=&sysdate9;
%put year=%substr(&sysdate9,6);
%let str1=this is a string with spaces;
%let word1=%scan(&str1,2);
%let word2=%scan(this is a string with spaces,4);
%put str1=&str1 word1=&word1 word2=&word2;
data tmp;
do i=1 to 4;
x=i**2;
output;
end;
run;
%put syslast=&syslast;
%put dsn=%scan(&syslast,2,.);
/* a macro variable containing the names of the variables of interest*/
%let dietvars=PROTEIN FAT TOTAL_CARBOHYDRATE Alcohol CALCIUM
PHOSPHORUS IRON SODIUM POTASSIUM SATURATED_FAT
OLEIC_ACID LINOLEIC_ACID CHOLESTEROL;
%put Variables: &dietvars;
/*Find the number of variables*/
%let numvars=%sysfunc(countw(&dietvars));
%put Number of variables= &numvars;
/*Find the fifth variable*/
%let var5=%scan(&dietvars,5);
%put Fifth variable= &var5;
/*find the last variable*/
%let lastvar=%scan(&dietvars,&numvars);
%put Last variable=&lastvar;
%let str1=this is simple text;
%let index1=%INDEX(this is simple text,is);
%let index2=%INDEX(&str1,text);
%put str1=&str1 index1=&index1 index2=&index2;
%let sum=2+7;
%let sum1=%EVAL(&sum);
%let sum2=%EVAL(&sum1>7);
%let sum3=%EVAL(&sum<=7;);
%put sum=&sum sum1=&sum1 sum2=&sum2 sum3=&sum3;
%let x=%eval(2+2);
%put x=&x;
%let z=3;
%let x=%eval(&z+2);
%put x=&x;
%let x=%eval(&x+&z);
%put &x;
%let thisyr=2007;
%let lastyr=%eval(&thisyr-1);
%put thisyr=&thisyr lastyr=&lastyr;
proc means data=mac1.order_fact maxdec=2 min max mean;
class order_type;
var total_retail_price;
where year(order_date) between &lastyr and &thisyr;
title1 "Orders for &lastyr and &thisyr";
title2 "(as of &sysdate9)";
run;
%let name=DAN MCGEE;
%put name=&name;
%let name1=%sysfunc(propcase(&name));
%put name1=&name1;
%let varlist=alc bmi sbp diab chol chd;
%let numvars=%SYSFUNC(countw("&varlist"));
%put varlist=&varlist numvars=&numvars;
title1 "%sysfunc(today(),weekdate.)";
title2 "%sysfunc(time(),timeAMPM8.)";
data orders;
set orion.Orders;
where year(Order_Date)=2007;
Lag=Delivery_Date - Order_Date;
run;
proc means data=orders maxdec=2 min max mean;
class Order_Type;
var Lag;
run;
title;
%let dietvars=PROTEIN FAT TOTAL_CARBOHYDRATE Alcohol CALCIUM PHOSPHORUS IRON SODIUM
POTASSIUM SATURATED_FAT OLEIC_ACID LINOLEIC_ACID CHOLESTEROL;
%put &dietvars;
%let varlist=%sysfunc(compbl(&dietvars));
%put &varlist;
%let varlist=%sysfunc(translate(&varlist,","," "));
%put &varlist;
Create a macro variable with a single line of sas code: proc means data=orion.payroll min max;
In the following, the first semi-colon is interpreted as the end of the macro definition so is no included in the text.
%let str1=proc means data=orion.payroll min max;;
%put str1=&str1;
%let str1=proc means data=orion.payroll min max%str(;);
%let str2=%str(proc means data=orion.payroll min max;);
%put str1=&str1;
%put str2=&str2;
%let statement=%str(title "S&P 500";);
%put &statement;
%let statement=%nrstr(title "S&P 500";);
%put &statement;