<?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 Correct way to handle Datagrids in Intelligent Decisioning in SAS Viya</title>
    <link>https://communities.sas.com/t5/SAS-Viya/Correct-way-to-handle-Datagrids-in-Intelligent-Decisioning/m-p/953223#M2684</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm unsure how data grids are supposed to be initialised in Intelligent Decisioning. Working in a ds2 code file, I declare my data grid in the variables tab and define the columns as such:&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="AndrewM_0-1733916816653.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102903i54218B1D3AA00549/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AndrewM_0-1733916816653.png" alt="AndrewM_0-1733916816653.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AndrewM_1-1733916874386.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102904i842D602A1BD86E35/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AndrewM_1-1733916874386.png" alt="AndrewM_1-1733916874386.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AndrewM_2-1733916952318.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102905i9D13980D92B7C475/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AndrewM_2-1733916952318.png" alt="AndrewM_2-1733916952318.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We have 3 columns defined: `str1`, `dec1`, and `int1`.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My ds2 code is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;package "${PACKAGE_NAME}" /inline;
    method execute(in_out package datagrid my_datagrid);

        dcl varchar col_name;
        dcl int num_cols;&lt;BR /&gt;        dcl int num_rows;
        dcl int i;

        num_cols = DATAGRID_COLUMNCOUNT(my_datagrid) ;
        put num_cols=;
        do i = 1 to num_cols;
            col_name = DATAGRID_COLUMNNAME(my_datagrid, i);
            put col_name=;
        end;
        &lt;BR /&gt;        DATAGRID_ADDROW(my_datagrid, 1);&lt;BR /&gt;        num_rows = DATAGRID_COUNT(my_datagrid);&lt;BR /&gt;
        DATAGRID_SET(my_datagrid, 'str1', num_rows, 'abc');
        DATAGRID_SET(my_datagrid, 'dec1', num_rows, 3.14);
        DATAGRID_SET(my_datagrid, 'int1', num_rows, 123);

    end;
endpackage;&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;When I score this as a scenario test I expect to see `&lt;SPAN&gt;num_cols=&lt;/SPAN&gt;3` and the three column names printed to the log.&lt;/P&gt;&lt;P&gt;What I actually see is &lt;SPAN&gt;num_cols=&lt;/SPAN&gt;0 and the following error repeated for each column:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;ERROR: Line 30: Datagrid my_datagrid_grid.columnIndex(): Datagrid Column Name not found: 'xxx'&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AndrewM_4-1733917743163.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102907iF14E4302DB0BF039/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AndrewM_4-1733917743163.png" alt="AndrewM_4-1733917743163.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I understand that I can use the various Data Grid functions to initialise the columns, so I could add the following to my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATAGRID_ADDCOLUMN(my_datagrid, 'str1', 'string');
DATAGRID_ADDCOLUMN(my_datagrid, 'dec1', 'decimal');
DATAGRID_ADDCOLUMN(my_datagrid, 'int1', 'integer');&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But if I have to do this, then what is the point of defining the columns in the variables tab? I feel like defining the columns in the variables tab should negate the need to define them a second time in the code file, right?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Additionally, If we define the data grid as both an input and an output then we get inconsistent behavior depending on if the data grid passed as input has it's columns defined or not. So to solve that you first need to check if each and every column exists and then add the column if it doesn't. Like so:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;if NULL(DATAGRID_COLUMNINDEX(my_datagrid, 'str1')) then do;
    DATAGRID_ADDCOLUMN(my_datagrid, 'str1', 'string');
end;
if  NULL(DATAGRID_COLUMNINDEX(my_datagrid, 'dec1')) then do;
    DATAGRID_ADDCOLUMN(my_datagrid, 'dec1', 'decimal');
end;
if NULL(DATAGRID_COLUMNINDEX(my_datagrid, 'int1')) then do;
    DATAGRID_ADDCOLUMN(my_datagrid, 'int1', 'integer');
