<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Having trouble with macro compilation within macro call in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956896#M373595</link>
    <description>&lt;P&gt;Agree with Tom. I would focus on this macro from your example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro get_bu_date(name);
proc sql;
select substr(fname, 1, prxmatch("m/(?&amp;lt;=_)(\d{2}.*)(?=\.json)/oi",fname)-2) as rep_name,  floor(max(input(substr(fname, prxmatch("m/(?&amp;lt;=_)(\d{2}.*)(?=\.json)/oi",fname), 18), anydtdtm.))) format=best12. into :nom, :_last 
from WORK.FILENAMES
where prxmatch("m/_&amp;amp;name/oi",fname)
group by 1;
quit;

data _null_;
set WORK.FILENAMES;
where prxmatch("m/_&amp;amp;name/oi",fname);
retain valor;
valor=max(valor, floor(max(input(substr(fname, prxmatch("m/(?&amp;lt;=_)(\d{2}.*)(?=\.json)/oi",fname), 18), anydtdtm.))));
call symputx('_last', valor);
run;
%put &amp;amp;_last.;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you run that macro, I assume the %PUT &amp;amp;_last statement inside the macro works and the macro variable resolves.&lt;BR /&gt;&lt;BR /&gt;But if after running the macro, you submit&amp;nbsp; another %PUT&amp;nbsp; &amp;amp;_last statement= you probably get a message that &amp;amp;_last does not resolve.&amp;nbsp; This is because &amp;amp;_last was created as a local macro variable, and does not exist outside of the macro. To make it a global macro variable (which may not be the best approach, but it's an option), you could add a %global statement or tell CALL SYMPUTX to make it global:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call symputx('_last', valor,G);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 22 Jan 2025 18:12:18 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2025-01-22T18:12:18Z</dc:date>
    <item>
      <title>Having trouble with macro compilation within macro call</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956888#M373593</link>
      <description>&lt;P&gt;I'm going crazy. I cannot figure out how to resolve this issue.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to save a file under the condition that the latest modified date is greater than the last saved date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It works but it does not update correctly the macro vars _mod and _last.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Like a headless chicken I'm trying to solve this by brute force doing things like calling twice the macro, usage of symget, call symputx, select :into, call symdel...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Without success. At run time it doesn't get the latest updated values of _mod (last modified) and _last (last saved).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;cas mySession sessopts=(caslib=casuser timeout=1800 locale="en_US");
caslib _all_ assign;

/* --- Begin Edit --- */
%let BASE_URI=%sysfunc(getoption(servicesbaseurl));

%put &amp;amp;base_uri.;

%let baseurl=https:/xxxxxxxxx;

%let ppath=/Users/xxxxxxxx@comp.com/My Folder/in_pdf/;

filename myfldr filesrvc folderPath="&amp;amp;ppath.";

data filenames;
length  fname $200;
did = dopen('myfldr');
do i = 1 to dnum(did);
fname = dread(did,i);
if index(lowcase(fname), 'json') then output;
end;
did = dclose(did);
keep fname;
run;
 
filename folders temp;
proc http
url = "https://xxxxxxxxx"
out= folders
oauth_bearer = sas_services;
headers
'Accept'= 'application/vnd.sas.collection+json';
run;
libname folders clear;
libname folders json;
 
%let hostname = xxxxxxxxx;
%let endpoint = /folders/folders;
%let path = "/Public/ODAP_REPORTS" ;
%let output = "/out_json_backup";
%let currentDateTime = %sysfunc(datetime(), datetime20.);
%put &amp;amp;currentDateTime;
 
/**********************************************************/
/* Retrieve the ID of the folder where the report resides */
/**********************************************************/
filename folders clear;
filename folders temp;
 
proc http
url = "&amp;amp;hostname.&amp;amp;endpoint/@item"
query = ("path"=&amp;amp;path)
out= folders
oauth_bearer = sas_services;
headers
'Accept'= 'application/vnd.sas.content.folder+json';
run;
 
libname folders clear;
libname folders json;
 
/*************************************************/
/* Get a list of objects in that specific folder */
/*************************************************/
/* Identify the endpoint to be used */
proc sql ;
select href, type into :endpoint, :type
from folders.links
where method="GET" and rel = "members";
quit;
 
/* Retrieve the list of objects */
filename memList clear;
filename memList temp;
 
proc http
url = "&amp;amp;hostname.%trim(&amp;amp;endpoint)"
out= memList
oauth_bearer = sas_services;
headers
"Accept"= "%trim(&amp;amp;type)+json";
run;
 
libname memList clear;
libname memList json;
 
/******************************************************/
/* Extract report content (structure) for each report */
/******************************************************/
/* Macro to write the output to a json file */
%sysmacdelete writeJson;
%macro writeJson(name, path);
filename outcsv FILESRVC FOLDERPATH="&amp;amp;ppath."  FILENAME="_&amp;amp;name._&amp;amp;currentDateTime..json";
proc json out=outcsv nosastags pretty noscan   ;
export casuser.info;
run;
%mend;

%macro get_bu_date(name);
proc sql;
select substr(fname, 1, prxmatch("m/(?&amp;lt;=_)(\d{2}.*)(?=\.json)/oi",fname)-2) as rep_name,  floor(max(input(substr(fname, prxmatch("m/(?&amp;lt;=_)(\d{2}.*)(?=\.json)/oi",fname), 18), anydtdtm.))) format=best12. into :nom, :_last 
from WORK.FILENAMES
where prxmatch("m/_&amp;amp;name/oi",fname)
group by 1;
quit;

data _null_;
set WORK.FILENAMES;
where prxmatch("m/_&amp;amp;name/oi",fname);
retain valor;
valor=max(valor, floor(max(input(substr(fname, prxmatch("m/(?&amp;lt;=_)(\d{2}.*)(?=\.json)/oi",fname), 18), anydtdtm.))));
call symputx('_last', valor);
run;
%put &amp;amp;_last.;
%mend;
 
/* Macro to read the report content and generate the output file */
%sysmacdelete readReportContent;
%macro readReportContent (url, name, path, endpoint, output);
filename f FILESRVC FOLDERPATH="&amp;amp;path."  FILENAME="text.txt" lrecl=2000000;
/* filename f temp lrecl=2000000; */
 
proc http
url="&amp;amp;url.%trim(&amp;amp;endpoint)/content"
out=f
oauth_bearer=sas_services;
headers
"Accept"="application/vnd.sas.report.content+json";
quit;

%let n=60;

data casuser.info;
length report $128 path $1024 c1-c&amp;amp;n. varchar(32767) content varchar(2000000);
infile f lrecl=2000000 recfm=v  truncover;
input (c1-c&amp;amp;n.) ($32767.);

content=cats(of c1-c&amp;amp;n.);
array c[*] c1-c&amp;amp;n. content;
report="&amp;amp;name";
path="&amp;amp;path";
drop c2-c&amp;amp;n.;
run;

proc sql;
select compress(substr(content, 1, 1000), '"') into :inf
from casuser.info;
run;


data _null_;
pid=prxparse('/(?&amp;lt;=datemodified:)(.{19})/io');
format want $26. fecha datetime19.;
string="&amp;amp;inf.";
put string=;
s=1;e=length(string);
 call prxnext(pid,s,e,string,p,l);
 do while(p&amp;gt;0);
   	want=catx(' ',want,substr(string,p+1,l));
	want=tranwrd(want,'T', ' ');
	fecha=input(want, anydtdtm.);
    call prxnext(pid,s,e,string,p,l);
	put string= want= p= l= fecha=;
	call symputx('_mod', fecha);
 end;
	temp=symget('_last');
	call symputx('lasted', temp);

run;

%put &amp;amp;_mod. &amp;amp;_last. &amp;amp;lasted. ;

%if %sysevalf(&amp;amp;_mod. &amp;gt; &amp;amp;_last.,boolean) %then %do;
%put "executed";
data _null_;
set casuser.info;
where path ne '';
location=tranwrd(path, "/", "_");
mcall=cat('%writeJson(', trim(report), ',', trim(location), ')');
call execute(mcall);
run;

%end;
 
%mend;
 

/* Generate a view containing information to call readReportContent */
proc sql;
create table merged as
select "&amp;amp;hostname" as url,
a.ordinal_items,
a. name,
a.uri,
b.href,
b.type ,
b.method
from memlist.items as a
left join memlist.items_links as b
on a.ordinal_items=b.ordinal_items
having a.contentType="report" and b.rel="getResource";
quit;

options mlogic;
 
/* Call readReportContent macro for each report */
data _null_;
length mcall $1024;
set merged(obs=3 firstobs=3);
out=&amp;amp;output;
path=&amp;amp;path;
/* call symdel("_last"); */
/* call symdel("lasted"); */
/* call symdel("_mod"); */
mstart=cat('%get_bu_date(', trim(name), ')');
call execute (%nrstr(mstart));
/* mcall=cat('%readReportContent(', trim(url), ",", trim(name), ",", trim(path),",", trim(href), ",", trim(out), ')'); */
/* call execute(mcall); */
run;

data _null_;
length mcall $1024;
set merged(obs=3 firstobs=3);
out=&amp;amp;output;
path=&amp;amp;path;
temp= symget("_last");
call symputx('_last', temp);
/* call symdel("lasted"); */
/* call symdel("_mod"); */
mstart=cat('%get_bu_date(', trim(name), ')');
call execute (%nrstr(mstart));
mcall=cat('%readReportContent(', trim(url), ",", trim(name), ",", trim(path),",", trim(href), ",", trim(out), ')');
call execute(mcall);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jan 2025 17:24:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956888#M373593</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2025-01-22T17:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble with macro compilation within macro call</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956889#M373594</link>
      <description>&lt;P&gt;I do not see any place where you are trying to compile a macro definition inside another macro defintion. (Something you shouldn't do anyway as macros use a flat name space unlike macro variables where you can use LOCAL macro variables that only exist while a macro is running.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I suspect your issue is not understanding the scoping rules of MACRO VARIABLES.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you shared too much code as I am having a hard time locating which part of that long program you are having issue with.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Can you just show to part of the code that is trying to set macro variables named _MOD and _LAST?&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Remember if _MOD and _LAST do not exist and you run code to make them inside a macro, like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymac;
%let _mod=1;
%mend;
%mymac;
%put &amp;amp;=_mod ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then they will be defined as LOCAL to the macro and disappear when the macro finishes running.&lt;/P&gt;
&lt;PRE&gt;164  %macro mymac;
165  %let _mod=1;
166  %mend;
167  %mymac;
168  %put &amp;amp;=_mod ;
WARNING: Apparent symbolic reference _MOD not resolved.
_mod&lt;/PRE&gt;
&lt;P&gt;You can either define the macro variable BEFORE calling the macro so that the existing macro variable will be used by the %LET sstatement.&lt;/P&gt;
&lt;PRE&gt;169  %let _mod=before macro call;
170  %put &amp;amp;=_mod;
_MOD=before macro call
171  %mymac;
172  %put &amp;amp;=_mod;
_MOD=1
&lt;/PRE&gt;
&lt;P&gt;Or make the macro smart enough the create a GLOBAL macro variable when there is no one already defined.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymac;
%if not %symexist(_mod) %then %global _mod;
%let _mod=1;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jan 2025 17:40:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956889#M373594</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-22T17:40:48Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble with macro compilation within macro call</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956896#M373595</link>
      <description>&lt;P&gt;Agree with Tom. I would focus on this macro from your example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro get_bu_date(name);
proc sql;
select substr(fname, 1, prxmatch("m/(?&amp;lt;=_)(\d{2}.*)(?=\.json)/oi",fname)-2) as rep_name,  floor(max(input(substr(fname, prxmatch("m/(?&amp;lt;=_)(\d{2}.*)(?=\.json)/oi",fname), 18), anydtdtm.))) format=best12. into :nom, :_last 
from WORK.FILENAMES
where prxmatch("m/_&amp;amp;name/oi",fname)
group by 1;
quit;

data _null_;
set WORK.FILENAMES;
where prxmatch("m/_&amp;amp;name/oi",fname);
retain valor;
valor=max(valor, floor(max(input(substr(fname, prxmatch("m/(?&amp;lt;=_)(\d{2}.*)(?=\.json)/oi",fname), 18), anydtdtm.))));
call symputx('_last', valor);
run;
%put &amp;amp;_last.;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you run that macro, I assume the %PUT &amp;amp;_last statement inside the macro works and the macro variable resolves.&lt;BR /&gt;&lt;BR /&gt;But if after running the macro, you submit&amp;nbsp; another %PUT&amp;nbsp; &amp;amp;_last statement= you probably get a message that &amp;amp;_last does not resolve.&amp;nbsp; This is because &amp;amp;_last was created as a local macro variable, and does not exist outside of the macro. To make it a global macro variable (which may not be the best approach, but it's an option), you could add a %global statement or tell CALL SYMPUTX to make it global:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call symputx('_last', valor,G);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jan 2025 18:12:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956896#M373595</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2025-01-22T18:12:18Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble with macro compilation within macro call</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956968#M373607</link>
      <description>&lt;P&gt;The first time I run the code I don't get a result and several alerts like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WARNING: Apparent symbolic reference _LAST not resolved
WARNING: Apparent symbolic reference INF not resolved
WARNING: Apparent symbolic reference _MOD not resolved
WARNING: Apparent symbolic reference _LAST not resolved
WARNING: Apparent symbolic reference LASTED not resolved
WARNING: Apparent symbolic reference _MOD not resolved
WARNING: Apparent symbolic reference _LAST not resolved
WARNING: Apparent symbolic reference _MOD not resolved
WARNING: Apparent symbolic reference _LAST not resolved
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The second time it produces the desired result writing an additional file to the target folder as the %sysevalf(_mod&amp;gt;_last, boolean) resolved to true.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The third time I run this code it keeps writing to the destination folder but it shouldn't if it fetched correctly the &amp;amp;_mod and &amp;amp;_last.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The fourth time onwards it stops creating additional files.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So my second and fourth time are the correct ones.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro variables resolve delayed or out of context.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the second file is created correctly, the third shouldn't be there.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PIC.png" style="width: 491px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103918i2E4F429595B860F9/image-size/large?v=v2&amp;amp;px=999" role="button" title="PIC.png" alt="PIC.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I added the global option the call symputx.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jan 2025 10:33:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956968#M373607</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2025-01-23T10:33:38Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble with macro compilation within macro call</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956969#M373608</link>
      <description>&lt;P&gt;When I simplify the code it still keeps unresolving.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro get_bu_fecha(name);

data _null_;
set WORK.FILENAMES;
where prxmatch("m/_&amp;amp;name/oi",fname);
retain valor;
valor=max(valor, floor(max(input(substr(fname, prxmatch("m/(?&amp;lt;=_)(\d{2}.*)(?=\.json)/oi",fname), 18), anydtdtm.))));
call symputx('_last', valor, G);
run;
%put &amp;amp;_last.;
%mend;

%get_bu_fecha(S1_AEIT_LS01_ModelSelection);

%put &amp;amp;_last.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;80   %get_bu_fecha(S1_AEIT_LS01_ModelSelection);&lt;BR /&gt;NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).&lt;BR /&gt;      80:234   &lt;BR /&gt;NOTE: There were 1 observations read from the data set WORK.FILENAMES.&lt;BR /&gt;      WHERE PRXMATCH('m/_S1_AEIT_LS01_ModelSelection/oi', fname);&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;      real time           0.01 seconds&lt;BR /&gt;      cpu time            0.00 seconds&lt;BR /&gt;      &lt;BR /&gt;1990008012&lt;BR /&gt;WARNING: Apparent symbolic reference _LAST not resolved.&lt;BR /&gt;81   &lt;BR /&gt;82   %put &amp;amp;_last.;&lt;BR /&gt;&amp;amp;_last.&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jan 2025 10:48:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956969#M373608</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2025-01-23T10:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble with macro compilation within macro call</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956973#M373609</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/127222"&gt;@acordes&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;When I simplify the code it still keeps unresolving.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is because of the missing quotation marks in the third argument of the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p1fa0ay5pzr9yun1mvqxv8ipzd4d.htm" target="_blank" rel="noopener"&gt;CALL SYMPUTX routine&lt;/A&gt;:&lt;/P&gt;
&lt;PRE&gt;call symputx('_last', valor, &lt;STRONG&gt;&lt;FONT color="#00DD00"&gt;'&lt;/FONT&gt;&lt;/STRONG&gt;G&lt;STRONG&gt;&lt;FONT color="#00DD00"&gt;'&lt;/FONT&gt;&lt;/STRONG&gt;);&lt;/PRE&gt;
&lt;P&gt;The note&lt;/P&gt;
&lt;PRE&gt;NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      80:234   &lt;/PRE&gt;
&lt;P&gt;in the log documents the conversion of the value of the (most likely uninitialized) numeric variable &lt;FONT face="courier new,courier"&gt;G&lt;/FONT&gt; to a &lt;STRIKE&gt;(missing)&lt;/STRIKE&gt; character value probably like &lt;FONT face="courier new,courier"&gt;'&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.'&lt;/FONT&gt;&amp;nbsp;(a period, right-aligned in a 12-character string).&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jan 2025 14:55:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956973#M373609</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2025-01-23T14:55:09Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble with macro compilation within macro call</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956984#M373611</link>
      <description>&lt;P&gt;I found the solution following on this blog post.&lt;/P&gt;
&lt;P&gt;&lt;A title="resolving macro within call execute" href="https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-getting-resolved-when-use-Call-execute/td-p/246980" target="_self"&gt;https://communities.sas.com/t5/SAS-Programming/Macro-variable-not-getting-resolved-when-use-Call-execute/td-p/246980&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PIC.png" style="width: 497px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103920i7306D3770DE53DB2/image-size/large?v=v2&amp;amp;px=999" role="button" title="PIC.png" alt="PIC.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;And the final code is this one:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;cas mySession sessopts=(caslib=casuser timeout=1800 locale="en_US");
caslib _all_ assign;

/* --- Begin Edit --- */
%let BASE_URI=%sysfunc(getoption(servicesbaseurl));

%put &amp;amp;base_uri.;

%let baseurl=xxxxxxxxxxx;

/* %let ppath=/Users/xxxxx@comp.com/My Folder/in_pdf/; */
%let ppath=/Public/ODAP_REPORTS/out_json_backup/;

filename myfldr filesrvc folderPath="&amp;amp;ppath.";

data filenames;
length  fname $200;
did = dopen('myfldr');
do i = 1 to dnum(did);
fname = dread(did,i);
if index(lowcase(fname), 'json') then output;
end;
did = dclose(did);
keep fname;
run;
 
filename folders temp;
proc http
url = "https://xxxxxxxxx"
out= folders
oauth_bearer = sas_services;
headers
'Accept'= 'application/vnd.sas.collection+json';
run;
libname folders clear;
libname folders json;
 
%let hostname = xxxxxxxxxx;
%let endpoint = /folders/folders;
%let path = "/Public/ODAP_REPORTS" ;
%let output = "/out_json_backup";
%let currentDateTime = %sysfunc(datetime(), datetime20.);
%put &amp;amp;currentDateTime;
 
/**********************************************************/
/* Retrieve the ID of the folder where the report resides */
/**********************************************************/
filename folders clear;
filename folders temp;
 
proc http
url = "&amp;amp;hostname.&amp;amp;endpoint/@item"
query = ("path"=&amp;amp;path)
out= folders
oauth_bearer = sas_services;
headers
'Accept'= 'application/vnd.sas.content.folder+json';
run;
 
/* libname folders clear; */
libname folders json;
 
/*************************************************/
/* Get a list of objects in that specific folder */
/*************************************************/
/* Identify the endpoint to be used */
proc sql ;
select href, type into :endpoint, :type
from folders.links
where method="GET" and rel = "members";
quit;
 
/* Retrieve the list of objects */
/* filename memList clear; */
filename memList temp;
 
proc http
url = "&amp;amp;hostname.%trim(&amp;amp;endpoint)"
out= memList
oauth_bearer = sas_services;
headers
"Accept"= "%trim(&amp;amp;type)+json";
run;
 
/* libname memList clear; */
libname memList json;
 
/******************************************************/
/* Extract report content (structure) for each report */
/******************************************************/
/* Macro to write the output to a json file */
/* %sysmacdelete writeJson; */
%macro writeJson(name, path);
filename outcsv FILESRVC FOLDERPATH="&amp;amp;ppath."  FILENAME="_&amp;amp;name._&amp;amp;currentDateTime..json";
proc json out=outcsv nosastags pretty noscan   ;
export casuser.info;
run;
%mend;

/* Macro to read the report content and generate the output file */
/* %sysmacdelete readReportContent; */
%macro readReportContent (url, name, path, endpoint, output);
filename f FILESRVC FOLDERPATH="&amp;amp;path."  FILENAME="text.txt" lrecl=2000000;
/* filename f temp lrecl=2000000; */
 
proc http
url="&amp;amp;url.%trim(&amp;amp;endpoint)/content"
out=f
oauth_bearer=sas_services;
headers
"Accept"="application/vnd.sas.report.content+json";
quit;

%let n=60;

data casuser.info;
length report $128 path $1024 c1-c&amp;amp;n. varchar(32767) content varchar(2000000);
infile f lrecl=2000000 recfm=v  truncover;
input (c1-c&amp;amp;n.) ($32767.);

content=cats(of c1-c&amp;amp;n.);
array c[*] c1-c&amp;amp;n. content;
report="&amp;amp;name";
path="&amp;amp;path";
drop c2-c&amp;amp;n.;
run;

data _null_;
pid=prxparse('/(?&amp;lt;=datemodified":")(.{19})/io');
format want $26. fecha datetime19.;
set casuser.info;
string=compress(substr(content, 1, 1000));
/* string=symget('inf'); */
put string=;
s=1;e=length(string);
 call prxnext(pid,s,e,string,p,l);
 do while(p&amp;gt;0);
   	want=catx(' ',want,substr(string,p+1,l));
	want=tranwrd(want,'T', ' ');
	fecha=input(want, anydtdtm.);
    call prxnext(pid,s,e,string,p,l);
	put string= want= p= l= fecha=;
	if fecha then call symputx('_mod', fecha, 'G');
 end;

run;

data filenames;
length  fname $200;
did = dopen('myfldr');
do i = 1 to dnum(did);
fname = dread(did,i);
if index(lowcase(fname), 'json') then output;
end;
did = dclose(did);
keep fname;
run;

data _null_;
set WORK.FILENAMES;
where prxmatch("m/_&amp;amp;name/oi",fname);
retain valor;
valor=max(valor, floor(max(input(substr(fname, prxmatch("m/(?&amp;lt;=_)(\d{2}.*)(?=\.json)/oi",fname), 18), anydtdtm.))));
call symputx('_last', valor, 'G');
run;

%put &amp;amp;_mod. &amp;amp;_last. ;

%if %sysevalf(&amp;amp;_mod. &amp;gt; &amp;amp;_last.,boolean) %then %do;
%put "executed";
data _null_;
set casuser.info;
where path ne '';
location=tranwrd(path, "/", "_");
mcall=cat('%writeJson(', trim(report), ',', trim(location), ')');
call execute(mcall);
run;

%end;
 
%mend;
 

/* Generate a view containing information to call readReportContent */
proc sql;
create table merged as
select "&amp;amp;hostname" as url,
a.ordinal_items,
a. name,
a.uri,
b.href,
b.type ,
b.method
from memlist.items as a
left join memlist.items_links as b
on a.ordinal_items=b.ordinal_items
having a.contentType="report" and b.rel="getResource";
quit;

options mlogic;

data _null_ ;
set merged;
/* (obs=3 firstobs=3); */
out=&amp;amp;output;
path=&amp;amp;path;
  call execute(cats(
     '%nrstr(%%readReportContent)'
    ,'(url =',trim(url)
    ,',name =',trim(name)
    ,',path=',trim(path)
    ,',endpoint =',trim(href)
    ,',output=',trim(out)
    ,')'
  ));
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jan 2025 15:02:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/956984#M373611</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2025-01-23T15:02:07Z</dc:date>
    </item>
    <item>
      <title>Re: Having trouble with macro compilation within macro call</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/957002#M373620</link>
      <description>&lt;P&gt;There is no need for TRIM() if you are using CATS() function.&amp;nbsp; That already removes the trailing spaces from the values. It also removes the leading spaces.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;
  set merged;
/* (obs=3 firstobs=3); */
  out=&amp;amp;output;
  path=&amp;amp;path;
  call execute(cats(
     '%nrstr(%readReportContent)'
    ,'(url=',url
    ,',name=',name
    ,',path=',path
    ,',endpoint=',href
    ,',output=',out
    ,')'
  ));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jan 2025 17:43:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Having-trouble-with-macro-compilation-within-macro-call/m-p/957002#M373620</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-23T17:43:51Z</dc:date>
    </item>
  </channel>
</rss>

