DATA Step, Macro, Functions and more

Single quotation in macro variable value causing problem

Reply
Regular Contributor
Posts: 157

Single quotation in macro variable value causing problem

I want to create value as shown below

Jan'16

Feb'16

 

The code i created

 

%let month=%sysfunc(month(%sysfunc(today())));
%put &month;
%let year=%sysfunc(year(%sysfunc(strip(%sysfunc(today())))));
%put &year;

 

proc format ;
value month 1='Jan'
2='Feb'
3='Mar'
4='Apr'
5='May'
6='Jun'
7='Jul'
8='Aug'
9='Sep'
10='Oct'
11='Nov'
12='Dec'

;
run;

 

Data _null_;
Year=put(&year,4.);
yy=Substr(Year,3,2);
mth=strip(put(&month.,month.));
mth_yy=strip(catx("'",mth,yy));
call symput('Mthyy',mth_yy);
run;

%put &Mthyy;

 

log

Aug'16;GOPTIONS NOACCESSIBLE;%LET _CLIENTTASKLABEL=;%LET _CLIENTPROJECTPATH=;%LET _CLIENTPROJECTNAME=;%LET _SASPROGRAMFILE=;;*'

 

It's showing the value but when i am trying to use the value to access the folder it's saying the folder length is 262 charachters long or some thing

 

When i tried removing the single quotation in the macro variable it's working fine.

 

Can any one look into this asap

Super User
Posts: 19,773

Re: Single quotation in macro variable value causing problem

Please clarify and show how your using it and how it doesn't work there. 

You can use formats such as MONNAME3. And year2 to get the components. 

 

It sounds like you have folders that have a single quote in the file path?

Untested:

 

data _null_;

date=today();

call symputx('Mmthyy', catx("'", put(date, monname3.), put(date, year2.)));

run;

 

%put &mmthyy;

 


JasonNC wrote:

I want to create value as shown below

Jan'16

Feb'16

 

The code i created

 

%let month=%sysfunc(month(%sysfunc(today())));
%put &month;
%let year=%sysfunc(year(%sysfunc(strip(%sysfunc(today())))));
%put &year;

 

proc format ;
value month 1='Jan'
2='Feb'
3='Mar'
4='Apr'
5='May'
6='Jun'
7='Jul'
8='Aug'
9='Sep'
10='Oct'
11='Nov'
12='Dec'

;
run;

 

Data _null_;
Year=put(&year,4.);
yy=Substr(Year,3,2);
mth=strip(put(&month.,month.));
mth_yy=strip(catx("'",mth,yy));
call symput('Mthyy',mth_yy);
run;

%put &Mthyy;

 

log

Aug'16;GOPTIONS NOACCESSIBLE;%LET _CLIENTTASKLABEL=;%LET _CLIENTPROJECTPATH=;%LET _CLIENTPROJECTNAME=;%LET _SASPROGRAMFILE=;;*'

 

It's showing the value but when i am trying to use the value to access the folder it's saying the folder length is 262 charachters long or some thing

 

When i tried removing the single quotation in the macro variable it's working fine.

 

Can any one look into this asap


 

Super User
Posts: 7,768

Re: Single quotation in macro variable value causing problem

If you actually have folders or filenames containing single quotes, use a LART on the one who created them in the first way.

Blanks, quotes and the like have no place in file or directory names, period.

 

Your problem comes from the fact that wherever you use your macro variable, a single quote is inserted into the program text (macro is just a text generator!), and what happens thereafter is the same as when you had entered that single quote manually. This also explains the funny layout of the log, as everything that EG does automatically is masked until the "magic line" with ;*';*";*/;quit;run; appears that finally terminates your virtual string; without the magic line your Workspace Server session would become unusable until you sent it a terminating single quote.

Your string does work if you enlcose it in double quotes:

proc format ;
value _month
  1='Jan'
  2='Feb'
  3='Mar'
  4='Apr'
  5='May'
  6='Jun'
  7='Jul'
  8='Aug'
  9='Sep'
  10='Oct'
  11='Nov'
  12='Dec'
;
run;

data _null_;
year = year(today());
month = month(today());
string = put(month,_month.) !! "'" !! substr(put(year,4.),3,2);
put string=;
call symput('Mthyy',trim(string));
run;

%put "&Mthyy";

I had to use _month for the format name, as SAS already has a format with the name month.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,799

Re: Single quotation in macro variable value causing problem

[ Edited ]

Try a PICTURE format, then you don't have to mess with all those functions and concatenation.

proc format;
   picture mmmxyy other='%3B''%y' (datatype=date);
   run;
   
data _null_;
   x = today();
   put x=mmmxyy.;
   run;
x=Aug'16

 

 

 

 

Super User
Super User
Posts: 7,039

Re: Single quotation in macro variable value causing problem

Looks like you got the value into the macro variable.  Your problem is not taking care for how you use it becuase of the single quote.

 

When you use it in the actual filename it should not be an issue.  A statement like 

filename in "/mypath/Data for &mthyr.csv";

would work find since the unbalanced single quote will be inside of balanced double quotes.

 

If you want to view the value using %PUT you will need to either also add quotes.

%put "&mthyr";

Or add macro quoting.

%put %superq(mthyr);
%put %bquote(&mthyr);
%let mthyr=%superq(mthyr);
%put &mthyr;

 

Ask a Question
Discussion stats
  • 4 replies
  • 267 views
  • 2 likes
  • 5 in conversation