I have a dataset which takes the following form:
ID | North | East | West | South |
4634 | 45 | 767 | 34 | 65 |
1264 | 23 | 54 | 643 | 43 |
2654 | 5 | 34 | 342 | 434 |
I want to calcualte the percentage across each row for each ID, so that I get something that looks like this:
ID | North | East | West | South | Total |
4634 | 5% | 84% | 4% | 7% | 100% |
1264 | 3% | 7% | 84% | 6% | 100% |
2654 | 1% | 4% | 42% | 53% | 100% |
Please help! Thank you!
Please try
data have;
input ID North East West South;
tot=sum(of North,East, West, South);
cards;
4634 45 767 34 65
1264 23 54 643 43
2654 5 34 342 434
;
data want;
set have;
array pct(5) North East West South tot;
array pcts(5) North_ East_ West_ South_ tot_;
do i = 1 to 5;
pcts(i)=input((pct(i)/tot),PERCENT8.);
end;
run;
Please try
data have;
input ID North East West South;
tot=sum(of North,East, West, South);
cards;
4634 45 767 34 65
1264 23 54 643 43
2654 5 34 342 434
;
data want;
set have;
array pct(5) North East West South tot;
array pcts(5) North_ East_ West_ South_ tot_;
do i = 1 to 5;
pcts(i)=input((pct(i)/tot),PERCENT8.);
end;
run;
It gives the following error:
ERROR 22-322: Syntax error, expecting one of the following: ), -.
ERROR 200-322: The symbol is not recognized and will be ignored.
Remove total out of the array and use sum(of pct(*)) instead as the total.
Your error shouldn't be caused by that though. In general when you get an error post your code and the error. We can't see your computer screen.
Hey thanks, for some reason it works now. Would you mind re-commenting with the amended code, with the suggestion you have made in the above comment....just so that I know what exactly you meant?
I'm sorry, I'm new to programing, so struggling a bit.
Thanks again!
No, @Jagadishkatam original solution was correct. I missed seeing the tot calculation when he read the file.
Your error was likely caused by a typo. If you didn't change anything and it ran I would double check my log and results.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.