BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

One solution is to create a macro function which creates the cases:

%let Currencies=5801_EUR 5701_EUR 3009_PLN;
%macro CaseStr(Currencies);
  %local i w;
  %do i=1 %to %sysfunc(countw(&Currencies,%str( )));
    %let w=%scan(&Currencies,&i,%str( ));
    %do; when UNIT="%scan(&w,1,_)" then "%scan(&w,2,_)" %end;
    %end;
%mend;

Then you can use that in the case statement:

Case
  %CaseStr(&Currencies)
  Else CURRENCY
end

Or you can use the values directly, instead of the CURRENCIES macro variable, if you only refer to them once:

case
  %CaseStr(5801_EUR 5701_EUR 3009_PLN)
  else CURRENCY
end

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Keep your lookup values in a dataset, create a format from it, and use it in a put() function:

data lookup;
fmtname = "lookup";
type = 'C';
if _n_ = 1
then do;
  start = 'Other';
  label = '***';
  hlo = 'O';
  output;
end;
input start $ label $;
hlo = '';
output;
datalines;
5801 EUR
5701 EUR
3009 PLN
;

proc format cntlin=lookup;
run;

then use

case
  when put(unit,$lookup.) = '***'
  then currency
  else put(unit,$lookup.)
end as newvar
David_Billa
Rhodochrosite | Level 12
Thanks. Is there a way that we can tackle it using macro variables as
mentioned in the initial post?
Kurt_Bremser
Super User

@David_Billa wrote:
Thanks. Is there a way that we can tackle it using macro variables as
mentioned in the initial post?

Yes, but it's inefficient, so I won't waste time with it.

Lookup DATA belongs in DATASETS, and can easily be processed from there with static code which needs no macro processing.

s_lassen
Meteorite | Level 14

One solution is to create a macro function which creates the cases:

%let Currencies=5801_EUR 5701_EUR 3009_PLN;
%macro CaseStr(Currencies);
  %local i w;
  %do i=1 %to %sysfunc(countw(&Currencies,%str( )));
    %let w=%scan(&Currencies,&i,%str( ));
    %do; when UNIT="%scan(&w,1,_)" then "%scan(&w,2,_)" %end;
    %end;
%mend;

Then you can use that in the case statement:

Case
  %CaseStr(&Currencies)
  Else CURRENCY
end

Or you can use the values directly, instead of the CURRENCIES macro variable, if you only refer to them once:

case
  %CaseStr(5801_EUR 5701_EUR 3009_PLN)
  else CURRENCY
end

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2198 views
  • 2 likes
  • 3 in conversation