DATA Step, Macro, Functions and more

Solved
Occasional Contributor
Posts: 6

Accepted Solutions
Solution
‎09-17-2017 03:48 PM
Super User
Posts: 10,850

```OK. How about this one .

data have;
input x \$;
n=0;
do i=length(x) to 1 by -1;
n+1;
if mod(n,2)=0 then substr(x,length(x)-n+1,1)=' ';
y=reverse(substr(x,2))||x;
end;
cards;
*
**
***
****
*****
;
run;

```

All Replies
Posts: 5,626

What is pyramid?

PG
Occasional Contributor
Posts: 6

The output must be in pyramid shape.it has to be done in core coding by using do loop.
Occasional Contributor
Posts: 6

[ Edited ]

HI PG,
> >
> > I have a below values in data set and how can i build pyramid?
> > *
> > **
> > ***
> > ****
> > *****
> >
> > i want to move first row to start at position 5 and later on decreasing
> as
> > moving to next record.
> > My output should be this way.
> >
> >        *
> >       * *
> >      * * *
> >    * * * *
> >   * * * * *
> >  * * * * * *
> >

Frequent Contributor
Posts: 118

``````data _null_;
put '   *   ';
put '  ***  ';
put ' ***** ';
put '*******';
run;

``````

Submit.  Find your pyramid erected inside the log.

Occasional Contributor
Posts: 6

i have to get the output by hard coding (by doing iterations).

Super User
Posts: 10,850

```@Rick make many different type of Christmas tree .

http://blogs.sas.com/content/iml/2013/12/18/christmas-tree-matrix.html

http://blogs.sas.com/content/iml/2014/12/10/xmas-tree-pascals-triangle.html

```
Occasional Contributor
Posts: 6

[ Edited ]

Hi ksharp,
Thanks for helping me,
pyramid must be build using normal coding not by matrix or graph plot.
> >
> > I have a below values in data set and how can i build pyramid?
> > *
> > **
> > ***
> > ****
> > *****
> >
> > i want to move first row to start at position 5 and later on decreasing
> as
> > moving to next record.
> > My output should be this way.
> >
> >      *
> >     * *
> >    * * *
> >   * * * *
> >  * * * * *
> > * * * * * *
> >

Super User
Posts: 13,941

sandeep79 wrote:

Hi ksharp,
Thanks for helping me,
pyramid must be build using normal coding not by matrix or graph plot.

How are graph plots "abnormal"?

What is your destination? A text file? HTML? RTF? PDF?

Note that the FONT used to display the output can result in an uneven appearance because of the way spaces may be displayed with proportional fonts (which is one possible reason to use graphic output)

Community Manager
Posts: 3,462

Sounds like an algorithm puzzle.  Perhaps a class assignment?

Given a list of records with asterisks left-aligned, build a pyramid (centered) version of these records?  Sounds like you need to:

1. Learn the length of the longest record (let's call it X)
2. Pad each record with spaces on either side of the asterisks -- prepend and append X/2 - length(trim(current_record)), adjusting by one when that length is an even number.

It could be a fun little programming golf puzzle...but maybe people would be more inclined to help if they knew this wasn't just your homework?  Or an interview question?  Is there a business goal?

Solution
‎09-17-2017 03:48 PM
Super User
Posts: 10,850

```OK. How about this one .

data have;
input x \$;
n=0;
do i=length(x) to 1 by -1;
n+1;
if mod(n,2)=0 then substr(x,length(x)-n+1,1)=' ';
y=reverse(substr(x,2))||x;
end;
cards;
*
**
***
****
*****
;
run;

```
☑ This topic is solved.