<?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: Assign variable length in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/781058#M31713</link>
    <description>&lt;P&gt;I found out that variable lengths were set in an Excel file and used by the program. So I found it cleaner to just update the Excel and it worked!&lt;/P&gt;</description>
    <pubDate>Thu, 18 Nov 2021 16:42:49 GMT</pubDate>
    <dc:creator>juju_p</dc:creator>
    <dc:date>2021-11-18T16:42:49Z</dc:date>
    <item>
      <title>Assign variable length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/780775#M31666</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am working on an existing program and I am trying to figure out when the variable lengths are defined...&lt;/P&gt;
&lt;P&gt;After running the program I see the variable length of DSSCAT is 9 char long and I would like to increase it.&lt;/P&gt;
&lt;P&gt;But from the code, it is not clear when it is assigned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can someone help?&lt;/P&gt;
&lt;P&gt;PRGM&lt;/P&gt;
&lt;P&gt;--------&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data recons (keep=SUBJID DSTERM_all DSDECOD DSCAT DSSCAT DSSTDTC ord /*GENPART*/);
%SLENGTH (DS, DSCAT DSSTDTC DSDECOD DSSCAT);
length DSTERM_all $500;
set Pbp2_gen;

*date of reconsent;
if DRECONM ne . and DRECOND ne . and DRECONY ne . then rconsdt = mdy(DRECONM, DRECOND, DRECONY);

if rconsdt ne . then DSSTDTC = put(rconsdt, e8601da.);



DSTERM_all = 'INFORMED CONSENT OBTAINED';
DSDECOD = DSTERM_all;
DSCAT = 'PROTOCOL MILESTONE';
DSSCAT = 'RECONSE3NT';
ord = 2;
if RECONS = 1;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MACRO SLENGTH&lt;/P&gt;
&lt;P&gt;--------------------------&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO SLENGTH(dset, name_list);

%local i varname vlabel vlen;

%local dsid;
%let dsid = %sysfunc(open(empty_&amp;amp;dset));

LENGTH

%do i=1 %to %sysfunc(countw(&amp;amp;name_list));
%let varname = %scan(&amp;amp;name_list, &amp;amp;i);
%** DO whatever needs to be done for &amp;amp;NEXT_NAME;
%let vnum = %sysfunc(varnum(&amp;amp;dsid,&amp;amp;varname));
%*let vlabel = %sysfunc(varlabel(&amp;amp;dsid,&amp;amp;varname));
%let vlabel = %sysfunc(varlabel(&amp;amp;dsid, %sysfunc(varnum (&amp;amp;dsid,&amp;amp;varname))));
%let vlen = %sysfunc(varlen (&amp;amp;dsid, %sysfunc(varnum (&amp;amp;dsid,&amp;amp;varname))));
%let vtype = %sysfunc(vartype (&amp;amp;dsid, %sysfunc(varnum (&amp;amp;dsid,&amp;amp;varname))));

&amp;amp;varname $&amp;amp;vlen..
%end;

;

%let dsid = %sysfunc(close(&amp;amp;dsid));

%MEND SLENGTH;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Nov 2021 17:00:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/780775#M31666</guid>
      <dc:creator>juju_p</dc:creator>
      <dc:date>2021-11-17T17:00:36Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/780783#M31667</link>
      <description>&lt;P&gt;The variable attributes are taken from a dataset called WORK.EMPTY_DS; you will see that the variable is defined with this length there.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Nov 2021 17:03:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/780783#M31667</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-11-17T17:03:39Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/780784#M31668</link>
      <description>&lt;P&gt;So your macro %SLENGTH() appears to be generating a LENGTH statement.&amp;nbsp; It does this by searching for the variables listed in the second argument from the dataset listed in the first argument.&amp;nbsp; You can test it yourself by running executing it in a %PUT statement and see what is written to the SAS log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could do basically the same thing without needing macro code by just use a SET statement. It can even be one that will never actually execute.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 1=2 then set ds(keep=DSCAT DSSTDTC DSDECOD DSSCAT);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the answer to your question is that the length of DSSCAT is being set from the length it had in the dataset DS.&lt;/P&gt;
&lt;P&gt;If you want set it to something else then you should remove DSSCAT from the list of variables passed to %SLENGTH() and add your own LENGTH statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data recons (keep=SUBJID DSTERM_all DSDECOD DSCAT DSSCAT DSSTDTC ord /*GENPART*/);
  %SLENGTH (DS, DSCAT DSSTDTC DSDECOD);
  length DSSCAT $20 DSTERM_all $500;
...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might also want to prevent DSSCAT from getting the wrong format attached to it. For example if it existed in the input dataset&amp;nbsp;Pbp2_gen then whatever format it had there would be attached.&amp;nbsp; Since there SAS does not need to have special instructions for how to display character strings just remove the format by using a FORMAT statement that does not actually mention any format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format dsscat ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Nov 2021 17:10:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/780784#M31668</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-17T17:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/780793#M31669</link>
      <description>&lt;P&gt;It's not the same dataset that is searched. EMPTY_ is prefixed to the dataset name.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Nov 2021 17:31:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/780793#M31669</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-11-17T17:31:01Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/781051#M31711</link>
      <description>&lt;P&gt;You're absolutely right! and the empty_DS sets the variable with another macro which is opening an excel file where the variable lengths are set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot!&lt;/P&gt;</description>
      <pubDate>Thu, 18 Nov 2021 16:24:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/781051#M31711</guid>
      <dc:creator>juju_p</dc:creator>
      <dc:date>2021-11-18T16:24:51Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/781054#M31712</link>
      <description>&lt;P&gt;You should just fix the %SLENGTH() macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO SLENGTH(dset, name_list);
if 1=2 then set EMPTY_&amp;amp;dset(keep=&amp;amp;name_list);
%MEND SLENGTH;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Nov 2021 16:30:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/781054#M31712</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-18T16:30:17Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable length</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/781058#M31713</link>
      <description>&lt;P&gt;I found out that variable lengths were set in an Excel file and used by the program. So I found it cleaner to just update the Excel and it worked!&lt;/P&gt;</description>
      <pubDate>Thu, 18 Nov 2021 16:42:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-length/m-p/781058#M31713</guid>
      <dc:creator>juju_p</dc:creator>
      <dc:date>2021-11-18T16:42:49Z</dc:date>
    </item>
  </channel>
</rss>

