I have the below code which gets the first day of the month in YYYYMMDD format. But I need to adjust to End of Month.
%let CLNA_EOM = %sysfunc(intnx(month,&sysfunc(date()),-1),yymmddn8.);
I tried adding ,'E' after the -1 but that doesn't work.
%let CLNA_EOM =%sysfunc(intnx(month,&sysfunc(date()),-1,'E'),yymmddn8.);
Thoughts?
To the macro processor everything is a string, so there is no need to add quotes around literal strings.
The INTNX() function does not understand the value 'E' that you gave it. Use either E or END. Just like you used MONTH instead of 'MONTH' when telling INTNX() what type of interval to use.
%let CLNA_EOM =%sysfunc(intnx(month,&sysfunc(date()),-1,E),yymmddn8.)
Related
Let the sample macro variable be
%let temp="A","B","C";
How do you get the array length of this macro variable, which includes quotations and commas? For example, I would want to return length 3.
%let length_of_temp=%sysfunc(SOME_FUNC(&temp.));
%put length=length_of_temp;
LOG: length=3
Preferably I would want to do it using one established SAS function or line of code, not creating a new function for processing. Here is what I have attempted so far:
countw("&temp.",","): the quotes create an error when trying to convert it to a string.
NOTE: Line generated by the macro variable "TEMP". 4
""A","B","C"
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS
release. Inserting white space between a quoted string and the succeeding
identifier is recommended.
ERROR 388-185: Expecting an arithmetic operator.
countw(&temp.,",") and count(&temp.): typical error of function call has too many arguments
count((&temp.)) and dim((&temp.))
variations using %superq on the above
Use macro quoting on your macro variable value so that commas do not cause trouble for call to countw() function. Use the q and possibly the m optional third argument to the countw() function to let it know not to count delimiters that are inside the quotes.
%let temp="A1,a2","B","C";
%let count = %sysfunc(countw(%superq(temp),%str(,),mq));
If you want to calculate the count in a data step then instead of macro quoting you can use the symget() function to retrieve the value of the macro variable.
count = countw(symget('temp'),',','mq');
THis works for me:
%let temp="A","B","C";
%let count = %sysfunc(countw("&temp", ","));
%put Number of elements = &count.;
Results:
8002 %put Number of elements = &count.;
Number of elements = 3
The %quote function might be helpful here to mask the quotation marks:
%let count = %sysfunc(countw(%quote(&temp), ","));
%put Number of elements = &count.;
I have a simple macro where I am passing in a parameter but also want to append onto the macro. However, when I try to add the additional text it won't recognize the original macro variable. I have tried to convert the macro variable to a string first, append the extra text, then unquote it but can't find an appropriate concatenate function to use.
Here is my macro and what isn't working now, with the problem being &del_30 in the third line. The compiler is trying to interpret &del_30 as a macro, instead of &del_ by itself.
%macro plot_better_same_worse(title_, del_);
proc Sgplot data=ALL_TP_NORM_TBL;
SERIES X = asofdt Y = &del_30 /
MARKERS LINEATTRS = (THICKNESS = 2);
run;
%mend plot_better_same_worse;
I have also tried to do this instead: &&del_&30 but SAS tries to interpret &30 as a macro variable as well.
Macro variable names begin with & and end with ., or the first character illegal to be in a macro variable name (A-Z, 0-9, _).
So &del_.30 would resolve &del_ and then put 30 after it.
I want to replace one combination of text with another. For example
data test;
a='raja\ram{work}italic';
if index(a,'\') then b=tranwrd(a,'\','\\');
if index(a,'{') then b=tranwrd(a,'{','\{');
if index(a,'}') then b=tranwrd(a,'}','\}');
if index(upcase(a),'ITALIC') then b=tranwrd(a,substr(a,index(upcase(a),'ITALIC'),length('ITALIC')),'\i');
run;
Required Result: b=raja\\ram\{work\}\i;
These kind of combination I wanted to replace. I'm not interested to use a macro or FCMP or if else condition.
Is there any function to do all at once? I tried to use a Perl expression that also working for one at a time b= prxchange('s/\\/\\\\/', -1, a)
Your regular expression is on the right track. You have a set of characters, right, you want to always prepend a \ to? So search for (one of that set of characters), which you do with [...], and then add a \ to it, using a capturing group. That's the escape character, so you have to add two any time you want to use one (\\ escapes itself to \).
data test;
a='Hello\Goodbye{stuff}';
b= prxchange('s/([\\{}])/\\$1/',-1,a);
put b=;
run;
You should do the italic bit in a second expression (or just use tranwrd). That's a totally different replacement and while theoretically possible to put in one, would make it too messy.
This question is almost identical to the other question: Multiple search and replace within a string through regular expression in SAS
Is that a coincidence?
Here is the code that worked for the other question.
%let text = abc\pqr{work};
data _null_;
var=prxchange("s/\\/\\\\/",-1,"&text");
var=prxchange("s/\{/\\\{/",-1,var);
var=prxchange("s/\}/\\\}/",-1,var);
put var;
run;
Result: abc\\pqr\{work\};
%let text = BOLD\ITALIC\ITALICBOLD\BOLDITALIC\B\I\IB\BI;
data _null_;
var=prxchange("s/BOLD/b/",-1,"&text");
var=prxchange("s/ITALIC/i/",-1,var);
var=lowcase(var);
put var;
run;
RESULT: b\i\ib\bi\b\i\ib\bi
I have a string called weight that is 85.5
I would like to convert it into a numeric 85,5 and replace the decimal seperator with a comma using SAS.
So far I am using this (messy) two step approach
weight_num= (weight*1);
format weight_num COMMAX13.2;
How can this be achieved in a less clumpsy way??
Your sample code is the recommended method of changing a variable type.
Another way is transtrn function to replace the . with a comma. This is only a good method if you don't plan to do any calculations on the values.
data have;
set sashelp.class;
keep name weight:;
weight_char=put(weight, 8.1);
run;
data want;
set have;
weight_char=transtrn(weight_char, ".", ",");
run;
proc print data=want;
run;
If you just want to change it so that commas are used for decimal point instead of periods then why not just use a simple character substitution. Do you also want to change thousands separator from comma to period? TRANSLATE() is good for that.
weight = translate(weight,',.','.,');
If you want to convert it to a number then use the INPUT() function rather than forcing SAS to convert for you.
weight_num = input(weight,comma32.);
You can then attach whatever format you want to the new numeric variable.
I don't remember how SAS deal with these special characters. Any built-in functions?
E.g
a = New Year's Day, should I use something like index(a, 'New Year's Day') > 0?
The key to this question is the masking of the apostrophe in quotes. If you wish to look for an occurrence of a single apostrophe, you can mask it with double apostrophes:
Looking for single apostrophes
data _NULL_;
a="New Year's Day";
b=index(a,"'");
put b=;
run;
The single apostrophe is passed as a second argument to the index function, using double quotes.
Looking for double quotes
data _NULL_;
a='They said, "Happy New Year!"';
b=index(a,'"');
put b=;
run;
This time around, the double quote is set inside single quotes when passed to the index function
mjsqu and NeoMental covered the basic case well, but in the special case where you do not have the option of using " (for example, you need to prevent macro variable resolution), you can double the apostrophe:
data _null_;
a='MerryXmas&HappyNewYear''s'; *here need single quotes or a macro quoting function;
b=find(a,"'"); *here do not need to mask ampersand resolution;
run;
Of course you could also use %nrstr to avoid resolution, but there are real life cases where this is occasionally needed. This works with "" similarly (two "" become one character ").
Use "find" command like below to find out what are you looking for is there in the string or not. If the returned value is greater than > 0 then apostrophe or whatever you are looking for is there, otherwise not.
Teststring - where you want to look
Next to Teststring is "'" - In quotes what are you looking for, in
your case apostrophe
data _null_;
TestString="New year's day";
IsItThere=find(TestString,"'");
put IsItThere=;
run;
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002267763.htm