BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robertrao
Quartz | Level 8


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

1 ACCEPTED SOLUTION

Accepted Solutions
esjackso
Quartz | Level 8

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

7 REPLIES 7
Murray_Court
Quartz | Level 8

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 .

Jagadishkatam
Amethyst | Level 16

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
esjackso
Quartz | Level 8

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

Jagadishkatam
Amethyst | Level 16

Yes Jack, this is the right approach

Thank you

Thanks,
Jag
robertrao
Quartz | Level 8

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;

esjackso
Quartz | Level 8

That is correct. Sorry did not notice the omission.

EJ

esjackso
Quartz | Level 8

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;

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 45102 views
  • 4 likes
  • 4 in conversation