<?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: %put log file to run instead of just displaying in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/376307#M90312</link>
    <description>&lt;P&gt;Hi everyone. Thanks for all the advices. After rounds and days of troubleshooting, i found out that the generated SQL statement(by using %put in LOG FILE tab) is not 100% correct and hence causing&amp;nbsp;error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, after fixing the error, i am still back to the first question. How do i further process this process&amp;nbsp;to let the system run my generated sql statement?&lt;/P&gt;&lt;P&gt;What i got is a set of codes with loops generating the necessary sql statement with conditional case when all in good shape when using %put.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did talk to my senior and he told me what i need to do now is use datastep to assign the generated script into data step then execute the data step to run the generated sql statement. This is something new to me and i need advice on how to assign&amp;nbsp;a loop into datastep and execute it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am kinda worried as i have already spent weeks to figure out this entire programme. It would be great if i can get an advice on how to do it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, if you look at my loop, argument after %then %do seems not correct as i think we have to do assignment or logical expression within the %do right? by doing this statement:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if xxx
%then %do;
'CASE WHEN A = 1 THEN BBB ELSE CCC;'
%end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;By looking at this sample, is it right or wrong? As there is no assignment or logical expression. Do note that it will work if i add %put to show in log file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 16 Jul 2017 07:06:52 GMT</pubDate>
    <dc:creator>imdickson</dc:creator>
    <dc:date>2017-07-16T07:06:52Z</dc:date>
    <item>
      <title>%put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374228#M89581</link>
      <description>&lt;P&gt;Hi Everyone. I have developed a simple programme where it will do a loop based on the source table and read all its column name with special handling for CASE WHEN.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the thing. I managed to complete the programme for all the data that supposed to be display out IN LOG FILE ONLY, meaning I am only using %put for all the results that I want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since I have already managed to get what i want, i want to get those data to be executed by programme instead of just displaying in log file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output that i generate using loop is actually a proc sql statement with different table name and column name each time it loops.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me show some sample data:&lt;/P&gt;&lt;PRE&gt;proc sql;
