DATA Step, Macro, Functions and more

Wilcard usage for character values in %LET statement

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Wilcard usage for character values in %LET statement

Hi all,

I'm a very newbie at SAS programming, so please bear with me.

I was writing a piece of code which includes a %LET statement. I would like to assign more than one value to a %let statement similar to below:

%let item=('A120XXX', 'A250XXX', 'A150XXX')

My item are way more than 3 and I would like to use a wildcard like

%let item=('A***XXX') ;

SAS do not return any results.

Is there a way to get around this?

Thanks


Accepted Solutions
Solution
‎04-10-2013 08:43 AM
Regular Contributor
Posts: 227

Re: Wilcard usage for character values in %LET statement

Posted in reply to Ron_Fehd_macro_maven

... and yes, %do loops are easier

%macro fluff2(prefix=,infix=,suffix=);

%local I J K value;

%let value =;

%do         I = 1 %to %sysfunc(countw(&prefix,%str( )));

    %do     J = 1 %to %sysfunc(countw(&infix ,%str( )));

        %do K = 1 %to %sysfunc(countw(&suffix,%str( )));

            %let value = &Value. %scan(&Prefix ,&I

                                 )%scan(&Infix ,&J

                                 )%scan(&Suffix,&K);

            %end;

        %end;

    %end;

&value

%mend;

%Let Item2 = %fluff2(prefix=A,infix=&ItemNmbrs,suffix=XXX);

%put Item2:&Item2;

View solution in original post


All Replies
Super User
Posts: 5,432

Re: Wilcard usage for character values in %LET statement

Perhaps you need to describe in more detail how you suppose to use this macro variable "item"? Also, what does your items represent?

Macro variable values are generally seen as plain text by the compiler, so if you wish to apply some logic in the assignment, you have to solve differently.

If your wild-card should represent numerical values in a range, you can perhaps use %do-loops to generate the values (but that needs to be done within a macro definition, not in open code).

The data step offers more flexibility in this area, so perhaps you can create your macro variable within a data step using call symput instead?

Data never sleeps
Regular Contributor
Posts: 227

Re: Wilcard usage for character values in %LET statement

in order to fill in the range of values which asterisks represent

you have to provide that list:

%Let ItemNmbrs = 120 150 250;

%macro fluff(prefix=,infix=,suffix=);

%local I J K dim_I dim_J dim_K value;

%let I = 1;

%let J = 1;

%let K = 1;

%let dim_I = %sysfunc(countw(&prefix,%str( )));

%let dim_J = %sysfunc(countw(&infix ,%str( )));

%let dim_K = %sysfunc(countw(&suffix,%str( )));

%let value =;

%Loop:

%let Pfx = %scan(&Prefix,&I);

%let Nfx = %scan(&Infix ,&J);

%let Sfx = %scan(&Suffix,&K);

%let value = &Value. &Pfx.&Nfx.&Sfx.;

%if     &I eq &Dim_I

    and &J eq &Dim_J

    and &K eq &Dim_K %then %do;

    &value

    %*goto %exit;

    %return;

    %end;

%if &I lt &Dim_I %then %let I = %eval(&I+1);

%if &J lt &Dim_J %then %let J = %eval(&J+1);

%if &K lt &Dim_K %then %let K = %eval(&K+1);

%goto Loop;

%exit:

%mend;

%Let Item = %fluff(prefix=A,infix=&ItemNmbrs,suffix=XXX);

%put Item:&Item;

Ron Fehd  loop maven

http://www.sascommunity.org/wiki/Do_which_loop_until_or_while

Solution
‎04-10-2013 08:43 AM
Regular Contributor
Posts: 227

Re: Wilcard usage for character values in %LET statement

Posted in reply to Ron_Fehd_macro_maven

... and yes, %do loops are easier

%macro fluff2(prefix=,infix=,suffix=);

%local I J K value;

%let value =;

%do         I = 1 %to %sysfunc(countw(&prefix,%str( )));

    %do     J = 1 %to %sysfunc(countw(&infix ,%str( )));

        %do K = 1 %to %sysfunc(countw(&suffix,%str( )));

            %let value = &Value. %scan(&Prefix ,&I

                                 )%scan(&Infix ,&J

                                 )%scan(&Suffix,&K);

            %end;

        %end;

    %end;

&value

%mend;

%Let Item2 = %fluff2(prefix=A,infix=&ItemNmbrs,suffix=XXX);

%put Item2:&Item2;

Respected Advisor
Posts: 3,799

Re: Wilcard usage for character values in %LET statement

Where to you look to find the part that is represented by ***.

   1) variable names.

   2) data values

   3) somewhere else

?

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 239 views
  • 7 likes
  • 4 in conversation