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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

5 REPLIES 5
Astounding
PROC Star

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.

PhilfromGermany
Fluorite | Level 6

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!

jakarman
Barite | Level 11

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 --<-----
PhilfromGermany
Fluorite | Level 6

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

jakarman
Barite | Level 11

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

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
  • 1338 views
  • 0 likes
  • 3 in conversation