BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Quartz | Level 8

Hi guys suppose to have the following table: 

 

_FREQ_ Count_1 Count_2
24213 30240 465
21292 58684 16485
11984 4168 332
24981 130459 33411

 

Is there a way to divide Count_* column by _FREQ_rowise? 

Let say: 30240/2413 then 58684/21292.... for Count_1 and 

                465/24213 then 16485/21292 .... for Count_2 and so on? 

 

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

So this is your real issue.

That's what arrays are for:

data want;
set have;
array count {28} count1-count28;
array ratio {28} ratio1-ratio28;
do i = 1 to 28;
  ratio{i} = count{i} / _freq_;
end;
drop i;
run;

But whenever I see such data structures, I suspect that a long layout would be much easier to work with from the beginning, even the PROC SUMMARY code would be simpler.

View solution in original post

5 REPLIES 5
NewUsrStat
Quartz | Level 8
Thank you for your help! I have 28 columns I would like to avoid write the ratios every time
Kurt_Bremser
Super User

So this is your real issue.

That's what arrays are for:

data want;
set have;
array count {28} count1-count28;
array ratio {28} ratio1-ratio28;
do i = 1 to 28;
  ratio{i} = count{i} / _freq_;
end;
drop i;
run;

But whenever I see such data structures, I suspect that a long layout would be much easier to work with from the beginning, even the PROC SUMMARY code would be simpler.

ballardw
Super User

I believe we have suggested providing data in the form of working data step code so we have actual data to work with and don't have to ask questions about it.

 

If I understand your question I think this is what you want:

data want;
   set have;
   rate1 = count_1 / _freq_;
   rate2 = count_2 / _freq_;
run;

If you have 'many' count or other numerator variables that you want then ARRAY processing can reduce the total amount of code:

data want;
   set have;
   array c (*) count_1 count_2; /*<= list of the variables you have*/
   array rate(2) ;  /* the number in () should match the number of "count" variables*/
   do i=1 to dim(c);
       rate[i] = c[i] / _freq_;
   end;
   drop i;
run;

Which if you had 20 count variables the only changes needed would be 1) the list (note SAS allows several list forms such as Count_1 - Count_20 if the variables are named such to simplify name lists)

and 2) the number of elements in the Rate array.

The array statement will create N variables named <name>1 to <name>N where N is the number in parentheses by default. Or you can explicitly name your variables with a list such as shown for the C array.

The function DIM is a special function that returns the number of defined elements in an array. So can be used in a loop as shown to get each index number and select the desired element from the array.

 

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

From SAS Users blog
Want more? Visit our blog for more articles like these.
5 Steps to Your First Analytics Project Using SAS

For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 230 views
  • 1 like
  • 3 in conversation