Qbik
Qbik

Reputation: 6147

How to add new observation to already created dataset in SAS?

How to add new observation to already created dataset in SAS ? For example, if I have dataset 'dataX' with variable 'x' and 'y' and I want to add new observation which is multiplication by two of the of the observation number n, how can I do it ?

dataX :
x y
1 1
1 21
2 3

I want to create :

dataX :
x y
1 1
1 21
2 3
10 210

where observation number four is multiplication by ten of observation number two.

Upvotes: 3

Views: 55517

Answers (4)

Sandy
Sandy

Reputation: 163

data X;
input x y;
datalines;
1 1
1 21
2 3 
;
run;



data X ;
set X end=eof;
if eof then do;
output;
x=10 ;y=210;
end;
output;
run;

Upvotes: 6

Bart Killam
Bart Killam

Reputation: 41

data a ;
 do kk=1 to 5 ;
    output ;
 end ;
run;

data a2 ;
    kk=999 ;
    output ;
run;

data a; set a a2 ;run ;

proc print data=a ;run ;

Result: The SAS System 1

                              OBS     kk

                               1       1
                               2       2
                               3       3
                               4       4
                               5       5
                               6     999

Upvotes: 4

BellevueBob
BellevueBob

Reputation: 9618

Here is one way to do this:

data dataX;
   input x y;
   datalines;
1 1
1 21
2 3
run;

/* Create a new observation into temp data set */
data _addRec;
  set dataX(firstobs=2); /* Get observation 2 */
   x = x * 10;  /* Multiply each by 10 */
   y = y * 10;
   output;      /* Output new observation */
   stop;
run;

/* Add new obs to original data set */
proc append base=dataX data=_addRec;
run;

/* Delete the temp data set (to be safe) */
proc delete data=_addRec;
run;

Upvotes: 4

Neetu
Neetu

Reputation: 9

You can use macro to obtain your desired result :

  1. Write a macro which will read first DataSet and when _n_=2 it will multiply x and y with 10.

  2. After that create another DataSet which will hold only your muliplied value let say x'=10x and y'=10y.

  3. Pass both DataSet in another macro which will set the original datset and newly created dataset.

Logic is you have to create another dataset with value 10x and 10y and after that set wih previous dataset.

I hope this will help !

Upvotes: 0

Related Questions