Hello community. I'd like to cross tab all the variables that have a certain prefix (e.g. psc:) against its summary variable (e.g. pscscore) without having to write:
proc freq data = have;
table pscscore*psc1*psc2*...*psc35/ list ;
run;
Thanks.
Do you really want a 36-dimensional table (which is what the embedded "*" will do)?
If so, then I would go with the proc summary approach:
proc summary data=have nway ;
class pscscore psc: ;
output out=want (drop=_type_);;
run;
proc print data=want;
run;
You might want to skip on the proc print part.
Perhaps
proc freq data = have; table pscscore * (psc1 - psc35)/ list ; run;
The table statement will take variable lists so psc1- psc35 will attempt to use sequentially numbered variables with a PSC prefix. If there are gaps you have to indicate those. Suppose you don't actually have a Psc24 then use Psc1-Psc23 Psc25-Psc35.
The parentheses treat a list of variables as a "group". You can use the * to cross two groups. With Pscscore as a single variable this will cross it with all the variables in the list.
IF you meant that you have multiple Pscscore variables, such as 35 of them you would be entering either macro programming to create parallel values to have one pair per * OR perhaps you might be better off with reshaping the data set to a single score and variable with an indicator of the number and do a BY group with different variables.
Hello @nd,
Maybe it also helps to reduce the width of the resulting table if you concatenate PSC1, ..., PSC35 in a preliminary step using CAT or CATX (depending on the values of those variables, which I assume are numeric):
data want;
set have;
psc_all=catx(',', of psc1-psc35);
run;
proc freq data=want;
tables pscscore*psc_all / list;
run;
Do you really want a 36-dimensional table (which is what the embedded "*" will do)?
If so, then I would go with the proc summary approach:
proc summary data=have nway ;
class pscscore psc: ;
output out=want (drop=_type_);;
run;
proc print data=want;
run;
You might want to skip on the proc print part.
Yes, thank you! I wanted a 36-dimensional table to check the recode.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.