BookmarkSubscribeRSS Feed
vraj1
Quartz | Level 8

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;

5 REPLIES 5
Patrick
Opal | Level 21

@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;
vraj1
Quartz | Level 8

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

Patrick
Opal | Level 21

@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).

PrathameshP
Calcite | Level 5
proc sql;
create table WANT like HAVE;
quit;

proc sql; insert into WANT (var1, var2, var3)
values (123,'ABC','01JAN2020'D)
values (456,'EFG','20NOV2020'D);
quit;
mkeintz
PROC Star

@PrathameshP 

 

Your suggestion won't work.  The inserts need to be conditioned on some comparison.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

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

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!

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
  • 7399 views
  • 0 likes
  • 4 in conversation