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
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
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.
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.
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
This will work then....
proc sql;
create table want as
select *, totals/sum(totals)
from have;
quit;
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;
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;
Got you,
Could you also help me with the data step. I mean how can i get the same thing with Data step
Thanks
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
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
"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
Great Help
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.