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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 3 replies
  • 23067 views
  • 4 likes
  • 1 in conversation