BookmarkSubscribeRSS Feed
Crubal
Quartz | Level 8

Hi,

 

I have initial code which is in a macro function where vairable 'Records' is defined before. 

 

%if &Records > 5 %then %do;
data Work.Table_1(replace=yes);
set Work.Table_2;
run;
%end;

 

Table_1 already exists. And if 'Records > 5' then replace it with Table_2; and Table_1 remains if 'Records <= 5'. 

 

My question is: I am not using macro function, but general procedure (data step). How could I write the code? 

Thank you! 

9 REPLIES 9
mkeintz
PROC Star

First the macro %IF can only be utilized inside a defined macro, which you have not shown.

 

What are you trying to do?  Are you creating table2 only when some variable has at least one obs in table1 with a value > 5?

 

 

--------------------------
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

--------------------------
Crubal
Quartz | Level 8

Hi @mkeintz

 

Thanks! 

 

It is within a macro function that I omitted the part. 

 

%macro step_1;

 

...

 

%if &Records > 5 %then %do;
data Work.Table_1(replace=yes);
set Work.Table_2;
run;
%end;

 

 

%mend;

 

%step_1;

 

I would like to replace Table_1 if 'Recodrs>5' in Table_2, where 'Records' is a variable defiend before; and not replace Table_1 if this condition not satisfied. 

 

Thanks! 

ShiroAmada
Lapis Lazuli | Level 10

Try this...

%macro ReplaceWhenTrue;
%if %eval(&Records>5) %then %do;
  data table1;
     set table2;
  run;
%end;
%mend;

%ReplaceWhenTrue;

 

Hope this helps.

Crubal
Quartz | Level 8

Thanks @ShiroAmada

 

This works. Can I write the code which is the same as you did without using a macro function?

 

Thank you!

 

 

error_prone
Barite | Level 11

@Crubal wrote:

Thanks @ShiroAmada

 

This works. Can I write the code which is the same as you did without using a macro function?

 

Thank you!

  


During compilation each set statement fills the pdv with the variables of the datasets listed in the statement. I don't know any way to circumvent this. If both datasets have the same structure you can use 

data table1;   
   if _nobs_ > 5 then 
      set table2 nobs=_nobs_;
   else 
      set table1;
run;

This step always rewrites table1 ... not the best idea.

 

Crubal
Quartz | Level 8

Hi @error_prone

 

Thanks! I like the idea. As you mentioned they have the same data structure. 

And I changed the code a little bit, because 'table1' may not exist for the first time, but it will exist after running this part:

 

data table1;   
   if _nobs_ > 5 then 
      set table2 nobs=_nobs_;
run;

 

In this way, 'table1' does not exist initially, so it is same as 'table2'. And after updating, 'table1' will become 'table2' if _nobs_ > 5.

Is that right? 

 

Thank you! 

error_prone
Barite | Level 11

@Crubal wrote:

 

In this way, 'table1' does not exist initially, so it is same as 'table2'. And after updating, 'table1' will become 'table2' if _nobs_ > 5.

Is that right? 

 

Thank you! 


 

You could just test your code and see what's happening 😉

I would not recommend removing the else-statement. 

In a production process, i would use either use the solution posted by @Tom or keep the macro.

Crubal
Quartz | Level 8

Got it, thanks! 

Tom
Super User Tom
Super User

A simple way to conditionally generate code when not using a macro is to use CALL EXECUTE.  So assuming that you still have the macro variable RECORDS defined as before then you could use this data step to conditionally generate the data step that replaces TABLE_1.

data _null_;
  if  &Records > 5 then do;
    call execute(
'data Work.Table_1(replace=yes);
 set Work.Table_2;
 run;
'
    );
  end;
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
  • 9 replies
  • 1347 views
  • 5 likes
  • 5 in conversation