I am currently learning how to use SAS and I have a question on whether it is possible to store an entry from a SAS table as a macro variable, to then do some computation before moving onto the next iteration. That is, obtain values from a table using something like DUMMY_TABLE[i,j], such as what would be possible in R or Python.
Suppose we were to have a SAS table with name, "dummy_tab", with one variable named, "NAME" and entries [a,b,c,d,e,f]. Is it then possible using a DO Loop, or some other method, to be able to iterate over the values in "dummy_tab" and store this as a macrovariable to perform some operation on it before iterating to the next entry?
To give some idea as to why I am even considering such an approach, this is my problem:
I am trying to create a macro which automatically renames variables which have special characters with an underscore. My current approach is to extract the variable names and store it in a 1-dimensional SAS table (column vector).
Then, I will iterate over the entries of this column vector and store each entry into a macro variable (mc1). Upon which, I will use PXRCHANGE to replace all the special characters with an underscore and then store this as a new macro variable (mc2.)
Finally, I will then rename the variable in the original database using the two macro variables I've defined, namely: RENAME &mc1. = &mc2. After which I will go onto the next iteration, and begin the process to rename the second variable, etc.
/* Generate dummy data */
option validvarname = any;
data dummy_data;
input "dummy?"n "te!st"n;
datalines;
1 1
2 2
3 3
;
run;
/* Obtain variable names as a column vector */
proc contents
data = dummy_data
noprint
out = col_name
(keep = name);
run;
/* Stores number of variables in dummy_data in macrovariable n_obs*/
proc sql;
select count(NAME) into : n_obs
from col_name;
quit;
/* Iterates over column names and stores i_th column name as a macrovariable */
data output_tab;
set col_name;
do i = 1 to &n_obs.;
/* Here is where the problem lies: */
%put i_col_name = col_name[i];
/* PRXCHANGE step and rename of variable to go here */
run;