DATA Step, Macro, Functions and more

need to add new row in existing dataset

Reply
Regular Contributor
Posts: 200

need to add new row in existing dataset

[ Edited ]

Attached data. what i need is if PDCTREAS(resons for withdrawal is present then add a row in desc and desc_fmt and value for treatment as 1 . i am trying the below code but it doesnt work.

 

if PDCTREAS ne "" then do;
desc= "ByPrim";
dsreas_num_trt= 1;
dsreas_fmt = " "||PDCTREAS;
sort1 = 88;
output;
end;

Attachment
Respected Advisor
Posts: 4,545

Re: need to add new row in existing dataset

[ Edited ]

@vraj1

Without looking into the attached data set I believe what you're missing is an OUTPUT statement.

/* option 1 */
data old;
  set old;
  OUTPUT;
  if <some condition> then
    do;
      varA=1;
      ...
      OUTPUT;
    end;
run;

/* option 2: "better" */
data new_stuff;
  set old;
  if <some condition> then
    do;
      varA=1;
      ...
      OUTPUT;
    end;
run;

proc append base=old data=new_stuff;
run;
Regular Contributor
Posts: 200

Re: need to add new row in existing dataset

If i use the condition 

if PDCTREAS ne "" then do;  then it removes everything which is missing but i need all the data and add new variables.

 

I am not able to find out a way

Respected Advisor
Posts: 4,545

Re: need to add new row in existing dataset

[ Edited ]

@vraj1

That's why you need a first OUTPUT statement right after the SET statement. This will write all observations from the input data set directly to the output data set.

Then comes your condition. Whenever the condition is TRUE you're doing additional variable assignments and then you're issuing an additional OUTPUT statements which writes another row to the output table.

 

... or even better: Just collect all new rows into a table and then append this table to your original data set (that's Option 2).

Ask a Question
Discussion stats
  • 3 replies
  • 148 views
  • 0 likes
  • 2 in conversation