Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- Re: insert the last observation for every two or three observations in...

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 09-29-2015 12:09 PM
(907 views)

Hi, i have a data set like this

data test;

input x$ a b c;

cards;

A 1 2 3

B 4 5 6

C 7 8 9

D 1 2 6

E 3 7 9

Total 10 20 30

;

run;

I would like to create a data set like below. Which means i want to repeat the last row of my data set with every 2 records of my existing data set. Please help?

A 1 2 3

B 4 5 6

Total 10 20 30

C 7 8 9

D 1 2 6

Total 10 20 30

E 3 7 9

Total 10 20 30

5 REPLIES 5

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

By just quick looking at your sample data, it makes little sense. Perhaps you could use more realistic names/values? And tell us why you wish to do this...

Data never sleeps

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks for your response.

I have a real time scenario where if there are 100 rows and there will be a final row which is the sum of 100 rows , so total there will be 101 rows. I have to print 10 rows for each page + the sum row.The 11 th row is the sum of all 100 rows and nothing to do with the 10 rows. So it will be like 11 rows for 10 pages. Hope it is clear. Please let em know if you need more information w.r.t this

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

This should do the trick for you:

**data** test;

input x$ a b c;

cards;

A 1 2 3

B 4 5 6

C 7 8 9

D 1 2 6

E 3 7 9

Total 10 20 30

;

**run**;

**data** last;

set test end=end;

if end then output;

**run**;

**data** final(drop=lastx lasta lastb lastc);

set test end=end;

if _n_=**1** then set last(rename=(x=lastx a=lasta b=lastb c=lastc));

if mod(_n_,**2**)=**0** then do;

output;

x=lastx;

a=lasta;

b=lastb;

c=lastc;

end;

if not end then output;

**run**;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Or

```
data TEST;
input X $ A B C;
cards;
A 1 2 3
B 4 5 6
C 7 8 9
D 1 2 6
E 3 7 9
Total 10 20 30
run;
data FINAL;
set TEST nobs=NOBS ;
if _N_ ne NOBS then output;
if mod(_N_,2)=0 then do;
set TEST point=NOBS ;
output;
end;
run;
```

**Available on demand!**

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

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.