create table
CSTG_WELLMON_SURFACE_PRESSURE as(
case when CCP_UOM = PSIG then CCP_UOM 2nd condition
when CCP_UOM = BARG then CCP *  formula  3rd condition
when CCP_UOM = kPa then CCP *  formula  3rd condition
, case when CCP_UOM ne PSIG then PSIG    else PSIG end
, case when DHP_UOM = PSIG then DHP_UOM 2nd condition
when DHP_UOM = BARG then DHP *  formula  3rd condition
when DHP_UOM = kPa then DHP *  formula  3rd condition
, case when DHP_UOM ne PSIG then PSIG    else PSIG end
, case when DHT_UOM = °C then DHT_UOM 2nd condition
when DHT_UOM = °F then DHT *  formula  3rd condition
, case when DHT_UOM ne °C then °C    else °C end
, case when FLP_UOM = PSIG then FLP_UOM 2nd condition
when FLP_UOM = BARG then FLP *  formula  3rd condition
when FLP_UOM = kPa then FLP *  formula  3rd condition
, case when FLP_UOM ne PSIG then PSIG    else PSIG end
, case when FLT_UOM = °C then FLT_UOM 2nd condition
when FLT_UOM = °F then FLT *  formula  3rd condition
, case when FLT_UOM ne °C then °C    else °C end
, case when FTHP_UOM = PSIG then FTHP_UOM 2nd condition
when FTHP_UOM = BARG then FTHP *  formula  3rd condition
when FTHP_UOM = kPa then FTHP *  formula  3rd condition
, case when FTHP_UOM ne PSIG then PSIG    else PSIG end
when GAS_LIFT_INJECT_RATE_UOM = MMscf/d then GAS_LIFT_INJECT_RATE *  formula  3rd condition
when GAS_LIFT_INJECT_RATE_UOM = km3/d then GAS_LIFT_INJECT_RATE *  formula  3rd condition
when GAS_LIFT_INJECT_RATE_UOM = skL/d then GAS_LIFT_INJECT_RATE *  formula  3rd condition
when GAS_LIFT_INJECT_RATE_UOM = scf/d then GAS_LIFT_INJECT_RATE *  formula  3rd condition
when GAS_LIFT_INJECT_RATE_UOM = bbtu/d then GAS_LIFT_INJECT_RATE *  formula  3rd condition
,GAS_LIFT_INJECT_RATE_UOM_
, case when ICP_UOM = PSIG then ICP_UOM 2nd condition
when ICP_UOM = BARG then ICP *  formula  3rd condition
when ICP_UOM = kPa then ICP *  formula  3rd condition
, case when ICP_UOM ne PSIG then PSIG    else PSIG end
,OFFLINE_DATE
, case when PCP_UOM = PSIG then PCP_UOM 2nd condition
when PCP_UOM = BARG then PCP *  formula  3rd condition
when PCP_UOM = kPa then PCP *  formula  3rd condition
, case when PCP_UOM ne PSIG then PSIG    else PSIG end
,PLATFORM_CD
,REMARKS
,REPORT_DT
, case when SCP_UOM = PSIG then SCP_UOM 2nd condition
when SCP_UOM = BARG then SCP *  formula  3rd condition
when SCP_UOM = kPa then SCP *  formula  3rd condition
, case when SCP_UOM ne PSIG then PSIG    else PSIG end
, case when SITHP_UOM = PSIG then SITHP_UOM 2nd condition
when SITHP_UOM = BARG then SITHP *  formula  3rd condition
when SITHP_UOM = kPa then SITHP *  formula  3rd condition
, case when SITHP_UOM ne PSIG then PSIG    else PSIG end
,SURFACE_PRESSURE_DATE
,WELL_NAME
,WELL_STATUS
,WELL_TYPE
,WELL_UPTIME
,WF_COACT
,WF_DEL_DATE
,WF_PHASE
,WF_STATUS
,WF_TXNID
,WF_USERID
,WF_USERMODDATE
, case when WHT_UOM = °C then WHT_UOM 2nd condition
when WHT_UOM = °F then WHT *  formula  3rd condition
, case when WHT_UOM ne °C then °C    else °C end
) from CSTGT_WELLMON_SURFACE_PRESSURE;&lt;/PRE&gt;&lt;P&gt;I copied the above proc sql statement from log file. The log files has 37 proc sql with each different source table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, I want the programme to run based on the above sql statement so that when the source data can do unit conversion when the source data does not match with the default unit.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are some of my partial code(please refer to "inner loop* where i do all the %put to see my output:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro conversionmacro;
/*get distinct tablename which starts with CSTGT_*/
proc sql noprint;
create table sourcetblname as(
select distinct memname 
from dictionarytable
where memname like 'CSTGT\_%' escape '\');
quit;

/*get distinct count record which is 37*/
proc sql noprint;
select count(*) into:tabledistinct
from sourcetblname;
quit;

/*Assign distinct all table name into tblnm macro array variable, 37 rows*/
/*proc sql noprint;*/
/*select distinct memname into:tblnm1-:tblnm&amp;amp;SysMaxLong*/
/*from dictionarytable;*/
/*Join UOM Mapping and UOM standard*/
PROC SQL noprint;
create table uomjoined as (
SELECT T1.SOURCETABLE,
SUBSTR(CAT(T1.SOURCETABLE),7,19) AS SHORTSOURCETABLE,
T1.SOURCEVALUENAME,
T1.SOURCEUOMNAME,
T1.SOURCEUOMGROUP,
 T1.TARGETTABLE,
T1.TARGETVALUENAME,
T1.TARGETUOMNAME,
T1.TARGETUOMGROUP,
T2.UOM_GROUP,
T2.FROM,
T2.TO,
T2.FORMULA,
CASE WHEN T2.FROM=T2.TO THEN '1' else '2' END AS DEFAULTFLAG
FROM CDSEXT.CEXT_EXF_UOM_MAPPING T1 
LEFT JOIN CDSEXT.CEXT_EXF_STANDARD_UOM T2 ON T1.SOURCEUOMGROUP=T2.UOM_GROUP
/*WHERE T1.SOURCETABLE LIKE '%EQUIP%'*/
);
QUIT;


/*group by to extract from uomjoined*/
proc sql noprint;
create table uomjoinedforuom as(
select distinct sourcetable,sourceuomname, to
from uomjoined);
/*group by sourcetable, sourceuomname,to;*/
quit;



/*proc sql;*/
/*select t1.memname , */
/*t1.name,*/
/*t2.sourceuomname,*/
/*t2.sourceuomgroup,*/
/*t2.from,*/
/*t2.to,*/
/*t2.formula */
/*from dictionarytable t1 left join uomjoined t2*/
/*on t1.memname=t2.sourcetable and t1.name=t2.sourcevaluename;*/
/*quit;*/




/*left join to get full metadata table with uom details*/
proc sql noprint;
create table METAFULLWITHUOM as(
select t1.tablename , 
t1.columnname,
t2.sourceuomname,
t2.sourceuomgroup,
/*newly added 29th june*/
t2.targetuomname,
t2.targetuomgroup,
t2.from,
T2.to,/*CASE WHEN SUBSTR(t2.sourceuomgroup,1,LENGTH(t2.sourceuomgroup)-4)=t2.sourceuomname THEN t2.TARGETUOMGROUP ELSE T2.TO END,*/
t2.formula ,
case when t2.from=t2.to and t2.from is not null then 1 
	 when t2.from is null then 0
	else 2 end as DEFAULTFLAG
from sourcetablev2 t1 left join uomjoined t2
on SUBSTR(t1.tablename,1,19)=SUBSTR(t2.sourcetable,1,19) and t1.columnname=t2.sourcevaluename)
ORDER BY t1.tablename, t1.columnname,t2.sourceuomname,T2.DEFAULTFLAG ;
quit;

proc sql noprint;
create table METAFULLWITHUOM2 AS(
select t1.tablename , 
t1.columnname,
t1.sourceuomname,
t1.sourceuomgroup,
t1.targetuomname,
t1.targetuomgroup,
t1.from,
T1.to,
t1.formula,
t2.to as uomto,
T1.DEFAULTFLAG
from METAFULLWITHUOM t1 left join uomjoinedforuom t2 on SUBSTR(t1.tablename,1,19)=SUBSTR(t2.sourcetable,1,19) and TRIM(t1.columnname)=TRIM(t2.sourceuomname))
order by tablename, columnname, DEFAULTFLAG ASC;
quit;

proc sql noprint;
select distinct tablename into:tblnm1-:tblnm&amp;amp;SysMaxLong
from metafullwithuom;
quit;


PROC SQL noprint;
SELECT MEMNAME, NAME FROM DICTIONARYTABLE where MEMname like '%CSTGT%';
QUIT; 
/*get source tablename and column name that starts with CSTGT*/
PROC SQL noprint;
CREATE TABLE SOURCETABLEV2 AS(
select memname as tablename,
name as columnname 
from dictionarytable
where tablename like '%CSTGT%');
quit;

/*add target tablename and column name that starts with CSTG*/
PROC SQL noprint;
CREATE TABLE TARGETTABLEV2 AS(
select SUBSTR(memname,1,4) || SUBSTR(memname,6) as tablename,
name as columnname 
from dictionarytable
where memname like '%CSTGT%');
QUIT;

/*PROC SQL noprint;*/
/*create table joinsrctgt as(*/
/*select a.tablename, a.columnname,b.tablename*/
/*from SOURCETABLEV2 a INNER JOIN TARGETTABLEV2 b*/
/*on substr(SOURCETABLEV2.tablename,7)=substr(TARGETTABLEV2.tablename,6) and a.columnname=b.columnname);*/
/*quit;*/

/*proc sql noprint;*/
/*select distinct tablename into:sourcetblnm1-:sourcetblnm&amp;amp;SysMaxLong*/
/*from SOURCETABLEV2;*/

proc sql noprint;
select distinct tablename into:sourcetblnm1-:sourcetblnm&amp;amp;SysMaxLong
from SOURCETABLEV2;
quit;

proc sql noprint;
select distinct tablename into:targettblnm1-:targettblnm&amp;amp;SysMaxLong
from TARGETTABLEV2;
quit;



/*outer loop*/
%do i=1 %to &amp;amp;tabledistinct.;
%let tblnm = &amp;amp;&amp;amp;tblnm&amp;amp;i..;
%let sourcetblnm = &amp;amp;&amp;amp;sourcetblnm&amp;amp;i..;
%let targettblnm = &amp;amp;&amp;amp;targettblnm&amp;amp;i..;

/*%put &amp;amp;tblnm;*/





/*start of inner loop*/
/*create table to stores all column name belongs to CSTGT*/
proc sql noprint;
select count(*) into:columncountnumber
from dictionarytable
where memname = "&amp;amp;tblnm.";
quit;
/*%put &amp;amp;columncountnumber;*/

/*assign columns into array list*/
proc sql noprint;
select COLUMNNAME into:columnarray1-:columnarray&amp;amp;SysMaxLong
from SOURCETABLEV2
where TABLENAME = "&amp;amp;tblnm.";
quit
;




proc sql noprint;
CREATE TABLE ONLYCOLUMNWITHUOM AS(
select t2.tablename , 
t2.columnname,
t2.sourceuomname,
t2.sourceuomgroup,
t2.from,
t2.to,
t2.formula
/*row_number() over(order by t2.columnname) RowNumber*/
from metafullwithuom t2
where sourceuomname is not null);
quit;

/*try logic*/
/*proc sql;*/
/*select columnname into: casewhencolumnarray1-:casewhencolumnarray&amp;amp;SysMaxLong*/
/*from ONLYCOLUMNWITHUOM;*/
/*quit;*/
/**/
/*proc sql;*/
/*select sourceuomname into: casewhensourceuomarray1-:casewhensourceuomarray&amp;amp;SysMaxLong*/
/*from ONLYCOLUMNWITHUOM;*/
/*quit;*/

proc sql noprint;
select columnname, sourceuomname, sourceuomgroup,targetuomname, targetuomgroup,from, to, formula , uomto, DEFAULTFLAG
into: cwcolumn1-:cwcolumn&amp;amp;SysMaxLong,:cwsourceuomname1-:cwsourceuomname&amp;amp;SysMaxLong,
:cwsourceuomgroup1-:cwsourceuomgroup&amp;amp;SysMaxLong,:targetuomname1-:targetuomname&amp;amp;SysMaxLong,:targetuomgroup1-:targetuomgroup&amp;amp;SysMaxlong,  :cwfrom1-:cwfrom&amp;amp;SysMaxLong, :cwto1-:cwto&amp;amp;SysMaxLong, 
:cwformula1-:cwformula&amp;amp;SysMaxlong, :cwuomto1-:cwuomto&amp;amp;SysMaxLong, :cwdefaultflag1-:cwdefaultflag&amp;amp;SysMaxLong
from METAFULLWITHUOM2
where TABLENAME = "&amp;amp;tblnm.";
quit;



proc sql noprint;
select count(*) into:cwcount
from METAFULLWITHUOM
where TABLENAME = "&amp;amp;tblnm."
/*newly added 29th june 2017*/
;
quit;


/*TRY WITH UOM ONLY COLUMN*/
proc sql noprint;
select columnname, sourceuomname, sourceuomgroup,from, to, formula 
into: cwv2column1-:cwv2column&amp;amp;SysMaxLong,:cwv2sourceuomname1-:cwv2sourceuomname&amp;amp;SysMaxLong,
:cwv2sourceuomgroup1-:cwv2sourceuomgroup&amp;amp;SysMaxLong,:cwv2from1-:cwv2from&amp;amp;SysMaxLong, :cwv2to1-:cwv2to&amp;amp;SysMaxLong, :cwv2formula1-:cwv2formula&amp;amp;SysMaxlong
from ONLYCOLUMNWITHUOM
where TABLENAME = "&amp;amp;tblnm.";
quit;


/*%let sqlheader = proc sql; create table &amp;amp;targettblnm as(;*/
/*%put &amp;amp;sqlheader;*/

/*print table name*/
%put proc sql%STR(;);
%put create table;
%put &amp;amp;targettblnm as(;
%let comma =;
%let null=;
%let case2=case;
%let comma2=,;
%let one=1;
%let two=2;
%let previous=;
/*proc sql noprint; */
/*create table &amp;amp;targettblnm as(*/
	%do k=1 %to &amp;amp;cwcount;
	/*first condition is to change unit only*/
	%if "&amp;amp;previous" ne "&amp;amp;&amp;amp;cwcolumn&amp;amp;k.." %then %do;
	%let tamat=end;
	%end;
	%else %do;
	%let tamat=;
	%end;
	%if "&amp;amp;&amp;amp;cwuomto&amp;amp;k.." ne "&amp;amp;null" %then %do;
/*		%put &amp;amp;tamat. */
	%put	&amp;amp;comma. case when &amp;amp;&amp;amp;cwcolumn&amp;amp;k.. ne &amp;amp;&amp;amp;cwuomto&amp;amp;k.. then &amp;amp;&amp;amp;cwuomto&amp;amp;k.. 
		else &amp;amp;&amp;amp;cwuomto&amp;amp;k.. end;
	%end;
	/*second condition is to convert based on formula*/

	%else %if "&amp;amp;&amp;amp;cwsourceuomname&amp;amp;k.." ne "&amp;amp;null" and "&amp;amp;&amp;amp;cwdefaultflag&amp;amp;k.." eq "&amp;amp;one" %then %do;

/*		%put */
	%put	&amp;amp;comma. case when &amp;amp;&amp;amp;cwsourceuomname&amp;amp;k.. = &amp;amp;&amp;amp;cwto&amp;amp;k.. then &amp;amp;&amp;amp;cwsourceuomname&amp;amp;k.. 2nd condition;

		
		
	%end;

	%else %if "&amp;amp;&amp;amp;cwsourceuomname&amp;amp;k.." ne "&amp;amp;null" and "&amp;amp;&amp;amp;cwdefaultflag&amp;amp;k.." eq "&amp;amp;two" %then %do;
		%put 
		when &amp;amp;&amp;amp;cwsourceuomname&amp;amp;k.. = &amp;amp;&amp;amp;cwfrom&amp;amp;k.. then &amp;amp;&amp;amp;cwcolumn&amp;amp;k * &amp;amp;&amp;amp;cwformula&amp;amp;k.. formula  3rd condition ;
	%end;
	%else %do;
	%put 
	&amp;amp;comma&amp;amp;&amp;amp;cwcolumn&amp;amp;k..;
	%end;
	%let comma=,;
	%let previous=&amp;amp;&amp;amp;cwcolumn&amp;amp;k..;


	%end;
	%put ) from &amp;amp;sourcetblnm%STR(;);
	quit;
%let sqlfooter = ) from &amp;amp;sourcetblnm%STR(;);
/*	%put ) from &amp;amp;sourcetblnm%STR(;);*/
/*%put &amp;amp;sqlfooter;*/





	

%end;
%mend;
%conversionmacro;



&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need your advice to change my log file proc sql statement back to the programme itself instead of %put.&lt;/P&gt;</description>
      <pubDate>Sun, 09 Jul 2017 06:12:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374228#M89581</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2017-07-09T06:12:28Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374230#M89582</link>
      <description>&lt;P&gt;Remove the %PUT to generate executable code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or write the put statements to a text file in the work library and use %include to execute the code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may want to look into CALL EXECUTE/DOSUBL to automate your code instead of this loop.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Jul 2017 07:33:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374230#M89582</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-09T07:33:54Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374233#M89584</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/63520"&gt;@imdickson&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;SAS macros are your automated typewriters. Just don't use %PUT but simpy have the SAS code generated. Once the macro stuff is done the SAS interpreter takes over and executes whatever the macro generates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* options 1: using macro */
options mprint;
%macro demo();

  proc sql noprint;
    select cats(libname,'.',memname) into :tbl_list separated by ' '
    from dictionary.tables
    where libname='SASHELP' and memname like 'CLASS%'
    ;
  quit;

  %if %sysfunc(countw(&amp;amp;tbl_list,%str( ))) = 1 %then
    %do;
      data test_singleTbl;
        set &amp;amp;tbl_list;
      run;
    %end;
    %do;
      data test_multipleTbl;
    %end;
        set &amp;amp;tbl_list;
      run;

%mend;


%demo()
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I often prefer to use a &lt;EM&gt;data _null_&lt;/EM&gt; step to generate code. I then &lt;EM&gt;PUT&lt;/EM&gt; the code into a temporary file and then &lt;EM&gt;%include&lt;/EM&gt; this file after the &lt;EM&gt;run&lt;/EM&gt; statement. I find this often easier to debug as during development is simply use a &lt;EM&gt;file print &lt;/EM&gt;in the &lt;EM&gt;data _null_&lt;/EM&gt; step which shows me all the code without executing it. And once I'm happy I switch over and write to a temporary file (&lt;EM&gt;filename codegen &amp;nbsp;temp;&lt;/EM&gt;) and then &lt;EM&gt;%include codegen / source2;&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Jul 2017 07:51:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374233#M89584</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-07-09T07:51:41Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374238#M89586</link>
      <description>Thanks everyone for the reply. I tried to run the code by just simply removing %put but it will prompt error.&lt;BR /&gt;&lt;BR /&gt;Now my question is, by removing %put, do i have to put quotation for those strings? Words like CASE WHEN and THEN.</description>
      <pubDate>Sun, 09 Jul 2017 09:28:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374238#M89586</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2017-07-09T09:28:33Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374239#M89587</link>
      <description>&lt;P&gt;By simply removing %put and add double quotation for string such as CASE WHEN, THEN, END.&lt;/P&gt;&lt;P&gt;This is the error that i got :&lt;/P&gt;&lt;PRE&gt;NOTE: No rows were selected.
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

proc sql;
create table
CSTG_ACTIVITY_CONCERN_AREA as(

NOTE: Line generated by the macro variable "CWCOLUMN1".
378           ACTION_DATE
              ___________
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn2.
              _
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn3.
              _
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn4.
              _
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn5.
              _
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn6.
              _
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
11                                                         The SAS System                               09:34 Thursday, July 6, 2017

378           ,&amp;amp;cwcolumn7.
              _
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn8.
              _
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn9.
              _
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn10.
              _
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn11.
              _
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn12.
              _
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn13.
              _
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn14.
              _
              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn15.
              _
12                                                         The SAS System                               09:34 Thursday, July 6, 2017

              180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "K".
378           ,&amp;amp;cwcolumn16.&lt;/PRE&gt;&lt;P&gt;I have no clue why am i getting that as It works with %put&lt;/P&gt;</description>
      <pubDate>Sun, 09 Jul 2017 09:33:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374239#M89587</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2017-07-09T09:33:16Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374294#M89612</link>
      <description>&lt;P&gt;The generated SQL that you copied from the log does not look like valid SQL code. &amp;nbsp;For example this little block has extra characters at the end of the lines and is missing the END for the CASE statement and a name to use for the variable generated by the CASE statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;, case when FTHP_UOM = PSIG then FTHP_UOM 2nd condition
when FTHP_UOM = BARG then FTHP *  formula  3rd condition
when FTHP_UOM = kPa then FTHP *  formula  3rd condition&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You also have examples where you appear to be generating string literals without quotes. Like these two:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;, case when WHT_UOM = °C then WHT_UOM 2nd condition
when WHT_UOM = °F then WHT *  formula  3rd condition
, case when WHT_UOM ne °C then °C    else °C end&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would recommend that you post an example of the data that you want to use to generate the SQL statement and an example of a validly generated SQL statement. &amp;nbsp;You can probably simplify the example to just have one or two target variables that need case statements and one or two that don't.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since it looks like some of your CASE statements are using multiple WHEN clauses it will probably be much easier to generate your SQL&amp;nbsp;statement using a data step since then&amp;nbsp;you can use FIRST. and LAST. flags to know when a new target variable starts and ends.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Jul 2017 17:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374294#M89612</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-09T17:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374424#M89648</link>
      <description>Hi Reeza, by right, removing %put would make the programme works. However, I am suspecting the format/syntax in that line of statement is giving error as they are consist of string and variable all without any quotation or whatsover. Would you mind to see if there is any enhancement need to be made there or not?</description>
      <pubDate>Mon, 10 Jul 2017 10:13:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374424#M89648</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2017-07-10T10:13:37Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374435#M89656</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/63520"&gt;@imdickson&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;"By simply removing %put and add double quotation for string such as CASE WHEN, THEN, END."&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;SAS Macros work purely on text level. You certainly don't need double quotes as they are treated as nothing else than just another character.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Using SAS macros can be very powerful but writing and debugging such macros needs experience and can be quite messy (and that's why some people are not big fans).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I suggest you switch over to a SAS data step and generate the code into a file which you then %include. That will make it much easier for you to "see what you get" and to test the generated code.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If you need support with this then please post the source data used to generate the SQL and also provide an example of all the SQL you'd like generated, ...and of course don't expect us to do all the work for you so post some source data for generation of one SQL for you as a starting point to build on it.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2017 10:52:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374435#M89656</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-07-10T10:52:52Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374437#M89657</link>
      <description>&lt;P&gt;Hi there. The reason why the sql statement contains word like 2nd condition is because i use %put to see if it appears in LOG for testing purpose only. I have already removed it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The generated statement should be something like this:&lt;/P&gt;&lt;PRE&gt;proc sql;
create table CSTG_WELL_TEST as (
REPORT_DT,
PLATFORM_CD,
Case When SUCTION_PRESSURE_UOM != TO then TO else TO end as SUCTION_PRESSURE_UOM,
CASE WHEN SUCTION_PRESSURE_UOM = bbs then SUCTION_PRESSURE end as SUCTION_PRESSURE,
WF_PHASE,
WF_STAGE
from CSTGT_WELL_TEST;
quit;&lt;/PRE&gt;&lt;P&gt;iT IS true that some of the case when will have multiple based on the given data from the loop. For example, some of the UOM will have more than 1 unit, so it will generate the WHEN based on the given non defaulted unit.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Come back to my original question, by simply removing %put, do i need to put any quotation or special function to wrap my statement/argument in the %IF condition? I suspect the error is due to the formatting/quatition that i need to put in after removing %put.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2017 10:56:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374437#M89657</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2017-07-10T10:56:49Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374439#M89659</link>
      <description>&lt;P&gt;So you need to make valid SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;You need to add quotes around the values of string literals. &amp;nbsp;Personally I would add the quotes using the QUOTE() function in the step that generats the macro variables. That will handle any embedded quotes in the data.&lt;/LI&gt;
&lt;LI&gt;Make sure to include the SELECT keyword.&lt;/LI&gt;
&lt;LI&gt;You should also remove the macro quoting that you added around the&amp;nbsp;semicolons to make the %PUT statements function.&lt;/LI&gt;
&lt;LI&gt;I would also remove the extra () that you added around the SELECT statement, but I don't think SAS really cares about that as long as you close it&amp;nbsp;properly.&lt;/LI&gt;
&lt;LI&gt;Not sure if your logic can detect that the first CASE statement in your example can be replaced with just a constant.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is how I would format the example you posted.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table CSTG_WELL_TEST as 
  select REPORT_DT
       , PLATFORM_CD
       , case when SUCTION_PRESSURE_UOM ^= 'TO' then 'TO'
              else 'TO' 
         end as SUCTION_PRESSURE_UOM
       , case when SUCTION_PRESSURE_UOM = 'bbs' then SUCTION_PRESSURE 
         end as SUCTION_PRESSURE
       , WF_PHASE
       , WF_STAGE
  from CSTGT_WELL_TEST
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Jul 2017 11:13:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/374439#M89659</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-10T11:13:36Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/376307#M90312</link>
      <description>&lt;P&gt;Hi everyone. Thanks for all the advices. After rounds and days of troubleshooting, i found out that the generated SQL statement(by using %put in LOG FILE tab) is not 100% correct and hence causing&amp;nbsp;error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, after fixing the error, i am still back to the first question. How do i further process this process&amp;nbsp;to let the system run my generated sql statement?&lt;/P&gt;&lt;P&gt;What i got is a set of codes with loops generating the necessary sql statement with conditional case when all in good shape when using %put.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did talk to my senior and he told me what i need to do now is use datastep to assign the generated script into data step then execute the data step to run the generated sql statement. This is something new to me and i need advice on how to assign&amp;nbsp;a loop into datastep and execute it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am kinda worried as i have already spent weeks to figure out this entire programme. It would be great if i can get an advice on how to do it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, if you look at my loop, argument after %then %do seems not correct as i think we have to do assignment or logical expression within the %do right? by doing this statement:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if xxx
%then %do;
'CASE WHEN A = 1 THEN BBB ELSE CCC;'
%end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;By looking at this sample, is it right or wrong? As there is no assignment or logical expression. Do note that it will work if i add %put to show in log file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 16 Jul 2017 07:06:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/376307#M90312</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2017-07-16T07:06:52Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/376309#M90314</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/63520"&gt;@imdickson&lt;/a&gt;&amp;nbsp;Make a simple test case first for something this complex so you understand, in theory AND practice, what you need to build. Break it down to the simplist case, in this, how do you create macro code that executes rather than displays. Review the example from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;and start with the basic code in your case and slowly expand it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 16 Jul 2017 07:00:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/376309#M90314</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-16T07:00:45Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/376337#M90327</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;i
&lt;P&gt;Also, if you look at my loop, argument after %then %do seems not correct as i think we have to do assignment or logical expression within the %do right? by doing this statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if xxx
%then %do;
'CASE WHEN A = 1 THEN BBB ELSE CCC;'
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;By looking at this sample, is it right or wrong? As there is no assignment or logical expression. Do note that it will work if i add %put to show in log file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;SAS macro code is used to generate SAS code that will then be executed by SAS. &amp;nbsp;Your little example above will conditionally generate a string literal. &amp;nbsp;Is that what you wanted? &amp;nbsp;Or did you want it to conditionally generate some actual SAS code? &amp;nbsp;And if you did want to generate a CASE clause for use in an SQL statement why would you generate one that is missing the END keyword and also includes an extra semi-colon?&lt;/P&gt;
&lt;P&gt;Here is more likely what an part of a macro program that is generating a complete SQL statement that includes a CASE clause might look like.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if (&amp;amp;condition) %then %do;
  case when (a=1) then bbb else ccc end
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want an example of how to using data to generate a program take a look at the answer to this recent post.&amp;nbsp;&lt;A href="https://communities.sas.com/t5/General-SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375523#M48053" target="_blank"&gt;https://communities.sas.com/t5/General-SAS-Programming/Best-way-to-find-current-value-of-a-variable/m-p/375523#M48053&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Basically you should start with a simple working example of code for just a few of the cases and create a program that generates that code from data for those cases. &amp;nbsp;Then expand that data and make sure that your code generatation handles that larger case. One big advantage of using a data step to write the code to a file is that you can generate the code and pull it into SAS and test it to help you debug the code generation (and also debug the logic of the code that you want to generate).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 16 Jul 2017 13:40:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/376337#M90327</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-16T13:40:56Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/376338#M90328</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/63520"&gt;@imdickson&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;The following is meant&amp;nbsp;to help you. I hope you won't take offence.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're making in my opinion two junior&amp;nbsp;mistakes.&lt;/P&gt;
&lt;P&gt;1. You start using SAS Macro language before you understand how&amp;nbsp;it works and how it interacts with "normal" SAS language.&lt;/P&gt;
&lt;P&gt;2. You've coded yourself into a mess and you are now trying desperately to fix&amp;nbsp;something complex and messed-up instead of taking a step back.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I would advise you to do:&lt;/P&gt;
&lt;P&gt;If you're in a working environment which accepts that people can get it wrong&amp;nbsp;and have to learn things before they can master them AND you've got more senior colleagues: Admit that you're seriously stuck and can't solve the problem and ask for help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need to hide your situation: Take a step back! Split the big problem into multiple smaller problem and then solve one problem at a time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The people here in this forum are very helpful and have all the knowledge required to support you BUT you need to provide all the necessary information for us to answer you with real and tested code.&lt;/P&gt;
&lt;P&gt;I suggest you create sample data as realistic as possible and post this data here in the form of a SAS data step which creates the sample source data.&lt;/P&gt;
&lt;P&gt;Then you explain us as exact and detailed what you need and you also provide sample output (sample code as you'd like it generated).&lt;/P&gt;
&lt;P&gt;It could&amp;nbsp;take you quite a bit of time to ask a question that strucutured but I promise you it's time well spent and you'll gain more clarity for yourself what needs to be done simply by formulating the problem in such clarity.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And last but not least:&lt;/P&gt;
&lt;P&gt;"&lt;EM&gt;I did talk to my senior and he told me what i need to do now is use datastep to assign the generated script into data step then execute the data step to run the generated sql statement.&lt;/EM&gt;"&lt;/P&gt;
&lt;P&gt;That's not really true for your case. May be your senior didn't really understand what you're doing or you didn't fully understand what you've got told. Below another code sample where SQL code gets generated conditionally by a SAS macro and then executes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint mlogic;
%macro test(selected_var);

  proc sql feedback;
    select 
      name,
      age,
      case
      %if %upcase(&amp;amp;selected_var) = USE_HEIGHT %then
        %do;
          WHEN height&amp;lt;60 THEN 'Height &amp;lt;60' 
          ELSE 'Height &amp;gt;=60'
        %end;
      %else %if %upcase(&amp;amp;selected_var) = USE_WEIGHT %then
        %do;
          WHEN weight&amp;lt;90 THEN 'Weight &amp;lt;90' 
          ELSE 'Weight &amp;gt;=90'
        %end;
          end as 
            my_selected_var length=20
    from sashelp.class
    ;
  quit;

%mend;

%test(use_weight)
%test(use_height)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 16 Jul 2017 13:58:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/376338#M90328</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-07-16T13:58:04Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/377166#M90551</link>
      <description>&lt;P&gt;Hi Guys. Thanks for all the advices. I truly understand about the learn from basics part. Currently i will have to settle my works on my hand first. I talked to my senior and just like what some of you all mentioned, SAS can execute the code straight after removing %put.&lt;BR /&gt;&lt;BR /&gt;I tried to put double quote wrapping like :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;"&amp;amp;comma. &amp;amp;&amp;amp;cwcolumn&amp;amp;k.."&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;(previously i never wrap it with double quote)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I am getting error for all the resolved variable:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;May i know if there is any special handling when it is actually resolving a proc sql statement?&lt;/P&gt;&lt;PRE&gt;proc sql;
create table
CDSSTG.CSTG_ACTIVITY_CONCERN_AREA as(
select

NOTE: Line generated by the macro variable "CWCOLUMN1".
386             " ACTION_DATE
                ______________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN2".
386             ", ACTION_PARTY
                ________________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN3".
11                                                         The SAS System                               15:40 Tuesday, July 11, 2017

386             ", ACTION_REQUIRED
                ___________________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN4".
386             ", ACTIVITIES
                ______________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN5".
386             ", OFFLINE_DATE
                ________________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN6".
386             ", PLATFORM_CD
                _______________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN7".
386             ", REMARKS
                ___________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN8".
386             ", REPORT_DT
                _____________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN9".
386             ", STATUS
                __________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN10".
386             ", TARGET_DATE
                _______________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN11".
386             ", WF_COACT
                ____________
12                                                         The SAS System                               15:40 Tuesday, July 11, 2017

                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN12".
386             ", WF_DEL_DATE
                _______________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN13".
386             ", WF_PHASE
                ____________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN14".
386             ", WF_STATUS
                _____________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN15".
386             ", WF_TXNID
                ____________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN16".
386             ", WF_USERID
                _____________
                180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: Line generated by the macro variable "CWCOLUMN17".
386             ", WF_USERMODDATE
                __________________
                180
from CDSSTG.CSTGT_ACTIVITY_CONCERN_AREA);
ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/PRE&gt;&lt;P&gt;May i know if the double quote wrap here is wrongly used or&amp;nbsp;there is something missing from it.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 03:23:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/377166#M90551</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2017-07-19T03:23:34Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/377168#M90552</link>
      <description>&lt;P&gt;You need to post your actual code in addition to your log. We see the generated errors but not the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did you do a mock example like we suggested? It will help you figure out how to build this process.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 03:29:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/377168#M90552</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-19T03:29:50Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/377239#M90566</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/63520"&gt;@imdickson&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;I truly understand about the learn from basics part. Currently i will have to settle my works on my hand first.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Actually, I believe you haven't understood what we're trying to tell you. If you got lost in your code then DO NOT TRY TO FIX EVERYTHING AT ONCE. Take a step back!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You've got now a macro with lots of code in it and it's not working. One way to go:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1. Create a backup of your current code.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2. Start a new code (a brand new .sas file or EG/Studio code node).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;3. Copy the macro "shell" to your new code: %macro blah(...); %mend; %blah(...) - run it. If no error then proceed else debug and fix.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;4. Copy the very first run group from your backed up code into the new code (ie. the first proc sql, data step etc.). If you've got already macro syntax in this first step: Remove it as far as possible - run it. If no error then proceed else debug and fix.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;5. If there had been macro syntax/logic: add it back - run it. If no error&amp;nbsp;then proceed else debug and fix.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;6. Now add the next logical unit ....and so on and so on, step by step&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For your latest question: I don't believe you need these quotes.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The way SAS Macro code interacts with "normal" SAS code:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;First comes the SAS Macro guy (could also be a girl of course). This one looks all the code and searches for % and &amp;amp; - these are the tokens for the SAS Macro guy telling him that this is his job and he needs to do something with it. The SAS macro guy consumes everything macro and once he's done only non macro stuff remains.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Now comes the SAS girl. She picks up the "left overs" and does then does her thing with it, i.e. executes the "left over" Proc SQL syntax.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If you're using options &lt;EM&gt;mprint&lt;/EM&gt; and &lt;EM&gt;mlogic&lt;/EM&gt; for the code I've posted earlier then the log shows you what the macro guy does and what the SAS girl has to do.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint mlogic;
/* macro definition */
%macro test(selected_var);

  proc sql feedback;
    select 
      name,
      age,
      case
      %if %upcase(&amp;amp;selected_var) = USE_HEIGHT %then
        %do;
          WHEN height&amp;lt;60 THEN 'Height &amp;lt;60' 
          ELSE 'Height &amp;gt;=60'
        %end;
      %else %if %upcase(&amp;amp;selected_var) = USE_WEIGHT %then
        %do;
          WHEN weight&amp;lt;90 THEN 'Weight &amp;lt;90' 
          ELSE 'Weight &amp;gt;=90'
        %end;
          end as 
            my_selected_var length=20
    from sashelp.class
    ;
  quit;

%mend;

/* macro call */
%test(use_weight)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;The macro call generates the following log:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;54         /* macro call */
55         %test(use_weight)
MLOGIC(TEST):  Beginning execution.
MLOGIC(TEST):  Parameter SELECTED_VAR has value use_weight
2                                                          The SAS System                             16:02 Wednesday, July 19, 2017

MPRINT(TEST):   proc sql feedback;
MLOGIC(TEST):  %IF condition %upcase(&amp;amp;selected_var) = USE_HEIGHT is FALSE
MLOGIC(TEST):  %IF condition %upcase(&amp;amp;selected_var) = USE_WEIGHT is TRUE
MPRINT(TEST):   select name, age, case WHEN weight&amp;lt;90 THEN 'Weight &amp;lt;90' ELSE 'Weight &amp;gt;=90' end as my_selected_var length=20 from 
sashelp.class ;
NOTE: Statement transforms to:

        select CLASS.Name, CLASS.Age, case 
                                        when CLASS.Weight &amp;lt; 90 then 'Weight &amp;lt;90'
                                        else 'Weight &amp;gt;=90'
                                        end as my_selected_var length=20
          from SASHELP.CLASS;

MPRINT(TEST):   quit;
NOTE: The PROCEDURE SQL printed page 1.
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.03 seconds
      

MLOGIC(TEST):  Ending execution.
&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;What's shown in lines starting with MLOGIC is what the Macro guy has been doing, what's shown in lines starting with MPRINT is what the macro guy left over for the SAS girl to do; and the rest of the log shows you what the SAS girl has been doing.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;And the sometimes a bit fiddely thing with macro coding: You must code carefully so that the macro guy only leaves stuff behind for the SAS girl which is really hers to deal with&amp;nbsp;- this one is not very generous&amp;nbsp;in this regards and throws errors if not treated with respect.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 11:00:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/377239#M90566</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-07-19T11:00:11Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/377391#M90610</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/63520"&gt;@imdickson&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi Guys. Thanks for all the advices. I truly understand about the learn from basics part. Currently i will have to settle my works on my hand first. I talked to my senior and just like what some of you all mentioned, SAS can execute the code straight after removing %put.&lt;BR /&gt;&lt;BR /&gt;I tried to put double quote wrapping like :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;"&amp;amp;comma. &amp;amp;&amp;amp;cwcolumn&amp;amp;k.."&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(previously i never wrap it with double quote)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I seriously doubt if you want the double quotes in that situation. In SAS code you use quotes around string literals. &lt;STRONG&gt;NOT&lt;/STRONG&gt; around pieces of code. &amp;nbsp;From your macro variable names it looks like COMMA will have a comma in it and &amp;amp;&amp;amp;cwcolumn&amp;amp;k.. should resolve to a variable name. &amp;nbsp;So it looks like you are trying to build part of the variable list in an SQL SELECT statement. Or perhaps part of the variable list in an ORDER BY or GROUP BY clause in an SQL SELECT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you ran this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro gen;
%local comma k ;
%do k=1 %to &amp;amp;n ;
 &amp;amp;comma. &amp;amp;&amp;amp;cwcolumn&amp;amp;k..
 %let comma=,;
%end;
%mend gen;
%let n=2 ;
%let cwcolumn1=FIRST ;
%let cwcolumn2=SECOND ;
%put %gen;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The last line will cause FIRST , SECOND to be written to the SAS log.&lt;/P&gt;
&lt;P&gt;Now if call %gen in the middle of an SQL statement then you could use it to conditionally generate a list of variable names to use in the select statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  select %gen 
    from mydata
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;So it is the same as if I had just hard coded the list of variable names in the SELECT statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  select FIRST , SECOND
    from mydata
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2017 15:31:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/377391#M90610</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-07-19T15:31:07Z</dc:date>
    </item>
    <item>
      <title>Re: %put log file to run instead of just displaying</title>
      <link>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/389481#M93390</link>
      <description>Thanks everyone for the reply. Patrick's first line answered it although i spent weeks to figure out. By removing %put, what i need to do is make sure it is a proper PROC SQL format and that's it. Works like a charm. Thanks everyone.</description>
      <pubDate>Mon, 21 Aug 2017 10:20:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/put-log-file-to-run-instead-of-just-displaying/m-p/389481#M93390</guid>
      <dc:creator>imdickson</dc:creator>
      <dc:date>2017-08-21T10:20:52Z</dc:date>
    </item>
  </channel>
</rss>

