## Proc freq and Transpose

Solved
Super Contributor
Posts: 1,041

# Proc freq and Transpose

Hi Team,

After a 2 way frequency of code and type variables
TYPE can take the values of C N N/A or Q
Ex:536 and 537 codes do not have Q

code   type   count  percent
532     C      32      5.97
532     N      34      6.34
532     n/a    305     56.90
532     Q      4       0.74
534     C      1       0.18
534     N      3       0.55
534     N/A    28      5.22
534     Q       1      0.18
535     C       3      0.55
535     N       4      0.74
535     N/A     51     9.51
536     C       4      0.74
536     N       1      0.18
536     N/A    48      8.95
537     C      1       0.18
537     N      1       0.18
537     N/A    15      2.79

I want like shown below but what percentage comes under percent and how to get it there.????????????
I was told that around 20 percent comes in the first row. How can I get like that...

Also how to get the total variable.....

Thanks

C    Q    n   n/a    code     total   percent

32   4    34  303     532      373     ?????

Thanks so much

Accepted Solutions
Solution
‎10-12-2012 04:51 PM
Posts: 3,167

## Re: Proc freq and Transpose

Then I guess you probably need two passes:

data want;

set have (in=up) have;

if up then sum+total;

if ^up then do;

percent=total/sum;

output;

end;

drop sum;

run;

Haikuo

All Replies
Super User
Posts: 23,724

## Re: Proc freq and Transpose

I think you have know what percent comes under percent. Do you want it to be the total/grand total for example? Percent is such a bad label, consider using a different term if possible, ie percent of grand total, row percent...

Given the format I'd expect it to be total/grand total for a standard table, but it really depends on what you're doing. From the data it looks like if it was row percent it would be 70% so not sure where the 20% is coming from.

Super User
Posts: 23,724

## Re: Proc freq and Transpose

Couple of ways, but in your original output from proc freq you had the percent as well.

If you transpose that variable and then add it up similar to above you can merge it in.

Or you can add in the grand total and divide by that using a variety of methods.

Super Contributor
Posts: 1,041

## Re: Proc freq and Transpose

Hi,

Actually what you see above is from several datasets combined. I did it in bits because I wanted identification of each cohort. To give labels and so on in the end. So I guess Percent comin from the proc freq and transpose steps will not work out for me.

Adding the grand total????How can I do that???

Based on the codes I categorized :

734-757 form a dataset

830 and 847 form a dataset

5832-5839 form a dataset

9342  a dataset

9357-9366 a dataset

Set them all finally

Super User
Posts: 23,724

## Re: Proc freq and Transpose

This will work then....

proc sql;

create table want as

select *, totals/sum(totals)

from have;

quit;

Super Contributor
Posts: 1,041

## Re: Proc freq and Transpose

Hi

Thanks for the help.

I tried this and its not working. A percent variable is created but i get 100 for all the rows!!!1

data fin;

set cohort_final;

percent=Totals/sum(Totals)*100;

run;

Super User
Posts: 23,724

## Re: Proc freq and Transpose

That's not proc sql that's a datastep which operates differently.

In Proc SQL, the sum() function adds values over the column, in a datastep it works over a row so 100% makes sense in a datastep.

proc sql;

create table want as

select *, totals/sum(totals) as percent

from have;

quit;

Super Contributor
Posts: 1,041

## Re: Proc freq and Transpose

Got you,

Could you also help me with the data step. I mean how can i get the same thing with Data step

Thanks

Solution
‎10-12-2012 04:51 PM
Posts: 3,167

## Re: Proc freq and Transpose

Then I guess you probably need two passes:

data want;

set have (in=up) have;

if up then sum+total;

if ^up then do;

percent=total/sum;

output;

end;

drop sum;

run;

Haikuo

Super Contributor
Posts: 1,041

## Re: Proc freq and Transpose

Hi Hai,

But could you please explain to me what is two passes. and whats going on with the code you sent..

Also why are we setting the have dataset twice

Thanks a ton

Posts: 3,167

## Re: Proc freq and Transpose

"Set have (in=up) have;" is to stack two identical table one after another. It is similar to what 'proc append" does, but with lower efficiency and more control. So we can get the sum in the first 'have', and apply it on the second one, and you want to make sure only the second one get to output.

Haikuo

Super Contributor
Posts: 1,041

Great Help

Super User
Posts: 23,724

## Re: Proc freq and Transpose

The most basic and beginner method is to create the total in proc means and then merge it in. You should really use proc sql though, both of these methods are more inefficient in terms of your time.

proc means data=sashelp.class;

output out=grand_total sum(height)=grand_total;

run;

data class;

set sashelp.class;

if _n_=1 then set grand_total; *merge in total across all rows;

percent=height/grand_total;

format percent percent10.1;

run;

🔒 This topic is solved and locked.