Help using Base SAS procedures

Adding extra data to existing dataset

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

Adding extra data to existing dataset


How can i add extra data to an existing dataset???

data want;

set have(ID flag time);

ID=103; flag="Y" ;time="xxx "DT; ---------/*103 already exists in the have dataset and want to create more extra records*/

ID=105 ; flag="N" time="XX"DT; etc etc    /*I am getting only the latest record????????

run;

Thanks


Accepted Solutions
Solution
‎08-21-2013 02:10 PM
Super Contributor
Posts: 333

Re: Adding extra data to existing dataset

Just looking at the above code I believe you would need an explicit output statement after each new row you were adding to the dataset:

if end then do;

ID=105 ; flag="N" time="XX"DT;

output;

ID=103 ; flag="Y" time="XX"DT;

output;

ID=105 ; flag="Y" time="XX"DT;

output;

end;

EJ

View solution in original post


All Replies
Contributor
Posts: 45

Re: Adding extra data to existing dataset

PROC APPEND is your answer.

proc append base=have;

                   data=extra;

run;

Simply put all of your additional data into a new dataset with the same structures and field names. Use the FORCE option to add observations with new variables .

Trusted Advisor
Posts: 1,128

Re: Adding extra data to existing dataset

Hi

Please try the below code

i removed 103 record considering that the record already exists in the have data set, in order to insert the new records please include with the do and end statement as below

data want;

set have(ID flag time) end=eof;

output;

if end then do;

ID=105 ; flag="N" time="XX"DT;

/*include more records*/

output;

end;

run;

Thanks,

Jagadish

Thanks,
Jag
Solution
‎08-21-2013 02:10 PM
Super Contributor
Posts: 333

Re: Adding extra data to existing dataset

Just looking at the above code I believe you would need an explicit output statement after each new row you were adding to the dataset:

if end then do;

ID=105 ; flag="N" time="XX"DT;

output;

ID=103 ; flag="Y" time="XX"DT;

output;

ID=105 ; flag="Y" time="XX"DT;

output;

end;

EJ

Trusted Advisor
Posts: 1,128

Re: Adding extra data to existing dataset

Yes Jack, this is the right approach

Thank you

Thanks,
Jag
Super Contributor
Posts: 1,040

Re: Adding extra data to existing dataset

Hi,

Is there a semicolon after the flag variable too like shown below????in your code the semicolon is missing after the flag variable

Thanks

if end then do;

ID=105 ; flag="N"; time="XX"DT;

output;

ID=103 ; flag="Y"; time="XX"DT;

output;

ID=105 ; flag="Y"; time="XX"DT;

output;

end;

Super Contributor
Posts: 333

Re: Adding extra data to existing dataset

That is correct. Sorry did not notice the omission.

EJ

Super Contributor
Posts: 333

Re: Adding extra data to existing dataset

Alternately to proc append, there is  proc sql insert method or the tried and true data step set. If its a large dataset append and insert have some advantages not having to rewrite the old data to the dataset,. Append has simplier syntax especially for very wide datasets.

Either way you need a separate dataset with the additions. Below I use Append to add to the original dataset and a data step to show an example of creating a new dataset with all the observations.

-----------------------------

data have;

format id $3. Flag $1. time timeampm8.;

infile cards dsd ;

input id flag $ time : time8.;

cards;

103,N,12:15 AM

105,Y,1:35 PM

;

run;

data Add;

format id $3. Flag $1. time timeampm8.;

infile cards dsd ;

input id flag $ time : time8.;

cards;

103,Y,2:15 PM

105,N,1:45 AM

;

run;

data want;

  set have add;

run;

proc append base=have data=add force; run;

--------------------------

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 6638 views
  • 3 likes
  • 4 in conversation