BookmarkSubscribeRSS Feed
nat_tha
Calcite | Level 5

I was given a frequency dataset and I would like to expand the rows based on n to better analyze the data. The way the data is collected is by frequency of specific disaggregate as opposed to one observation at a time. How would I go about duplicate a row based on the number of frequency? Would a DO loop work in this sense? 

 

Here is an example of what is given:

Obs Sex       No/Yes   Frequency

1     Female  Yes        5

2     Male      No         3

3     Male      Yes        2

4     Female  Yes        3

 

And I would like the data to look like this based on 'frequency' and will drop the 'frequency' later

Obs      Sex       No/Yes   Frequency

1           Female  Yes        5

2           Female  Yes        5

3           Female  Yes        5

4           Female  Yes        5

5           Female  Yes        5

6           Male      No         3

7           Male      No         3

8           Male      No         3

...

 

Thanks for your help.

2 REPLIES 2
Kurt_Bremser
Super User

Yes, a DO will do this:

data want;
set have;
do i = 1 to frequency;
  output;
end;
drop i;
run;

but you basically do not need this. SAS statistical procedures allow you to use frequeny as a WEIGHT variable.

ballardw
Super User

@Kurt_Bremser wrote:

Yes, a DO will do this:

data want;
set have;
do i = 1 to frequency;
  output;
end;
drop i;
run;

but you basically do not need this. SAS statistical procedures allow you to use frequeny as a WEIGHT variable.


Or sometimes FREQ variable. Depends on how it needs to be used. A demonstration for @nat_tha of difference between Weight and Freq.

data have;
input Obs Sex $ NoYes $   Frequency;
datalines;
1     Female  Yes        5
2     Male      No         3
3     Male      Yes        2
4     Female  Yes        3
;

proc tabulate data=have;
   title 'Frequency as Weight';
   class sex;
   weight frequency;
   var obs;
   table sex, obs*(n mean std);
run; title;


proc tabulate data=have;
   title 'Frequency as Freq';
   class sex;
   freq frequency;
   var obs;
   table sex, obs*(n mean std);
run; title;

Not all procedures have the distinction between Weight and Freq, but need to be aware if the option is available.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 2 replies
  • 808 views
  • 2 likes
  • 3 in conversation