Hello Everyone , I have this Dataset :
User | Orders | Fam | Num_Fam |
2563 | 1542 | 66 | 1 |
2563 | 2251 | 66 | 1 |
2563 | 1544 | 66 | 1 |
2563 | 2222 | 28 | 1 |
2563 | 2335 | 28 | 1 |
And i want to get the percentage for each user , the percentage of each fam , that is , since in my case the fam 68 is repeated 3 times i want to get next to each row 33.33% , for the fam 28 since it is repeated 2 times i want to get 50% for each row , so the output would be like this :
User | Orders | Fam | Num_Fam | per |
2563 | 1542 | 66 | 1 | 33,33% |
2563 | 2251 | 66 | 1 | 33,33% |
2563 | 1544 | 66 | 1 | 33,33% |
2563 | 2222 | 28 | 1 | 50% |
2563 | 2335 | 28 | 1 | 50% |
2563 | 5455 | 20 | 1 | 100% |
Any Help Would Be Much Appreciated , Thank you .
One way
data have;
input User Orders Fam Num_Fam;
datalines;
2563 1542 66 1
2563 2251 66 1
2563 1544 66 1
2563 2222 28 1
2563 2335 28 1
;
proc sql;
create table want as
select *, 1/(count(Fam)) as per format=percent8.2
from have
group by Fam;
quit;
One way
data have;
input User Orders Fam Num_Fam;
datalines;
2563 1542 66 1
2563 2251 66 1
2563 1544 66 1
2563 2222 28 1
2563 2335 28 1
;
proc sql;
create table want as
select *, 1/(count(Fam)) as per format=percent8.2
from have
group by Fam;
quit;
Alternatively
data want;
do _N_=1 by 1 until (last.Fam);
set have;
by Fam notsorted;
end;
do until (last.Fam);
set have;
by Fam notsorted;
per=1/_N_;
output;
end;
format per percent8.2;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.