BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robertrao
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

View solution in original post

12 REPLIES 12
Reeza
Super User

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.

Reeza
Super User

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.

robertrao
Quartz | Level 8

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

5877-5879 adataset

9342  a dataset

9357-9366 a dataset

Set them all finally

Reeza
Super User

This will work then....

proc sql;

create table want as

select *, totals/sum(totals)

from have;

quit;

robertrao
Quartz | Level 8

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;

Reeza
Super User

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;

robertrao
Quartz | Level 8

Got you,

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

Thanks

Haikuo
Onyx | Level 15

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

robertrao
Quartz | Level 8

Hi Hai,

I got the answer.

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

Haikuo
Onyx | Level 15


"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

robertrao
Quartz | Level 8

Great Help

Reeza
Super User

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;

sas-innovate-2024.png

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.

 

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
  • 12 replies
  • 2885 views
  • 6 likes
  • 3 in conversation