BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pkopersk
Fluorite | Level 6

Hello I am struggling with the function substr() and symputx().

 

* I am trying to generate this variable automatically;
%let suiviassinc=dsi_asti.suiviassincnew1807;



*this is my code;


%let annee=2018; 
%let mois=11; data _null_; call symputx('a',substr("&annee",3,4)); call symputx('suiviassinc',"dsi_asti.suiviassincnew&a&mois"); run;

 

Upon running the code I am getting the following error:

NOTE: Invalid third argument to function SUBSTR at line 397 column 22.

 

Can you please explain the issue here?

 

 

 

Thanks for your help

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

I have no clue what your real objective is i.e in other words what you want to accomplish and why the macro

 

But if you want just help and only just to make your code work, here you go

 


* I am trying to generate this variable automatically;
%let suiviassinc=dsi_asti.suiviassincnew1807;



*this is my code;


%let annee=2018; 
%let mois=11; 

data _null_;  
	
	call symputx('a',substr("&annee",3,2));

        call symputx('suiviassinc',resolve("dsi_asti.suiviassincnew&a&mois"));
run;

%put &suiviassinc;

 

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

I have no clue what your real objective is i.e in other words what you want to accomplish and why the macro

 

But if you want just help and only just to make your code work, here you go

 


* I am trying to generate this variable automatically;
%let suiviassinc=dsi_asti.suiviassincnew1807;



*this is my code;


%let annee=2018; 
%let mois=11; 

data _null_;  
	
	call symputx('a',substr("&annee",3,2));

        call symputx('suiviassinc',resolve("dsi_asti.suiviassincnew&a&mois"));
run;

%put &suiviassinc;

 

PaigeMiller
Diamond | Level 26

SUBSTR third argument should be the length of text you want to extract, which in this case seems it should be 2 and not 4.

 

In the future, please post the SASLOG when there are errors or warnings or notes that you don't understand.

--
Paige Miller
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The function substr() is of the form:

 

substr(<string>,<start position>,<number of characters to read>)

You are saying from the 3rd character, read 4 characters - which exceeds the length of the string.

 

So this:

 

call symputx('a',substr("&annee",3,4));

Should read:

call symputx('a',substr("&annee",3,2));

 To get the two characters from position 3 onwards, which is "18".

 

Also, you are overcomplicating your code, no need to create another macro variable, just do:

%let suiviassinc=dsi_asti.suiviassincnew1807;
%let annee=2018; 
%let mois=11; data _null_; call symputx('suiviassinc',cats("dsi_asti.suiviassincnew",substr("&annee.",3,4),"&mois.")); run;

 Please also, always put the decimal point after macro variables.

Astounding
PROC Star

Even once you resolve that error, there is an additional reason the code will not work.  You cannot create &A within a DATA step, and then use it within the same DATA step.  Basically, SAS has to determine what your statements are all about, before it has created &A.  You would be better off keeping all your code in macro language:

 

%let annee=2018;
%let mois=11;

%let a = %substr(&annee, 3, 2);
%let suiviassinc = dsi_asti.suiviassincnew&a&mois;

 

Even if you must use a DATA step for some other reason, you can emulate the %LET statement with:

 

data _null_;
call symputx('suiviassinc', "dsi_asti.suiviassincnew&a&mois");
run;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 805 views
  • 0 likes
  • 5 in conversation