BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
nd
Obsidian | Level 7 nd
Obsidian | Level 7

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

4 REPLIES 4
ballardw
Super User

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.

FreelanceReinh
Jade | Level 19

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;
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
nd
Obsidian | Level 7 nd
Obsidian | Level 7

Yes, thank you! I wanted a 36-dimensional table to check the recode. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 491 views
  • 1 like
  • 4 in conversation