How to Use PUT and %PUT Statements in SAS: 6 Tips

The PUT statement in SAS for programmers who have completed a SAS certification in the DATA step and the %PUT macro statements are highly useful statements, which will help to enable you to display the values of variables and macro variables, respectively.

 

How to Use PUT and %PUT Statements in SAS: 6 Tips

 

And almost by default the output will appear in the SAS logs. In this article we will share a few tips which will allow you to make use of these statements more efficiently.

Tip #1: how to display the name and value of a variable?

The PUT statement displays a “name output” syntax that allows one to easily display a variable name and value. The trick is to input an ‘=’ (equal) sign as soon as after the name variable; like for instance: PUT varname=;

 

Here is an example in which the following statement will display 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;
z=10.9

 

Tip #2: how to display the values of arrays?

One can choose to extend the tip mentioned above to arrays and to sets of variables. The PUT statement will allow you to showcase the elements of an array or even numerous variables. All one needs to do is to specify the array name in brackets, which should be followed by an equal sign also in parentheses. The example below will show you how:

 

data _null_;
array x[5];
do k = 1 to dim(x);
   x[k] = k**2;
end;
put (x[*]) (=);     /* put each element of array */
put (x1 x3 x5) (=); /* put each variable/value   */
run;
x1=1 x2=4 x3=9 x4=16 x5=25
x1=1 x3=9 x5=25

 

However, you must note that this syntax is not supported in case of _TEMPORARY_ arrays. But a workaround could be used with the utilization of the CATQ function to concatenate array values into character variables. Here is how:

 

temp = catq('d', ',', of x[*]);         /* x can be _TEMPORARY_ array */
put temp=;

 

Also it may so happen, that you want to apply a format to the values and the format name goes within the second set of parentheses, after an equal sign: put (x1 x3 x5) (=6.2);

Tip 3#: how to display values on separate lines?

The tip before this would work to display all values on a single line. But sometimes it may be useful to display each value on its own line. In order to do so, you can put a slash after an equal sign as shown below:

 

...
put (x[*]) (=/);                   /* put each element on separate lines */
...

 

Tip #4: how to display name-value pairs?

One can choose to display all the values of all variables using the _ALL_keyword. This is how to do so:

 

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

Take a note of the fact that in addition to the user-defined variables, the _ALL_ keyword will also print the values of both the automatic variables, which are named as _ERROR_ and _N_.

Tip #5: learn to display the name and value of a macro variable:

In the same way as the PUT statement is used to display the value of an ordinary variable, even you can use the %PUT statements to showcase the values of a macro variable.

 

If one uses the special syntax of “&=”, then SAS will automatically display the name and the value of the macro variable.

For instance, in order to showcase your SAs version, one can choose to display the value of the SYSVLONG, automatic system macro variable, as shown below:

 

%put &=SYSVLONG;
SYSVLONG=9.04.01M4P110916

 

The output given above are running on a system which runs SAS 9.4M4. it may vary if your SAS version is different.

Tip #6: learning to display all the name-value pairs for macros:

One can also display the name and value of all user-defined macros with the help of using the _USER_ keyword. It is also possible to display the values of all the SAS automatic system macros with the use of the _AUTOMATIC_ keyword.

 

%let N = 50;
%let NumSamples = 1e4;
%put _USER_;
GLOBAL N 50
GLOBAL NUMSAMPLES 1e4

 

Here were the six tips to make it a tad bit easier for handling the displaying of the values of SAS variables and macro variables. For more on PUT and %PUT statements or news about SAS courses visit our leading SAS training institute DexLab Analytics. We provide the best SAS predictive modeling training in the country.

 
This post originally appeared onblogs.sas.com/content/iml/2017/01/16/put-macro-arrays.html
 

Interested in a career in Data Analyst?

To learn more about Machine Learning Using Python and Spark – click here.
To learn more about Data Analyst with Advanced excel course – click here.
To learn more about Data Analyst with SAS Course – click here.
To learn more about Data Analyst with R Course – click here.
To learn more about Big Data Course – click here.

January 19, 2017 7:11 am Published by , , , ,

, , , , ,

Comments are closed here.

...

Call us to know more

×