Lesson 10, Part 1: Macro Variable Functions¶
Macro Character Functions that apply character string manipulations to the values of macro variables
%SUBSTR function extracts part of a string from a macro variable value based on position.¶
In [9]:
options nodate nonumber nonotes nosource;
ods html close;
*Ex5_Some_Macro_Functions.sas;
*** %SUBSTR Function;
%put &=sysdate9;
%put %upcase(sysdate9)=&sysdate9;
%put &sysdate9;
%let mvar_Year=%SUBSTR(&sysdate9,6);
%put &=mvar_Year;
%put _user_;
data _null_;
some_date = substr("&sysdate9",6);
put some_date ;
run;
data work.have;
some_year = substr("&sysdate9",6);
*another_year = substr(&sysdate9,6);
run;
proc contents data=work.have varnum;
ods select position;
run;;
The CONTENTS Procedure
| Variables in Creation Order | |||
|---|---|---|---|
| # | Variable | Type | Len |
| 1 | some_year | Char | 9 |
%LENGTH finds the lengths of character strings and text expressions.¶
In [4]:
options nodate nonumber nonotes nosource;
ods html close;
*** %LENGTH Function;
%put &sysvlong4;
%put Length_sysvlong4 = %length(&sysvlong4);
Out[4]:
The SAS System
9.04.01M5P09132017
Length_sysvlong4 = 18
E3969440A681A2408885998500000007
%SCAN function extracts nth word from the value of a macro variable.¶
In [17]:
options nodate nonumber nonotes nosource;
ods html close;
*** %SCAN function;
%put &sysuserid;
%put ID_Last_Name=%SCAN(&sysuserid, 2);
%put &SYSTCPIPHOSTNAME;
%put %SCAN(&SYSTCPIPHOSTNAME, 1, '-');
Out[17]:
The SAS System
PMuhuri
ID_Last_Name=
CCASTA-HWJP0G2
CCASTA
E3969440A681A2408885998500000020
In [3]:
options nodate nonumber nonotes nosource;
ods html close;
%LET list = Name|Age|Sex|Height|Weight;
%LET m_var = %scan(&list,-2,|);
%put Second word pulled off from the end of the string: &=m_var;
Out[3]:
The SAS System
Second word pulled off from the end of the string: M_VAR=Height
E3969440A681A2408885998500000006
%QSCAN masks the single quotation mark and the ampersand (&).¶
Ex16_scan_qscan_nrbquote.sas
In [17]:
options nodate nonumber nonotes nosource;
ods html close;
%LET list = %nrstr(McDonald%'s, AT&T);
%LET m_var = %qscan(&list,1);
%put First word pulled off: &=m_var;
Out[17]:
The SAS System
First word pulled off: M_VAR=McDonald's
E3969440A681A2408885998500000020
%UPCASE function changes macro variable values from lowercase to uppercase.¶
In [2]:
options nodate nonumber nonotes nosource;
ods html close;
**** %UPCASE Function;
%put ID_UpperCase=%UPCASE(&sysuserid);
The SAS System
ID_UpperCase=MUHURI
The SAS System
E3969440A681A2408885998500000004