The SAS Output Delivery System and reporting techniques

Dynamically repeat observations

Reply
N/A
Posts: 0

Dynamically repeat observations

I want to repeat an observation in a dataset based on the value in a field. I know this is usually the opposite of what most do. Let me explain: I am running a small contest and have the number of "tickets" each participant has. Therefore once I explode the dataset, I can assign a random number to each observation and select a winner.

My Data looks like this:
name tickets
aaa 1
bbb 2
ccc 1
ddd 3


What I want in the end is:
name tickets
aaa 1
bbb 2
bbb 2
ccc 1
ddd 3
ddd 3
ddd 3


Any ideas ?
N/A
Posts: 0

Re: Dynamically repeat observations

Posted in reply to deleted_user
This is where I am at so far.

data one;
input name $3. num 8.;
cards;
aaa 3
bbb 1
ccc 2
;
run;

%macro myrepeat(howmany);
%do i= 1 %to &howmany;
output;
%end;
%mend;

data two;
set one;
%myrepeat(num);
run;

But I am getting an error message:

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The
condition was: &howmany
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: The macro MYREPEAT will stop executing.
N/A
Posts: 0

Re: Dynamically repeat observations

Posted in reply to deleted_user
have you tried this?

data two (drop=i);
set one;
do i = 1 to num;
output;
end;
run;
N/A
Posts: 0

Re: Dynamically repeat observations

Posted in reply to deleted_user
Works like a charm.
Thanks.
Ask a Question
Discussion stats
  • 3 replies
  • 8132 views
  • 3 likes
  • 1 in conversation