end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This does not seem intuitive at all. What am I missing here?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 11 Dec 2024 12:33:55 GMT</pubDate>
    <dc:creator>Andrew-M</dc:creator>
    <dc:date>2024-12-11T12:33:55Z</dc:date>
    <item>
      <title>Correct way to handle Datagrids in Intelligent Decisioning</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Correct-way-to-handle-Datagrids-in-Intelligent-Decisioning/m-p/953223#M2684</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm unsure how data grids are supposed to be initialised in Intelligent Decisioning. Working in a ds2 code file, I declare my data grid in the variables tab and define the columns as such:&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="AndrewM_0-1733916816653.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102903i54218B1D3AA00549/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AndrewM_0-1733916816653.png" alt="AndrewM_0-1733916816653.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AndrewM_1-1733916874386.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102904i842D602A1BD86E35/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AndrewM_1-1733916874386.png" alt="AndrewM_1-1733916874386.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AndrewM_2-1733916952318.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102905i9D13980D92B7C475/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AndrewM_2-1733916952318.png" alt="AndrewM_2-1733916952318.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We have 3 columns defined: `str1`, `dec1`, and `int1`.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My ds2 code is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;package "${PACKAGE_NAME}" /inline;
    method execute(in_out package datagrid my_datagrid);

        dcl varchar col_name;
        dcl int num_cols;&lt;BR /&gt;        dcl int num_rows;
        dcl int i;

        num_cols = DATAGRID_COLUMNCOUNT(my_datagrid) ;
        put num_cols=;
        do i = 1 to num_cols;
            col_name = DATAGRID_COLUMNNAME(my_datagrid, i);
            put col_name=;
        end;
        &lt;BR /&gt;        DATAGRID_ADDROW(my_datagrid, 1);&lt;BR /&gt;        num_rows = DATAGRID_COUNT(my_datagrid);&lt;BR /&gt;
        DATAGRID_SET(my_datagrid, 'str1', num_rows, 'abc');
        DATAGRID_SET(my_datagrid, 'dec1', num_rows, 3.14);
        DATAGRID_SET(my_datagrid, 'int1', num_rows, 123);

    end;
endpackage;&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;When I score this as a scenario test I expect to see `&lt;SPAN&gt;num_cols=&lt;/SPAN&gt;3` and the three column names printed to the log.&lt;/P&gt;&lt;P&gt;What I actually see is &lt;SPAN&gt;num_cols=&lt;/SPAN&gt;0 and the following error repeated for each column:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;ERROR: Line 30: Datagrid my_datagrid_grid.columnIndex(): Datagrid Column Name not found: 'xxx'&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AndrewM_4-1733917743163.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102907iF14E4302DB0BF039/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AndrewM_4-1733917743163.png" alt="AndrewM_4-1733917743163.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I understand that I can use the various Data Grid functions to initialise the columns, so I could add the following to my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATAGRID_ADDCOLUMN(my_datagrid, 'str1', 'string');
DATAGRID_ADDCOLUMN(my_datagrid, 'dec1', 'decimal');
DATAGRID_ADDCOLUMN(my_datagrid, 'int1', 'integer');&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But if I have to do this, then what is the point of defining the columns in the variables tab? I feel like defining the columns in the variables tab should negate the need to define them a second time in the code file, right?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Additionally, If we define the data grid as both an input and an output then we get inconsistent behavior depending on if the data grid passed as input has it's columns defined or not. So to solve that you first need to check if each and every column exists and then add the column if it doesn't. Like so:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;if NULL(DATAGRID_COLUMNINDEX(my_datagrid, 'str1')) then do;
    DATAGRID_ADDCOLUMN(my_datagrid, 'str1', 'string');
end;
if  NULL(DATAGRID_COLUMNINDEX(my_datagrid, 'dec1')) then do;
    DATAGRID_ADDCOLUMN(my_datagrid, 'dec1', 'decimal');
end;
if NULL(DATAGRID_COLUMNINDEX(my_datagrid, 'int1')) then do;
    DATAGRID_ADDCOLUMN(my_datagrid, 'int1', 'integer');
end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This does not seem intuitive at all. What am I missing here?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Dec 2024 12:33:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Correct-way-to-handle-Datagrids-in-Intelligent-Decisioning/m-p/953223#M2684</guid>
      <dc:creator>Andrew-M</dc:creator>
      <dc:date>2024-12-11T12:33:55Z</dc:date>
    </item>
  </channel>
</rss>

