BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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 ?
3 REPLIES 3
deleted_user
Not applicable
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.
deleted_user
Not applicable
have you tried this?

data two (drop=i);
set one;
do i = 1 to num;
output;
end;
run;
deleted_user
Not applicable
Works like a charm.
Thanks.
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
  • 3 replies
  • 23848 views
  • 4 likes
  • 1 in conversation