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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.