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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 281 views
  • 2 likes
  • 3 in conversation