Good morning,
This is a similar question to this one (link) but I am ideally looking for further explanation that I cannot find anywhere.
I was recently asked by a colleague why the the following inequalities return the results that they do:
%MACRO TEST ;
%IF '01JAN2009'D < '01MAR2015'D %THEN %PUT 1 is True ;
%ELSE %PUT 1 is False ;
%IF '01FEB2009'D < '01MAR2015'D %THEN %PUT 2 is True ;
%ELSE %PUT 2 is False ;
%IF '01SEP2009'D < '01MAR2015'D %THEN %PUT 3 is True ;
%ELSE %PUT 3 is False ;
%IF '01OCT2009'D < '01MAR2015'D %THEN %PUT 4 is True ;
%ELSE %PUT 4 is False ;
%MEND TEST ;
%TEST ;
Running TEST returns:
1 is True
2 is True
3 is False
4 is False
I understand that %SYSEVALF() is required to resolve these inequalities as expected. However can anyone explain to me what SAS is doing in these four instances and why two return TRUE values, whilst two return FALSE?
You can't perform date-literal evaluations directly in SAS MACRO. In your examples above, the evaluations are being performed as character strings, hence '01SEP2009'D < '01MAR2015'D being false as 01S is greater than 01M.
To compare date literals, you need to convert them to the underlying SAS date (i.e. a number).
%PUT %EVAL(%SYSEVALF('01sep2009'd) < %SYSEVALF('01mar2015'd)) ;
/* log shows 1 (true) */
It is comparing them as strings. The results are explained by the fact that J and F come before M and S and O come after it.
Related
Is it possible in SAS to invoke a call to a macro using a macro variable whose value is the macro's name? Something like the following:
%MACRO TEST (macroName, var1, var2, var3, var4, var5);
%put LOG: %¯oName(&var1);
%MEND;
%TEST(testName,testVar1);
In response to Richard's answer. I tried following your solution, but am still getting an "apparent invocation of macro YearMonthString not resolved" using the following code:
%MACRO YearMonthString(nYear,nMonth);
/* Builds a string in the format YYYYMM using passed nYear and nMonth */
%local returnVal;
/*Build the string */
%let returnVal = &nYear.%AddLeadingZerosToMakeNumXLength(2,&nMonth);
/*Write to LOG */
%put MACRO LOG: YearMonthString(nYear= &nYear, nMonth= &nMonth) will return &returnVal;
/* return returnVal */
&returnVal
%MEND;
%MACRO TEST (macroName, var1, var2, var3, var4, var5);
%local loopCount;
%let loopCount = 1;
%do i=1 %to 13;
%¯oName.(&var1,&loopCount);
%let loopCount = %eval(&loopCount+1);
%end;
%MEND;
%TEST(YearMonthString,2018,8,,,);
Yes you can! The construct is % ¯o-symbol. Here is a demonstration:
%macro one();
%put &SYSMACRONAME;
%mend;
%macro two();
%put &SYSMACRONAME;
%mend;
%macro dispatch (routine);
%&routine.()
%mend;
%dispatch (one)
%dispatch (two)
---- LOG ----
13 %dispatch (one)
ONE
14 %dispatch (two)
TWO
I want call a macro in to another macro, this creates a Macro variable which I want used in the other macro. But the output is "WARNING: Apparent symbolic reference TEST33 not resolved."
data Base1;
input v1 v2 v3;
datalines;
1 7 8
;
run;
%let number = 6;
%Macro test1;
proc sql noprint;
select
case when (
case when &number eq 3 then v1
when &number eq 6 then v2
when &number eq 12 then v3
end ) ge 6 then 1 else 0 end into: Test22 from _last_; quit;
%let Test33 = &Test22;
%Mend test1;
options mlogic mprint symbolgen;
%Macro test2;
%test1
%put &= &Test33;
%Mend;
%test2;
Your code has two mistakes, but not sure if they're genuine mistakes or typo for the demo code.
Either way:
You need to declare the variable inside the macro as GLOBAL if you want to use it outside of the macro. Macro variables have scope, ie local or global.
You reference the data set last while your demo data is called BASE1.
This works fine for me:
data Base1;
input v1 v2 v3;
datalines;
1 7 8
;
run;
%let number = 6;
%Macro test1;
%global Test33;
proc sql noprint;
select
case when (
case when &number eq 3 then v1
when &number eq 6 then v2
when &number eq 12 then v3
end ) ge 6 then 1 else 0 end into: Test22 from base1; quit;
%let Test33 = &Test22;
%Mend test1;
options mlogic mprint symbolgen;
%Macro test2;
%test1
%put &= &Test33;
%Mend;
%test2;
Im using the following loop to generate the sums of some columns using a class statement:
%macro do_mean;
%do i = 50 %to 100;
%let string1 = %eval(100-&i);
**%if string1 = 5 %then %let string1 = "05";**
%if &i = 95 or &i = 90 or &i = 80 or &i = 70 or &i = 60 or &i = 50 %then %do;
proc means data = risiko.risiko_Haus sum nway noprint;
var HA_Max_Neg HA_Max_Pers;
class C_ze_Risiko_&i._2014_&string1._2015;
output out=test_ze_Risiko_&i._2014_&string1._2015 (drop=_type_ _freq_)
sum=C_Risiko_&i._2014_05_2015_Max_Neg C_Risiko_&string1._2014_05_2015_Max_Per;
run;
%end;
%end;
%mend do_mean;
%do_mean;here
The names columns I want to use as a class are "C_ze_Risiko_50_2014_50_2015" "C_ze_Risiko_60_2014_40_2015" and so on.
Unfortunately the code produces "C_ZE_RISIKO_95_2014_5_2015" but I need "C_ZE_RISIKO_95_2014_05_2015" instead. I marked the line where I tried to change this. Unfortunately this doesnt work. Can somebody tell me why and suggest a solution?
Thanks in advance.
An alternative to Gregory's answer is to use putn and the z2. format, e.g.
%LET STRING1 = %SYSFUNC(putn(%EVAL(100-&I),z2.)) ;
What you can do is always append "0" first and then take only the last 2 characters of your string:
%let string1 = "0".%eval(100-&i);
%let string1 = %substr(&string1,%length(&string1)-1);
I'm trying to program a macro that will take in a string of variable names separated by | and perform a calculation on them (e.g. ab dc|def). I have tried the following code, but I get a strange error on the scan function: "Macro function %SCAN has too few arguments."
SYMBOLGEN tells me that &from. and &k. were resolved correctly: "FROM resolves to ab dc|def" and "K resolves to 1" so I'm not sure what the problem is. Initially I suspected that %str() masks the value until after macro execution time, resulting in the parameters not being resolved. But this doesn't seem to be the case, as %unquote(%str(..scan function..)) gives the same error.
%macro data_mapping_sum(from);
%let k=1;
%let temp_ind = "%scan(&from., &k.,"|")";
%let THIS_FAILS = %str(%'%scan(&from., &k.,"|")%'n);
%do %while( (&temp_ind. NE "") );
%unquote(&THIS_FAILS.) = 999;
%let k = %eval(&k. + 1);
%let temp_ind = "%scan(&from., &k.,"|")";
%let THIS_FAILS = %str(%'%scan(&from., &k.,"|")%'n);
%end;
%mend;
data test;
%data_mapping_sum(ab dc|def);
run;
Macro functions don't need quotes. This seems to work..as a start
%macro data_mapping_sum(from);
%let k=1;
%do %while (%scan(&from,&k,|)^=%str());
%let temp_ind = %scan(&from, &k,|);
%Put temp_ind(&k)= &temp_ind;
%let k = %eval(&k. + 1);
%end;
%mend;
%data_mapping_sum(ab dc|def);
I'll leave this question open as I don't think that I've fully answered my own question. However, if anyone needs a program with similar functionality, my working code is:
%macro data_mapping_sum(from, map_to);
%let k=1;
%let temp_ind1 = %scan(&from., &k.,"|");
%let temp_ind2 = %scan(&map_to., &k.,"|");
%do %while( ("&temp_ind1." NE "") AND ("&temp_ind2." NE "") AND &k. NE 1000);
%unquote(%str(%'&temp_ind2.%'n)) = sum(%unquote(%str(%'&temp_ind2.%'n)),%unquote(%str(%'TRAN_&temp_ind1.%'n)));
%let k = %eval(&k. + 1);
%let temp_ind1 = %scan(&from., &k.,"|");
%let temp_ind2 = %scan(&map_to., &k.,"|");
%end;
%mend;
data test;
a="%data_mapping_sum(abc d|f,ter|te cy)";
run;
I hope that it's helpful.
Here is the macro I'm running....
%macro ControlLoop(ds);
%global dset nvars nobs;
%let dset=&ds;
/* Open data set passed as the macro parameter */
%let dsid = %sysfunc(open(&dset));
/* If the data set exists, then check the number of obs ,,,then close the data set */
%if &dsid %then %do;
%If %sysfunc(attrn(&dsid,nobs))>0 %THEN %DO;;
%local dsid cols rctotal ;
%let dsid = %sysfunc(open(&DS));
%let cols=%sysfunc(attrn(&dsid, nvars));
%do %while (%sysfunc(fetch(&dsid)) = 0); /* outer loop across rows*/
/*0:Success,>0:NoSuccess,<0:RowLocked,-1:eof reach*/
%If fmt_start_dt<=&sysdate9 and fmt_end_dt>=sysdate9 %then %Do;
%do i = 1 %to &cols;
%local v t; /*To get var names and types using
varname and vartype functions in next step*/
%let v=%sysfunc(varname(&dsid,&i)); /*gets var names*/
%let t = %sysfunc(vartype(&dsid, &i)); /*gets variable type*/
%let &v = %sysfunc(getvar&t(&dsid, &i));/*To get Var values Using
GetvarC or GetvarN functions based on var data type*/
%end;
%CreateFormat(dsn=&dsn, Label=&Label, Start=&Start, fmtName=&fmtName, type=&type);
%END;
%Else %put ###*****Format Expired*****;
%END;
%END;
%else %put ###*****Data set &dset has 0 rows in it.*****;
%let rc = %sysfunc(close(&dsid));
%end;
%else %put ###*****open for data set &dset failed - %sysfunc(sysmsg()).*****;
%mend ControlLoop;
%ControlLoop(format_control);
FOrmat_Control Data:
DSN :$12. Label :$15. Start :$15. fmtName :$8. type :$3. fmt_Start_dt :mmddyy. fmt_End_dt :mmddyy.;
ssin.prd prd_nm prd_id mealnm 'n' 01/01/2013 12/31/9999
ssin.prd prd_id prd_nm mealid 'c' 01/01/2013 12/31/9999
ssin.fac fac_nm onesrc_fac_id fac1SRnm 'n' 01/01/2013 12/31/9999
ssin.fac fac_nm D3_fac_id facD3nm 'n' 01/01/2013 12/31/9999
ssin.fac onesrc_fac_id D3_fac_id facD31SR 'n' 01/01/2013 02/01/2012
oper.wrkgrp wrkgrp_nm wrkgrp_id grpnm 'n' 01/01/2013 12/31/9999
How Can i compare fmt_Start_dt and fmt_end_dt with sysdate ?
I tried something like %If fmt_start_dt<=&sysdate9 and fmt_end_dt>=sysdate9 %then %Do; in the code but values are not picking up in the loop....Any Idea???
Thanks in advance....
I'm not entirely sure what you want, but I think this might work:
%if &fmt_start_dt <= %sysfunc(today()) and &fmt_end_dt >= %sysfunc(today())
Your FETCH function will copy dataset variables to macro variables, so you need to reference them with an ampersand. Also, you should use the TODAY() function rather than the SYSDATE9 macro variable.