I agree with you on this point that when writing a macro is not necessary then do not use macro, when a macro is necessary then use macro and do not use other procedures. There is another question (m105a04) in the material on the PDF's page 262, the solution of which was to produce separate tables for different levels (categories) of a variable, and a cat() statement in a sql step was used to split the table (and this serves as the first step of producing separate TFLs for different levels of a variable), the code and output are as follows (BTW: in the demo m105d04 next to the question, the code was indeed developed to a macro, which I tried several times and was not able to run through, perhaps due to the difficulty in debugging (for both the cat() statement and the case when then; statement in the subsequent data step which was intended to produce separate datasets):
%let tab=sashelp.cars;
%let col=Origin;
proc sql;
select distinct &col
from &tab;
select distinct cat('when ("', &col, '") output ', &col)
from &tab;
quit;
In the above code, the character string in the cat() statement consumes a lot of eyesight and detailed attention to type it. The more important issue is that I think this is a question which can be properly (and more appropriately) solved through a macro (there are other questions and sections for this type of macro in the material though, btw), i.e., a macro here is necessary (BTW, here as I mentioned in last paragraph, I made a mistake here - the code was indeed developed into a macro, yet which possibly may cost a lot debugging) . However, I guess the cat() statement here in the solution code for this question is to let leaners know that some steps in the macro can be replaced by the cat() statement, i.e., a possible usage of the cat() function and cat() statements in a sql step. The macro that I think can properly solve the question and the output are as follows (BTW: the code below was generated according to the solution code on page 286 of the PDF for another question, m105p01, as the reference code, which uses a proc print; step instead of proc sql; select * from; statement). A further step of consideration may be adding in the create table &xxx as to name the different datasets according to their respective categories/levels of that variable.
%macro splittable(tab,col);
proc sql;
select distinct &col
into :cat1-
from &tab;
quit;
%do i=1 %to &sqlobs;
proc sql outobs=2;
select *
from &tab
where &col="&&cat&i";
quit;
%end;
%mend splittable;
%splittable(sashelp.cars,origin);
... View more