SAS experts,
Is there a way to do a simple calculation all at once using the data as following?
Say I want to get a percentage of var1-var10 over FREQ
e.g. var11=var1/freq (I want to do this calculation for all numeric variables: var1-var10)
Thanks.
YEAR FREQ VAR1 VAR2 VAR3 …. VAR10
2000 800 5 4 5 5
2001 990 6 11 3 6
2002 850 8 4 4 8
2003 1200 9 8 4 9
2004 1000 2 7 7 2
2005 998 3 6 2 3
Arrays.
Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
data want;
set have;
array _vars(*) var1-var10;
array _pct(*) pct1-pct10;
do i=1 to dim(_vars);
_pct(i) = _vars(i) / freq;
end;
format pct1-pct10 percent12.1;
run;
There are various ways to list the variables that will get included in the array - see the different methods here:
Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html
@Xiaoyi wrote:
SAS experts,
Is there a way to do a simple calculation all at once using the data as following?Say I want to get a percentage of var1-var10 over FREQ
e.g. var11=var1/freq (I want to do this calculation for all numeric variables: var1-var10)
Thanks.
YEAR FREQ VAR1 VAR2 VAR3 …. VAR10
2000 800 5 4 5 5
2001 990 6 11 3 6
2002 850 8 4 4 8
2003 1200 9 8 4 9
2004 1000 2 7 7 2
2005 998 3 6 2 3
Arrays.
Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
data want;
set have;
array _vars(*) var1-var10;
array _pct(*) pct1-pct10;
do i=1 to dim(_vars);
_pct(i) = _vars(i) / freq;
end;
format pct1-pct10 percent12.1;
run;
There are various ways to list the variables that will get included in the array - see the different methods here:
Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html
@Xiaoyi wrote:
SAS experts,
Is there a way to do a simple calculation all at once using the data as following?Say I want to get a percentage of var1-var10 over FREQ
e.g. var11=var1/freq (I want to do this calculation for all numeric variables: var1-var10)
Thanks.
YEAR FREQ VAR1 VAR2 VAR3 …. VAR10
2000 800 5 4 5 5
2001 990 6 11 3 6
2002 850 8 4 4 8
2003 1200 9 8 4 9
2004 1000 2 7 7 2
2005 998 3 6 2 3
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.