Calling unknown numbers of Macro Variables in a Data Step

Reply
Occasional Contributor
Posts: 8

Calling unknown numbers of Macro Variables in a Data Step

Hi SAS Community,

 

Please find the dummy code with my question in below:

 

%macro test;

 

%do i = 1 %to n;

    data _null_; Create macro variables name1, name2, ... name<n> from a pre-made data set according to the variable i.

    set DS1&i end = final;

    call symput('name' || strip(_n_),strip(names));
    if FINAL then call symput('total',_n_);
    run;

 

    data test&i;
    set DS2; There was already a variable "assign" with some values filled in DS2. I plan to fill out the blanks with the value of the macro &&name&m. Checking if &&name&m satisfies certain condition in ascending order, if not, check &&name%eval(&m+1) and so on, until the blank is filled then exist the do loop (with m).
    retain assign;
    do m = 1 to &total;
        if missing(assign) = 1 and _n_ = &i then do;
        assign1 = "&&name&m";
        output;
    end;
    run;

 

%end;

%mend;

 

 

The condition for &&name&m couldn't be checked outside of the data step, because the value of the DS2 is involved in the condition checking. 

 

Could anyone please help? Or if there's any other approaches that you suggest. Thank you!

Super User
Posts: 13,583

Re: Calling unknown numbers of Macro Variables in a Data Step

I think you want to expand a little bit on what this is actually supposed to be doing in terms of purpose and best with small example of dataset DS2 and as many DS1x as you think this is supposed to process.

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

I am pretty sure that you have multiple errors starting with

%do i = 1 %to n;

As n is a character value, does not represent a macro variable but is used in a macro loop. So what is n supposed to represent and where is the value supposed to come from?

Super User
Posts: 23,774

Re: Calling unknown numbers of Macro Variables in a Data Step

Do you have non macro working code? That should be your starting point

Occasional Contributor
Posts: 8

Re: Calling unknown numbers of Macro Variables in a Data Step

Hi All,

 

Thank you for your reply. n is some number that I generated before this macro definition. Sorry I didn't make my question clear.

 

Basically my question is, we cannot have macro %do loop in a data step. What is the work around if we have to call a macro names depends on the DO index within a data step?

 

Or alternatively, is there any way to set a %if(variable condition) outside a data step?

 

Thank you. Really appreciate you guys' helps.  

 

Super User
Posts: 23,774

Re: Calling unknown numbers of Macro Variables in a Data Step

we cannot have macro %do loop in a data step

 

You can, but it depends. 

Occasional Contributor
Posts: 8

Re: Calling unknown numbers of Macro Variables in a Data Step

Hi Reeza,

 

In what case we are / are not able to do so?

Super User
Super User
Posts: 8,125

Re: Calling unknown numbers of Macro Variables in a Data Step


@sas_cc wrote:

Hi All,

 

Thank you for your reply. n is some number that I generated before this macro definition. Sorry I didn't make my question clear.

 

Basically my question is, we cannot have macro %do loop in a data step. What is the work around if we have to call a macro names depends on the DO index within a data step?

 

Or alternatively, is there any way to set a %if(variable condition) outside a data step?

 

Thank you. Really appreciate you guys' helps.  

 


You can use the SYMGET() function to retrieve the value of a macro variable.

%let mvar1=one;
%let mvar2=two;
data test ;
  do i=1 to 2;
     value = symget(cats('mvar',i));
     put i= value= ;
  end;
run;
Super User
Posts: 6,785

Re: Calling unknown numbers of Macro Variables in a Data Step

I think you might be taking a difficult approach.  While I can't test this right now (and would really like to, as I suspect there are tweaks needed here and there), here's the approach I would use.  Here's what goes inside the outer %DO loop:

 

proc sql noprint;

select quote(names) into : name_list separated by ' ' from DS1&i;

quit;

 

data test&i;

array list {&sqlobs} _temporary_ $ 32 (&name_list);

set DS2;

length assign1 $ 32;

if missing(assign)=1 then do _n_=_n_ to &sqlobs until (assign1 > ' ');

   assign1 = list{_n_};

end;

run;

 

I'm not sure if you intended to create a new variable (ASSIGN1) instead of just replacing ASSIGN.  

Occasional Contributor
Posts: 8

Re: Calling unknown numbers of Macro Variables in a Data Step

Posted in reply to Astounding

Hi Astounding,

 

Thank you so much for your helps. I took a different approach based on your suggestions! 

Ask a Question
Discussion stats
  • 8 replies
  • 161 views
  • 0 likes
  • 5 in conversation