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
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 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.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.