BookmarkSubscribeRSS Feed
alexdeguays
Calcite | Level 5

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!

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

alexdeguays
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

alexdeguays
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

 

Reeza
Super User

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

Ksharp
Super User
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))

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 1153 views
  • 2 likes
  • 4 in conversation