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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1676 views
  • 2 likes
  • 3 in conversation