<?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 Append records to a CAS table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Append-records-to-a-CAS-table/m-p/771395#M244787</link>
    <description>&lt;P&gt;I am working in SAS Studio in Viya 3.5.&lt;/P&gt;
&lt;P&gt;I want to maintain an Audit_History table that contains the contents of the SystemData.Audit table.&amp;nbsp; The Audit table only contains data for one week.&amp;nbsp; I want a complete history.&lt;/P&gt;
&lt;P&gt;I tried some simple methods ways that did not work.&amp;nbsp; This code seems to be a brute force method but it works.&amp;nbsp; It updates my Audit_History table (step 4).&amp;nbsp; The record count in step 5 is correct.&amp;nbsp; But when I check the table in Environment Manager, it has not been updated.&amp;nbsp; I tried to add a promote (step 6), but it generates an error "The target table Audit_History already exists".&lt;/P&gt;
&lt;P&gt;I'd like to know why step 4 does not update the table in the vAdm CAS library.&amp;nbsp; What do I need to do to update it?&lt;/P&gt;
&lt;P&gt;Do you have any suggestions for a better way to maintain my Audit_History table?&lt;/P&gt;
&lt;P&gt;Thank you for any help!&lt;/P&gt;
&lt;P&gt;Jerry&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;LIBNAME vSYS cas caslib="SystemData";
LIBNAME vAdm cas caslib="AdminDataCAS";

/* 1. this creates a Work table from the contents of the SystemData.Audit table 
	Some of the columns are not valid SAS names */
proc sql;
    create table wrk_audit as
	        SELECT 	ID
					, 'Time Stamp'n as Timestamp
					, Type
					, Action
					, State
					, Description
					, 'User ID'n	as User_ID
					, Application
					, 'Remote Address'n as Remote_Address
					, URI

        FROM vSys.Audit;
quit;

/* 2. Get the current contents of Audit_History */
data cur_audit_history;
	set vAdm.Audit_History;
run;

/* 3. Remove duplicates */
proc sql;
    create table audit_history_nodups as
		SELECT 	*
        FROM cur_audit_history

		UNION 
		SELECT *
		FROM wrk_audit;
quit;

/* 4. Update the Audit_History table */
data vAdm.Audit_History;    
  set audit_history_nodups;						/* This is the source table */
run;

/* 5. Check how many records are in a table */
proc sql;
	select count(*)
	from	vAdm.Audit_History;
quit;

/* 6. Tried to promote.  ERROR: Already exists */
/*
proc casutil;
	promote casdata="Audit_History"
	incaslib="AdminDataCAS";
run;
*/
/* This creates a .sashdat file from a table in memory.  */
proc casutil;
    save casdata="Audit_History"			/* This is the input table to be saved */
 	incaslib="AdminDataCAS" outcaslib="AdminDataCAS"
	     casout="Audit_History" replace;  /* This is the outut file */
												/* use replace in case the file exists */
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 30 Sep 2021 15:00:40 GMT</pubDate>
    <dc:creator>jblack38</dc:creator>
    <dc:date>2021-09-30T15:00:40Z</dc:date>
    <item>
      <title>Append records to a CAS table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-records-to-a-CAS-table/m-p/771395#M244787</link>
      <description>&lt;P&gt;I am working in SAS Studio in Viya 3.5.&lt;/P&gt;
&lt;P&gt;I want to maintain an Audit_History table that contains the contents of the SystemData.Audit table.&amp;nbsp; The Audit table only contains data for one week.&amp;nbsp; I want a complete history.&lt;/P&gt;
&lt;P&gt;I tried some simple methods ways that did not work.&amp;nbsp; This code seems to be a brute force method but it works.&amp;nbsp; It updates my Audit_History table (step 4).&amp;nbsp; The record count in step 5 is correct.&amp;nbsp; But when I check the table in Environment Manager, it has not been updated.&amp;nbsp; I tried to add a promote (step 6), but it generates an error "The target table Audit_History already exists".&lt;/P&gt;
&lt;P&gt;I'd like to know why step 4 does not update the table in the vAdm CAS library.&amp;nbsp; What do I need to do to update it?&lt;/P&gt;
&lt;P&gt;Do you have any suggestions for a better way to maintain my Audit_History table?&lt;/P&gt;
&lt;P&gt;Thank you for any help!&lt;/P&gt;
&lt;P&gt;Jerry&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;LIBNAME vSYS cas caslib="SystemData";
LIBNAME vAdm cas caslib="AdminDataCAS";

/* 1. this creates a Work table from the contents of the SystemData.Audit table 
	Some of the columns are not valid SAS names */
