DATA Step, Macro, Functions and more

Using i counter of loop in keep statement

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

Using i counter of loop in keep statement

Hello, I would like just as many array variables in the ouput dataset as there are loops. If the loop only runs three times and creates x1, x2, and x3, I would like to just keep those three variables and not all five. Using a macro that is being created within the same data step does not work and I'm pretty sure I know why. Any suggestions? Thanks! data dummy;   checker=3;   array x[5] $100.;   do i = 1 to 10 while (checker > 0);     x = 1 + 2;     checker = checker - 1;   end;   call symputx("i",i);   keep x1 - x&i; run; Not sure if this code actually runs. I just recreated something similar to my problem. Regards Phil


Accepted Solutions
Solution
‎08-06-2014 02:28 PM
Super User
Posts: 5,082

Re: Using i counter of loop in keep statement

Phil,

This can't be done in a single step.  The DATA step must be 100% compiled, including resolution of all macro variables referenced, before it executes.

It is easily possible to do this in two steps.  You could easily add a second DATA step:

data dummy;

   set dummy (keep=x1-x&i);

run;

That means reading/writing the observations again of course.

Good luck.

View solution in original post


All Replies
Solution
‎08-06-2014 02:28 PM
Super User
Posts: 5,082

Re: Using i counter of loop in keep statement

Phil,

This can't be done in a single step.  The DATA step must be 100% compiled, including resolution of all macro variables referenced, before it executes.

It is easily possible to do this in two steps.  You could easily add a second DATA step:

data dummy;

   set dummy (keep=x1-x&i);

run;

That means reading/writing the observations again of course.

Good luck.

Contributor
Posts: 39

Re: Using i counter of loop in keep statement

Thank you for your response, Astounding. My goal was to do it all in one data step, but after all this is an easy solution and it works. Thanks!

Valued Guide
Posts: 3,208

Re: Using i counter of loop in keep statement

Instead of a second datastep why not place the keep data set options in the first one: data dummy (keep=x1-x&i .....) ;

---->-- ja karman --<-----
Contributor
Posts: 39

Re: Using i counter of loop in keep statement

Wouldn't that cause the same problem, since the data step needs to compile completely before the macro can be referenced?

Valued Guide
Posts: 3,208

Re: Using i counter of loop in keep statement

You are right, did not notice the logic in your example.

Often I see this with a the datastep being included knowing the counter already. I did my  conclusion too fast. 

I did not get your logic as you did not mentioning that with your question. It seem not to be on dataprocessing it is possible matrices.

Often that is more easier to solve in an other way. We are all often mindblocked by some previous successes   

---->-- ja karman --<-----
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 267 views
  • 0 likes
  • 3 in conversation