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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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


 

View solution in original post

1 REPLY 1
Reeza
Super User

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


 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 2049 views
  • 1 like
  • 2 in conversation