Quote a substring inside Macro

Reply
New Contributor
Posts: 3

Quote a substring inside Macro

I'm trying to quote a substring of a macro variable. The code is:

%let hola=resuelto!;

%macro prueba(param);
    %let amper=&param;
    %let amper2=%nrbquote(%substr(&param,1,3));
    %put &param;
    %put &amper;
    %put &amper2;
%mend;

%prueba(%nrstr(&hola));

In the log, I'm getting:

WARNING: Apparent symbolic reference HO not resolved.
&hola
&hola
&ho

The problem is I need to assign amper2 literally the value &ho, without any attempt of resolution, like I do with amper, that gets literally the string &hola... any idea?

Thank you in advance!

Super User
Super User
Posts: 7,720

Re: Quote a substring inside Macro

In your code you could only put single quotes around it, otherwise as soon as you try to use double quotes it will treat it as a macro variable.  However the real question here, as with any question like this, is why you are trying to use macro code (which is a text generator) to do data manipulation.  SAS Base is the programming language, it is designed to manipulate and process data, and has the various constructs and data types to handle all programming functions.  Macro is a nice to have to save typing some code, it isn't there as a replacement for SAS Base.  Here:

%let hola=resuelto!;

%macro prueba(param);
  data _null_;
    call symputx('amper2',"'"||substr("&param.",1,3)||"'");
    %put &amper2;
run; %mend; %prueba(%nrstr(&hola));

However, there is no value in the above code at all, it is merely complicated messy programming.  Why do you want to do this in the first place?  Almost any code you can think of can be done in Base SAS.

New Contributor
Posts: 3

Re: Quote a substring inside Macro

Thank you for your quick answer!

 

I'm creating a macro for dates conversion/manipulation. I haven't found a way to change a date value to another value, for example as this:

 

20160812->08/12/2016

Super User
Super User
Posts: 7,720

Re: Quote a substring inside Macro

Well, you have just given a very good example of why not to use macro language.  As I say above, macro language is a method for generating Text only, that is all it does.  It has no data constructs (other than text), nor any data processing facilities.  It is Base SAS which has all this.  For instance, in your example you have a date - which in itself is the number of days since a certain timepoint, represented for humans using a format.  In textual context the representation for humans is not a logically processable thing.  So using Base SAS the code is very simple:

data temp;
  d=input("20160812",yymmdd8.);
  format d ddmmyy8.;
run;

If you need to use that in macro code you can call symputx('d',put(d,ddmmyy8.));

However unless there is a very good reason to do so, I would avoid doing it and use Base SAS - if you see code repeating then of course macro-tizing that is useful.  This is the way I look at it Base SAS for everything, then if a certain bit of code is repeated, or could be useful elsewhere then macrotize it.

New Contributor
Posts: 3

Re: Quote a substring inside Macro

Thank you, but what if I need a 4-digit year?

Super User
Super User
Posts: 7,720

Re: Quote a substring inside Macro

Then you alter the format length (number at the end) to accomodate a four digit year:

data temp;
  d=input("20160812",yymmdd8.);
  format d ddmmyy10.;
run;

http://support.sas.com/documentation/cdl/en/etsug/60372/HTML/default/viewer.htm#etsug_intervals_sect...

 

Super User
Posts: 19,157

Re: Quote a substring inside Macro

And if your first value is already a SAS date you change the format using the Format statement. 

Super User
Posts: 9,867

Re: Quote a substring inside Macro

How about this one ?

%let hola=resuelto!;

%macro prueba(param);
    %let amper=&param;
    %let amper2=&%substr(&param,2,2);
    %put &param;
    %put &amper;
    %put %superq(amper2);
%mend;

%prueba(%nrstr(&hola))

Super User
Posts: 9,867

Re: Quote a substring inside Macro

 
Ask a Question
Discussion stats
  • 8 replies
  • 654 views
  • 2 likes
  • 4 in conversation