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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ron_MacroMaven
Lapis Lazuli | Level 10

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

4 REPLIES 4
LinusH
Tourmaline | Level 20

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
Ron_MacroMaven
Lapis Lazuli | Level 10

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

Ron_MacroMaven
Lapis Lazuli | Level 10

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

data_null__
Jade | Level 19

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

   1) variable names.

   2) data values

   3) somewhere else

?

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
  • 4 replies
  • 1077 views
  • 7 likes
  • 4 in conversation