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.
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.
@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.
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!
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.