🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 12-17-2019 02:35 AM
(2277 views)
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
4 REPLIES 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks. Is there a way that we can tackle it using macro variables as
mentioned in the initial post?
mentioned in the initial post?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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