proc sql;
    create table wrk_audit as
	        SELECT 	ID
					, 'Time Stamp'n as Timestamp
					, Type
					, Action
					, State
					, Description
					, 'User ID'n	as User_ID
					, Application
					, 'Remote Address'n as Remote_Address
					, URI

        FROM vSys.Audit;
quit;

/* 2. Get the current contents of Audit_History */
data cur_audit_history;
	set vAdm.Audit_History;
run;

/* 3. Remove duplicates */
proc sql;
    create table audit_history_nodups as
		SELECT 	*
        FROM cur_audit_history

		UNION 
		SELECT *
		FROM wrk_audit;
quit;

/* 4. Update the Audit_History table */
data vAdm.Audit_History;    
  set audit_history_nodups;						/* This is the source table */
run;

/* 5. Check how many records are in a table */
proc sql;
	select count(*)
	from	vAdm.Audit_History;
quit;

/* 6. Tried to promote.  ERROR: Already exists */
/*
proc casutil;
	promote casdata="Audit_History"
	incaslib="AdminDataCAS";
run;
*/
/* This creates a .sashdat file from a table in memory.  */
proc casutil;
    save casdata="Audit_History"			/* This is the input table to be saved */
 	incaslib="AdminDataCAS" outcaslib="AdminDataCAS"
	     casout="Audit_History" replace;  /* This is the outut file */
												/* use replace in case the file exists */
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Sep 2021 15:00:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-records-to-a-CAS-table/m-p/771395#M244787</guid>
      <dc:creator>jblack38</dc:creator>
      <dc:date>2021-09-30T15:00:40Z</dc:date>
    </item>
    <item>
      <title>Re: Append records to a CAS table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-records-to-a-CAS-table/m-p/771400#M244790</link>
      <description>&lt;P&gt;I just tried this code with the same result, the query that gets the record count is correct (80,000).&amp;nbsp; But when I look at the table in Environment Manager it shows it has not been updated and it shows the old record count (150,000).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc casutil;
 	load data=audit_history_nodups 
	casout="Audit_History" outcaslib="AdminDataCAS" replace;
quit;

/* Check how many records are in Audit_History */
proc sql;
	select count(*)
	from	Vadm.audit_history;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Sep 2021 15:28:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-records-to-a-CAS-table/m-p/771400#M244790</guid>
      <dc:creator>jblack38</dc:creator>
      <dc:date>2021-09-30T15:28:39Z</dc:date>
    </item>
    <item>
      <title>Re: Append records to a CAS table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Append-records-to-a-CAS-table/m-p/771636#M244917</link>
      <description>&lt;P&gt;Okay, I created some code that works.&amp;nbsp; But as I said in the original post, I'm sure there is an easier way.&amp;nbsp; I had to drop the table and re-create it to get it to show up in the CAS library in Environment Manager.&amp;nbsp; Here is my solution in case someone else is having difficulties similar to mine.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate any recommendations anyone has to simplify it.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;Jerry&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;LIBNAME vSYS cas caslib="SystemData";
LIBNAME vAdm cas caslib="AdminDataCAS";

/* 1. this creates a Work table from the contents of the SystemData.Audit table 
	Some of the columns are not valid SAS names */
proc sql;
    create table wrk_audit as
	        SELECT 	ID
					, 'Time Stamp'n as Timestamp
					, Type
					, Action
					, State
					, Description
					, 'User ID'n	as User_ID
					, Application
					, 'Remote Address'n as Remote_Address
					, URI

        FROM vSys.Audit;
quit;

/* 2. Get the current contents of my Audit_History table */
data cur_audit_history;
	set vAdm.Audit_History;
run;

/* 3. Combine the tables and remove duplicates */
proc sql;
    create table audit_history_nodups as
		SELECT 	*
        FROM cur_audit_history

		UNION 
		SELECT *
		FROM wrk_audit;
quit;

/* 4. Drop the Audit History table before we re-create it with new data */
proc casutil;
 	droptable casdata="audit_history" 
	incaslib="AdminDataCAS";
quit;

/* 5. Add new records to the Audit_History table */
proc casutil;
 	load data=audit_history_nodups 
	casout="Audit_History" outcaslib="AdminDataCAS" 
	promote
	;
quit;

/* 6. Create a .sashdat file from the  memory version.  */
proc casutil;
    save casdata="Audit_History"			/* This is the input table to be saved */
 	incaslib="AdminDataCAS" outcaslib="AdminDataCAS"
	     casout="Audit_History" replace;  /* This is the outut file */
												/* use replace in case the file exists */
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Oct 2021 18:23:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Append-records-to-a-CAS-table/m-p/771636#M244917</guid>
      <dc:creator>jblack38</dc:creator>
      <dc:date>2021-10-01T18:23:30Z</dc:date>
    </item>
  </channel>
</rss>

