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

Hi,

I am running a do until condition and I want the output at the end of each iteration to be added to the same dataset ie.

 

%macro ..;

%do %until ();

more processing....

data x y;

if ...output x;

else output y;

end;

%mend.

 

What I am looking to do is at the end of each do until iteration, one dataset that meets the condition is generated and I want to keep adding records to it as I go through the do loop in each iteration. How is this possible?

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Each loop iteration is going to replace both data sets X and Y as currently written. You may need to decide on desired data set names for the final result. Then us Proc Append to add x and y to the final set within each loop.

Something conceptually like:

%macro ..;
   %do %until ();
   /*more processing....*/
      data x y;
         if ...output x;
         else output y;
      run;

      proc append base=finalx data=x;
      run;
      Proc append base=finaly data=y;
      run;
   %end;
%mend.

Note, your current data steps would not have generated anything as there is nothing providing values that I can see, such as a SET statement.

This assumes that the variables in the X and Y data sets will be the same name, type and length each time. Otherwise you really need to provide examples as appending random stuff runs into issues of variable type compatibility and lengths of character variables.

 

It is an extremely good idea in macro programming to explicitly end every data step or procedure properly with Run or Quit statements.

 

 

You really should provide some example data and what you expect the output to look like.

View solution in original post

2 REPLIES 2
ballardw
Super User

Each loop iteration is going to replace both data sets X and Y as currently written. You may need to decide on desired data set names for the final result. Then us Proc Append to add x and y to the final set within each loop.

Something conceptually like:

%macro ..;
   %do %until ();
   /*more processing....*/
      data x y;
         if ...output x;
         else output y;
      run;

      proc append base=finalx data=x;
      run;
      Proc append base=finaly data=y;
      run;
   %end;
%mend.

Note, your current data steps would not have generated anything as there is nothing providing values that I can see, such as a SET statement.

This assumes that the variables in the X and Y data sets will be the same name, type and length each time. Otherwise you really need to provide examples as appending random stuff runs into issues of variable type compatibility and lengths of character variables.

 

It is an extremely good idea in macro programming to explicitly end every data step or procedure properly with Run or Quit statements.

 

 

You really should provide some example data and what you expect the output to look like.

Tommer
Obsidian | Level 7
This worked!! I used proc append. I think I need to work on my macro logic better but what i was looking for from this query worked! Thank you so so much!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 335 views
  • 0 likes
  • 2 in conversation