Calcite | Level 5

## base sas question

Data ds;
Infill datalines;
Input id name\$;
Cards;
1 aa
2 bb
3 cc
Run;

required

Output:
1  aa
1  aa
2  bb
2  bb
2  bb
3  cc
3  cc
3  cc
3  cc

8 REPLIES 8
Diamond | Level 26

## Re: base sas question

``````data want;
set ds;
if name='aa' then do;
output; output;
end;
else if name='bb' then do;
output; output; output;
end;
else if name='cc' then do;
output; output; output; output;
end;
run;``````

PS: in the future, and for this problem as well, it would help if you actually described the problem in words and also described in words the logic that leads to the solution desired.

--
Paige Miller
Calcite | Level 5

## Re: base sas question

Hi  @PaigeMiller , thaks for providing answer and is there any other  ways to make automate by using macro orelse program in base sas.

Diamond | Level 26

## Re: base sas question

As I said, please explain in words the problem, and please explain in words the logic you want to use to get the final answer. Do not make us guess what logic you want.

--
Paige Miller
Calcite | Level 5

## Re: base sas question

ya thank you Paige.
Diamond | Level 26

## Re: base sas question

It would be much more helpful if you explained the logic here. We could give you more useful answers then. Specifically, what is the reason aa appears twice and bb appears three times and cc appears 4 times?

--
Paige Miller
Super User

## Re: base sas question

@u59166072 wrote:

Hi  @PaigeMiller , thaks for providing answer and is there any other  ways to make automate by using macro orelse program in base sas.

SAS macros generate program code.

Programs implement rules.

Without knowing the actual rules examples are insufficient.

Consider these two other possible "solutions".

This one implements a rule of "create one more observation in the output than the numeric value of Id".

```/* one more output observation than the value of id*/
data example1;
set ds;
output;
do i= 1 to id;
output;
end;
drop i;
run;```

Looks the same. But is that actually the "rule"? Since many things that use numeric ids could have relatively large numeric values that seems unlikely to be generating 123,456,789 copies of one observation.

The rule implemented here is "use the observation (row) number in the source data set and create one more duplicate than number".

```/*based on observation number in the data set*/
data example2;
set ds;
do i=1 to _n_+1;
output;
end;
drop i ;
run;```

That seems like an unlikely need if there are very many observations. That would create a total of (n *(n+1)/2) + n observations where n is the original number of obs in the source data set

@PaigeMiller 's solution implements the rule "if the specific value of name is aa then one additional observation, if bb then two additional observations and if cc three additional observations". Nothing else. No other values of Name would result in any output observations. Is that the actual rule?

Without clear rules then a solution based on a small example may very well not even come close to providing the desired result for a larger data set.

Diamond | Level 26

## Re: base sas question

I could envision even more rules than what @ballardw explained. This is clearly not a real-world example, it is so contrived that we can't guess what the real-world usage of this code would be. So we ask questions to help us give better answers.

--
Paige Miller
PROC Star

## Re: base sas question

Since you're not sure what the problem is, here's my guess.  The intent is to output the original observation, then look at ID and output it that many additional times.  So the program could be:

``````data want;
set ds;
output;
do _n_ = 1 to id;
output;
end;
run;``````
Discussion stats
• 8 replies
• 752 views
• 3 likes
• 4 in conversation