The intend of the post [pre] do i=1 to n by 1;
col_name=cats("col",i);
put ....; /*here,how to use the string value of "col_name" as a column name?*/
end;
[/pre]was obviously to put= the variables one by one, not to build a long string of all the names.
I reckon Scott's use of the colon
[pre] array a_cols (*) col: ; [/pre]is the most elegant way to deal transparently with the unknown number of columns.
After that, 3 ways to output the values are all good:
[pre] temp_var = vvaluex('col'||put(i,3. -l)); * we don't access the array here, but the variable directly;
temp_var1 = vvalue(a_cols(i));
put a_cols(i)=
temp_var =
temp_var1=;[/pre]
I mentioned vvaluex() as an alternative because it is very useful, and too little known. Since we have an array in this case, it is no better than referencing the array directly (it is actually worse as there is an extra step to create a temporary variable), but it can access any variable name (not arrays only), and can replace macro code in some cases.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.