BookmarkSubscribeRSS Feed
sas715626
Calcite | Level 5

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
LinusH
Tourmaline | Level 20

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
sas715626
Calcite | Level 5

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

ballardw
Super User

This sounds more like something for Proc Report than a data set issue. Data sets do not have pages but proc report has tools to break printed output at changes is the value of a grouping variable, to the sums (and other statistics) with in the group and then provide an overal summary at the end.

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;

ChrisNZ
Tourmaline | Level 20

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;

sas-innovate-2024.png

Available on demand!

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

 

Register now!

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.

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
  • 5 replies
  • 909 views
  • 2 likes
  • 5 in conversation