For SAS programmers, the PUT statement in the DATA step and the %PUT macro statement are useful statements that enable you to display the values of variables and macro variables, respectively. By default, the output appears in the SAS log. This article shares a few tips that help you to use these statements more effectively.
Tip 1: Display the name and value of a variable
The PUT statement supports a "named output" syntax that enables you to easily display a variable name and value. The trick is to put an equal sign immediately after the name of a variable: PUT varname=; For example, the following statement displays the text "z=" followed by the value of z:
data _null_; x = 9.1; y = 6; z = sqrt(x**2 + y**2); put z=; /* display variable and value */ run;
Tip 2: Display values of arrays
You can extend the previous tip to arrays and to sets of variables. The PUT statement enables you to display elements of an array (or multiple variables) by specifying the array name in parentheses, followed by an equal sign in parentheses, as follows:
data _null_; array x; do k = 1 to dim(x); x[k] = k**2; end; put (x[*]) (=); /* put each element of array on separate lines */ put (x1 x3 x5) (=); /* put each variable/value on separate lines */ run;
x1=1 x2=4 x3=9 x4=16 x5=25 x1=1 x3=9 x5=25
This syntax is not supported for _TEMPORARY_ arrays. However, as a workaraoun, you can use the CATQ function to concatenate array values into a character variable, as follows:
temp = catq('d', ',', of x[*]); /* x can be _TEMPORARY_ array */ put temp=;
Incidentally, if you ever want to apply a format to the values, the format name goes inside the second set of parentheses, after the equal sign: put (x1 x3 x5) (=6.2);
Tip 3: Display values on separate lines
The previous tip displayed all values on a single line. Sometimes it is useful to display each value on its own line. To do that, put a slash after the equal sign, as follows:
... put (x[*]) (=/); /* put each element on separate lines */ ...
x1=1 x2=4 x3=9 x4=16 x5=25
Tip 4: Display all name-value pairs
You can display all values of all variables by using the _ALL_ keyword, as follows:
data _null_; x = 9.1; y = 6; z = sqrt(x**2 + y**2); A = "SAS"; B = "Statistics"; put _ALL_; /* display all variables and values */ run;
x=9.1 y=6 z=10.9 A=SAS B=Statistics _ERROR_=0 _N_=1
Notice that in addition to the user-defined variables, the _ALL_ keyword also prints the values of two automatic variables named _ERROR_ and _N_.
Tip 5: Display the name and value of a macro variable
Just as the PUT statement displays the value of an ordinary variable, you can use the %PUT statement to display the value of a macro variable. If you use the special "&=" syntax, SAS will display the name and value of a macro variable. For example, to display your SAS version, you can display the value of the SYSVLONG automatic system macro variable, as follows:
The results above are for my system, which is running SAS 9.4M4. Your SAS version might be different.
Tip 6: Display all name-value pairs for macros
You can display the name and value of all user-defined macros by using the _USER_ keyword. You can display the values of all SAS automatic system macros by using the _AUTOMATIC_ keyword.
%let N = 50; %let NumSamples = 1e4; %put _USER_;
GLOBAL N 50 GLOBAL NUMSAMPLES 1e4
Conclusion and References
There you have it: six tips to make it easier to display the value of SAS variables and macro variables. Thanks to Jiangtang Hu who pointed out the %PUT &=var syntax in his blog in 2012. For additional features of the PUT and %PUT statements, see:
The post PUT it there! Six tips for using PUT and %PUT statements in SAS appeared first on The DO Loop.