BookmarkSubscribeRSS Feed
sas_cc
Calcite | Level 5

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!

8 REPLIES 8
ballardw
Super User

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?

Reeza
Super User

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

sas_cc
Calcite | Level 5

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.  

 

Reeza
Super User

we cannot have macro %do loop in a data step

 

You can, but it depends. 

sas_cc
Calcite | Level 5

Hi Reeza,

 

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

Tom
Super User Tom
Super User

@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;
Astounding
PROC Star

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.  

sas_cc
Calcite | Level 5

Hi Astounding,

 

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 8 replies
  • 971 views
  • 0 likes
  • 5 in conversation