I have defined macro variable
%let data_names = fuzzy_Data_segment EMWS2.Clus_TRAIN;
Then I have written a macro to extract and print the values from the above macro variable as:
%macro calling_data;
%do i = 1 %to 2;
%let data_name&i = %qscan(&data_names,&i);
%put &&data_name&i;
%end;
%mend;
%calling_data;
My macro code is able print the first name(fuzzy_Data_segment), but, it is only printing the part of the second name(EMWS2). what should I do to print the entire second name
Your issue is that SAS considers a period to be one of the default delimiters in macro variables. In this case, it looks like you want to be using a space to delimit items in data_names, so specify that:
%let data_name&i= %qscan(&data_names,&i., %str( ));
You're also missing semicolons in your %let statement and in your call to calling_data.
Related
so I have a code like below
%let THIS_YEAR=2020;
%macro programall;
%do i = 2016 %to &THIS_YEAR;
%let num2 =%eval(&i-2000);
%let xxx= CAT("MP",&num2);
data t_&i.;
set table1;
where GROUP in ("&xxx");
run;
%end;
for example
when i=2016
num2 = 2016-2000;
num2 = 16;
and try to concatenate with "MP", so it should create xxx=MP16.
and try to use in where statement.
but it is causing error.
how can I create Macro Variable like "MP16"correctly, so I can use it in where clause?
Thanks
You cannot use functions in macro code, they are just treated as any other text to the macro processor. But there is no need to use a function to concatenate text in macro code. Just expand the macro variable where you want to use the text it contains.
%let xxx= MP&num2 ;
Macro variables are just text (not strings, text as in the thing you type in). So to concatenate macro variables, just put them next to each other.
%let var1=Banana;
%let var2=Pepper;
%let var3=&var1. &var2.;
%put &=var3;
You don't actually have to use the third variable of course, you could just use "&var1. &var2." or whatever in your code directly.
Try
%let THIS_YEAR=2020;
%macro programall;
%local year;
%do year = 2016 %to &THIS_YEAR;
data t_&year.;
set table1;
where GROUP in ("MP%eval(&year-2000)");
run;
%end;
%mend;
options mprint;
%programall
I can't figure out this seemingly trivial problem - expect macro variable to be assigned mpg_city.
%macro test(col=);
%let id = %sysfunc(tranwrd(&col, 'extra_', ''));
%put &id;
%mend test;
%test(col=extra_mpg_city);
Current output is extra_mpg_city.
Arguments listed in a function invoked through %sysfunc are implicitly text and should not be quoted. Placing quotes in a sysfunc invoked function is like nesting quotes in a DATA step invocation.
Try
%let id = %sysfunc(tranwrd(&col, extra_, %str()));
The DATA Step analog is
id = tranwrd("&col", "extra_", "");
Your original code in DATA Step analog (below) should show why the tranwrd did not operate as you expected.
id = tranwrd("&col", "'extra_'", "''");
You don't need the quotes when using string functions with %sysfunc, unless you expect to find them in the input. Try this:
%macro test(col=);
%let id = %sysfunc(tranwrd(&col, extra_, ));
%put &id;
%mend test;
%test(col=extra_mpg_city);
I'm looking to create a loop such that I run two macros for each dataset
%Let Classification = Data1 data2 data3 data4;
%let index = 1;
%do %until (%Scan(&Classification,&index," ")=);
%Macro1;
%Macro2;
%end;
%let index = %eval(&Index + 1);
The problem is my macros are not pre-loaded and are stored in a macro library, is it possible to do this if I run the above as a macro?
Any advice is appreciate in making this loop of macros work
EDIT:
In my ideal situation the loop would run like a macro
%Macro;
where inside it would look like
%Macro Macro;
%let index = 1;
%do %until (%scan(&classification,&index," ")=);
<Lines of Code>
%end;
%let index = %eval(&Index + 1);
%end;
%mend;
Another problem is my macros enclosed in the loop use the &classification to differentiate between data1, data2, data3, data4 as we process through the different lines of code.
It is probably easier to just iterate over the index. Use the countw() function to find how many iterations to do.
%macro loop(list);
%local index next ;
%do index=1 %to %sysfunc(countw(&list,%str( )));
%let next=%scan(&list,&index,%str( ));
... code to process &NEXT ...
%end;
%mend ;
Then pass in the list to the macro as the parameter value.
%Let Classification = Data1 data2 data3 data4;
%loop(&classification);
SAS does not allow the %DO statement in open-code. When you submit an open code loop you will get log messages
ERROR: The %DO statement is not valid in open code.
...
ERROR: The %END statement is not valid in open code.
as #Tom mentioned the macro %SCAN test should check for null string. Another common and more robust way is to check before token extraction. %do %until will iterate poorly when the classification passed is empty. A %do %while tests the classification scan prior to interior macro invocations. Another common test for null macro value is checking for 0 length and leveraging 0=false ^0=true automatic evaluation.
When the loop is to call other macros with the token value the best practice is to pass the token value instead of having the called macro presume the token symbol (aka macro variable) already exists (in a containing scope) prior the iterated macros invocation.
Example
%macro mydispatch (classification=);
%local index token;
%let index = 1;
%do %while ( %length (%scan (&classification, &index)));
%let token = %scan(&classification,&index));
%* emit code specifically for token;
* this is for &token;
%* iterated invocations, perform two analysis for each data set listed in classification;
%* second analysis is passed another argument specifying the data set that should be used to store output;
%analysis_1 (data=&token)
%analysis_2 (data=&token, out=WORK.results_&token.)
%let index = %eval(&index+1);
%end;
%mend mydispatch;
%mydispatch (classification=data1 data2 data3 data4)
The macro being in an autocall library (which is what I assume you refer to?) does not have any impact on how the above would work. If it's not in an autocall library you'll have to hook up the catalog up to the autocall library first.
In re: your edits; yes, you will need this to be in a macro (I assumed it was a subset of a macro initially). %do is not currently allowed in open code (this may change, but not today).
Note you have several significant issues in your code:
the incrementor is not in the loop
the scan function is wrong; macro language does not use quotations, so
%do %until (%Scan(&Classification,&index," ")=);
needs to be
%do %until (%Scan(&Classification,&index)=);
(space is the default separator), and if you really needed to clarify space:
%do %until (%Scan(&Classification,&index,%str( ))=);
Your macros do not utilize parameters; they should. %macro1; apparently uses &classification and &index; instead you should pass it the thing you want (the "word" from &classification) as a parameter.
How do I call a macro variable in the from clause of proc sql if I wish to use it in a libname?
Let me show you what I mean:
options nofmterr;
libname FRST "/ecm/retail/mortgage/nbk6kra/LGD/data/frst_201312bkts";
libname HELN "/ecm/retail/mortgage/nbk6kra/LGD/data/heln_201312bkts";
libname HELC "/ecm/retail/mortgage/nbk6kra/LGD/data/helc_201312bkts";
%let pathLGD = /new/mortgage/2014Q4/LGD;
%let prod = FRST;
/**************** Segment calculation **************** Date filter to be consistent with model documentation for segmented tables****************/
%macro Performance(prod);
proc sql;
create table lgd_seg_&prod as
select distinct
SegDT_LGD_2013,
min(ScoreDT_LGD_2013) as min_range,
max(ScoreDT_LGD_2013) as max_range,
count(*) as count,
mean(lgd_ncl_adjusted) as LGD_actual,
mean(ScorePIT_LGD_2013) as LGD_pred_pit_1,
mean(ScoreDT_LGD_2013) as LGD_pred_dt_1
from "&prod."scored;
where coh_asof_yyyymm > 200612
group by 1;
quit;
PROC EXPORT DATA=lgd_seg_&prod._fs
OUTFILE= "&pathLGD./lgd_seg.xlsx"
DBMS=XLSX REPLACE;
SHEET="&prod._lgd_seg_fs";
RUN;
%mend;
%Performance(prod=FRST);
%Performance(prod=HELN);
%Performance(prod=HELC);
So in the "from" clause, the macro is supposed to read FRST.scored, HELN.scored, and HELC.scored respectively. Currently it cannot find the file, and if I were to remove the quotation marks, then it'd become "work.FRSTscored".
I hope I've made this clear. Any input and comment is appreciated.
When you want to follow the the resolved value of a macro variable with an immediate additional character you should escape the macro variable with a full stop (.). For example:
%let start = one;
%put &start.two;
%put &start..two;
%put &startend;
onetwo
one.two
WARNING: Apparent symbolic reference STARTEND not resolved.
So your code should read from &prod..scored;.
If you ever need to you can also delay the resolution of a macro variable with double ampersand (&&):
%let end = two;
%let onetwo = three;
%put &&one&end;
%put &&&start&end;
Three
Three
Or:
%let three = inception;
%put &&&&&&&start&end;
inception
Remove quotation marks applied outside the macro variable prod and use two dots after macro variable (one to signify end of macro variable name and second one to specify the table name after the libname reference.
from &prod..scored
I want to create a SAS macro which takes a literal date (eg. '31may2011'd) as parameter. Inside the macro I want to transform this into a SAS date value (eg. 18778).
%macro transLiteralDate2Value(literal=);
%put literal = &literal.;
%put sasdatavalue = ???; /* how to calculate this value ? */
%mend;
%transLiteralDate2Value(literal='31may2011'd);
Is the are elegant way to achieve this? Of course I could do this by parsing the literal string, but I think there must be a better way.
I use SAS 9.1.3
This will work inside or outside of a macro. Don't forget %sysfunc() has a handy optional second parameter which will let you format the output value.
%let report_date = %sysfunc(sum('01JAN2011'd),best.);
or
%let report_date = %sysfunc(putn('01JAN2011'd,best.));
Cheers
Rob
You can do it using the %sysfunc macro function.
%macro transLiteralDate2Value(literal=);
%put literal = &literal.;
%put sasdatavalue = %sysfunc(putn(&literal.,8.));
%mend;
%transLiteralDate2Value(literal='31may2011'd);
It is handy to have a pair of simple conversion macros like mine below. See also my sas-l posting.
%macro date2num(date, informat=anydtdte.);
%*-- strip quotations and postfix d from date literal if any. --*;
%*-- quotations are intentionally doubled to prevent unmatched error --*;
%let date=%sysfunc(prxchange(s/[''""]d?//i,-1,&date));
%sysfunc(inputn(&date,&informat))
%mend date2num;
%macro num2date(num, format=date10., literal=1);
%local n;
%let n = %sysfunc(putn(&num,&format));
%if &literal %then "&n"d; %else &n;
%mend num2date;