Lesson 10, Part 3b: %SYSFUNC function executes most of the data step functions.¶
“Because %SYSFUNC is a macro function, you do not need to enclose character values in quotation marks as you do in DATA step functions” (SAS® Documentation).
In [6]:
**** Creating and displaying macro variables in open code;
options nodate nonumber nosource;
ods html close;
%let m_ds1 = CLASS|CARS|HEART|DEMOGRAPHICS;
%let m_count_words_v1 = %sysfunc(countw(&m_ds1, %str(|)) );
%put Macro variable: &=m_ds1;
%put Macro variable: &=m_count_words_v1;
%symdel m_ds1 m_count_words_v1/nowarn;
%put _user_;
Out[6]:
The SAS System
Macro variable: M_DS1=CLASS|CARS|HEART|DEMOGRAPHICS
Macro variable: M_COUNT_WORDS_V1=4
E3969440A681A2408885998500000009
In [ ]:
options nonumber nocenter nodate nosource nosymbolgen;
ods html close;
%LET list = %str(sashelp.class sashelp.iris sashelp.retail);
/* Count # of values in the string */
%LET count=%sysfunc(countw(&list, %STR( )));
%put &=list;
%put &=count;
%macro doit;
%do i = 1 %to &count;
%put title&i %left(%unquote(%SCAN(&list, &i, %STR( ))));
%end;
%put _user_;
%mend doit;
%doit
Translate() in the DATA step vs. Translate () with %SYSFUNC in the Open Code¶
In [5]:
options nodate nonumber nosource;
ods html close;
data _null_;
var1= 'STAT 4197';
var2 = translate(var1, '6', '4');
put 'DATA step variable:' var1= ;
put 'DATA step variable:' var2= ;
run;
%let m_var1 = STAT 4197;
%let m_var2 = %sysfunc(translate(&m_var1, 6, 4));
%put macro variable: &=m_var1;
%put macro variable: &=m_var2;
%symdel var1 var2 /nowarn;
Out[5]:
The SAS System
DATA step variable:var1=STAT 4197
DATA step variable:var2=STAT 6197
macro variable: M_VAR1=STAT 4197
macro variable: M_VAR2=STAT 6197
E3969440A681A2408885998500000008
%SYSFUNC and PUTN only take macro variables, not data step variables.¶
In [85]:
options nodate nonumber nosource;
ods html close;
%let x1=%sysfunc(today(),date9.);
%let x2=%sysfunc(today(),monname3.);
%let x3=%sysfunc(today(),day2.);
%let x4=%sysfunc(today(),year4.);
%let x5 =%sysfunc(putn('27jan2016'd, date9.));
%let x6 =%sysfunc(putn(0, date9.));
%let x7 =%sysfunc(inputn(04jul2016, date9.));
%let x7 =%sysfunc(putn(%sysfunc(inputn(04jul2016, date9.)), mmddyy10.));
%let x9=%sysfunc(datetime(), datetime.);
%let x10=%sysfunc(date(),worddate.);
%let x11= %sysfunc(left(%qsysfunc(date(),worddate32.)));
%put &=x1 &=x2 &=x3 &=x4 &=x5 &=x6 ;
%put &=x7 &=x8 &=x9;
%put &=x10 &=x11;
%symdel x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11/nowarn;
Out[85]:
The SAS System
X1=27OCT2019 X2=Oct X3=27 X4=2019 X5=27JAN2016 X6=01JAN1960
X7=07/04/2016 X8=27OCT19:15:43:33 X9=27OCT19:15:57:29
X10=October 27, 2019 X11=October 27, 2019
E3969440A681A2408885998500000088
In [20]:
options nonotes nodate nonumber nosource;
ods html close;
%put %sysfunc(intnx(month,%sysfunc(today()),1),monname8.);
%put %sysfunc(intnx(month,%sysfunc(today()),-1,s),year.);
%put %sysfunc(intnx(year,%sysfunc(today()),2),year.);
Out[20]:
The SAS System
November
2019
2021
E3969440A681A2408885998500000023
Challenge¶
How to replace commas in a macro variable and put spaces
"You will need to macro quote both commas and spaces to get them to be treated as text instead of delimiters." (Solution by Tom SUPER USER)
The %SYSFUNC enables the macro facility to execute SAS language functions or user-written funtions in open code (SAS Documentation).
In [24]:
options nocenter nonumber nodate nonotes nosource;
ods html close;
%let have = a,b,c;
%let want = %sysfunc(translate(%quote(&have),%str( ),%str(,)));
%put &=want;
Out[24]:
The SAS System
WANT=a b c
E3969440A681A2408885998500000027
Macro functions (%left and %qsysfunc) are resolved inside the quotes. %qsysfunc is used to handle special characters like commas safely.
The TITLE statement requires double quotes. Single quotes prevent all macro functions.
In [13]:
footnote;
title "SASHELP.CLASS - %left(%qsysfunc(date(),worddate18.))" ;
proc print data=sashelp.class (obs=5);
run;
| Obs | Name | Sex | Age | Height | Weight |
|---|---|---|---|---|---|
| 1 | Alfred | M | 14 | 69.0 | 112.5 |
| 2 | Alice | F | 13 | 56.5 | 84.0 |
| 3 | Barbara | F | 13 | 65.3 | 98.0 |
| 4 | Carol | F | 14 | 62.8 | 102.5 |
| 5 | Henry | M | 14 | 63.5 | 102.5 |
In [ ]: