BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
carmendee
Obsidian | Level 7

Table Input:

ID Name Num_Chances

Carmen 5

Sue       2

Jack      3

John      1

 

Need 1 row for each person based on num_chances:

Carmen

Carmen

Carmen

Carmen

Carmen

Sue

Sue

Jack

Jack

Jack

John

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15
data have;
input name:$10. Num_Chances;
cards;
Carmen 5
Sue       2
Jack      3
John      1
;

data want;
set have;
do _n_=1 to num_chances;
output;
end;
keep name;
run;

View solution in original post

12 REPLIES 12
Haikuo
Onyx | Level 15
data have;
input name:$10. Num_Chances;
cards;
Carmen 5
Sue       2
Jack      3
John      1
;

data want;
set have;
do _n_=1 to num_chances;
output;
end;
keep name;
run;
carmendee
Obsidian | Level 7

Help!

 

I was getting the output I wanted after posting and now I am not (unsure what I changed).  The following results are no rows:

 

data Rounded_Chances;
input Employee_ID Suffix:$10.  Num_Chances_Rounded;

data All_Rows;
set Rounded_Chances;
do _n_=1 to Num_Chances_Rounded;
output;
end;
keep Employee_ID Suffix Num_Chances_Rounded;

SAS_inquisitive
Lapis Lazuli | Level 10
Haikuo, may be I am missing something important. When data step reads Sue, isn't _n_ becomes 2. What is logic behind "do group" starting form _n_=1? I am assuming when _n_=2 it is asking to iterate to 2 (Num_chances). So there is interplay of two kinds of _n_? when I put index "i" instead of _n_, it gives same result.
Haikuo
Onyx | Level 15

I admit this is not conventional use of '_n_' and it does inherit some risks if you are not completely sure about its inandout. The only reason I choose to do so is my laziness. The normal way to do it is to initiate a non-automatic variable as index, then drop it after the use.

Here is what going on:

1. _n_ is automatic numeric variable that will not go to the output table, it does not need to be dropped, a potential to save some typing.

2. You already knew that _n_ will +1 for every data step implicit loop. What you are not aware is that it restored its counts from last loop and  plus 1 when new loop starts. 

3. Anywhere between data step implicit loops, _n_ is just a normal numeric variable that can be used as one, meaning you can assign values and use it as a index for a inner loop.  When next round of data step implicit loop starts, _n_ will be back into playing its meant-to- be roles.

SAS_inquisitive
Lapis Lazuli | Level 10
Thanks for clarification.
carmendee
Obsidian | Level 7

I was getting the output I wanted after posting and now I am not (unsure what I changed).  The following results are no rows:

 

data Rounded_Chances;
input Employee_ID Suffix:$10.  Num_Chances_Rounded;

data All_Rows;
set Rounded_Chances;
do _n_=1 to Num_Chances_Rounded;
output;
end;
keep Employee_ID Suffix Num_Chances_Rounded;
run;    

Haikuo
Onyx | Level 15

What does your data look like?

carmendee
Obsidian | Level 7

Did not return any rows.

 

Haikuo
Onyx | Level 15

I mean your incoming data.

carmendee
Obsidian | Level 7

Example

Prefix First_Name Middle_Name Last Name Suffix Employee_ID Total_Pledge
Ms. Jack K Abbott   11 50
Ms. Tome P Abernethy   23 45
Mr. Sue L Abeyta   23 20
Ms. Erica L Abondolo   45 250
Ms. Alana C Adam   45 15

:

 

Haikuo
Onyx | Level 15

Are you looping upon the number of 'Total_Pledge'? If so, make sure it is numeric. If not, convert it before the looping:

num_pledge=input(Total_Pledge,best32.);

 

 

Then loop upon 'num_pledge'.

carmendee
Obsidian | Level 7

Thanks, everything looks good now.!

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 12 replies
  • 1910 views
  • 3 likes
  • 3 in conversation