BookmarkSubscribeRSS Feed
u59166072
Calcite | Level 5

 

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

 

could you please help this .

8 REPLIES 8
PaigeMiller
Diamond | Level 26
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
u59166072
Calcite | Level 5

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

PaigeMiller
Diamond | Level 26

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
u59166072
Calcite | Level 5
ya thank you Paige.
PaigeMiller
Diamond | Level 26

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
ballardw
Super User

@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.

So how about this:

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.

PaigeMiller
Diamond | Level 26

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
Astounding
PROC Star

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 928 views
  • 3 likes
  • 4 in conversation