<?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: Initializing all array variables to zero if they are missing in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/565771#M158900</link>
    <description>&lt;P&gt;will these code work for character variables&lt;/P&gt;</description>
    <pubDate>Thu, 13 Jun 2019 08:47:34 GMT</pubDate>
    <dc:creator>Vijay77</dc:creator>
    <dc:date>2019-06-13T08:47:34Z</dc:date>
    <item>
      <title>Initializing all array variables to zero if they are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451444#M113809</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have a bunch of variables with their different array lengths. All are fined as PD2. or PD7.2 or PD8.2 or PD5.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At the end of the process, I want to populate the array variables to ZERO if they are having missing values. The below code I wrote but it is not working which strange. Can anyone help me. I don't want to code all array variables to do this as it will be cumbersome, some variables are daily variables and hence the array length if 366 &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The below code is not working!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATA OUTDB.OUTDATA;&lt;/P&gt;
&lt;P&gt;SET OUTDB.OUTDATA;&lt;/P&gt;
&lt;P&gt;ARRAY CHANGE _NUMERIC_;&lt;/P&gt;
&lt;P&gt;DO OVER CHANGE;&lt;/P&gt;
&lt;P&gt;IF CHANGE=. THEN CHANGE=0;&lt;/P&gt;
&lt;P&gt;END;&lt;/P&gt;
&lt;P&gt;RUN ;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 10:39:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451444#M113809</guid>
      <dc:creator>tapas_16880</dc:creator>
      <dc:date>2018-04-05T10:39:51Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing all array variables to zero if they are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451448#M113813</link>
      <description>&lt;UL&gt;
&lt;LI&gt;Don't shout at the poor SAS interpreter, it can handle lowercase very well &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/LI&gt;
&lt;LI&gt;Use the macro from&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; to create a data step from your dataset, and post the resulting code here, according to &lt;A href="https://communities.sas.com/t5/help/faqpage/faq-category-id/posting#posting" target="_blank"&gt;https://communities.sas.com/t5/help/faqpage/faq-category-id/posting#posting&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;That will give us a clear picture of your dataset as it is.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 10:53:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451448#M113813</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-05T10:53:38Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing all array variables to zero if they are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451455#M113815</link>
      <description>&lt;P&gt;Thanks but extracting the code not working from the above link. Can someone tell me why the DATASTEP that I coded not working or can someone give me a piece of code that can initialize all array variables to 0 if they are missing.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 10:59:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451455#M113815</guid>
      <dc:creator>tapas_16880</dc:creator>
      <dc:date>2018-04-05T10:59:14Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing all array variables to zero if they are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451457#M113817</link>
      <description>&lt;P&gt;Your code works, see this proof:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ var1 var2 var3;
cards;
X 1 . 1
Y 2 2 2
Z . . 3
;
run;

DATA want;
SET have;
ARRAY CHANGE _NUMERIC_;
DO OVER CHANGE;
IF CHANGE=. THEN CHANGE=0;
END;
RUN ;

proc print data=want noobs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So it's got to be in the data.&lt;/P&gt;
&lt;P&gt;When you use this macro from the link I gave you:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro data2datastep(dsn,lib,file,obs);
%local varlist msgtype;

%if %superq(obs)= %then %let obs=MAX;

%let msgtype=NOTE;
%if %superq(dsn)= %then %do;
   %let msgtype=ERROR;
   %put &amp;amp;msgtype: You must specify a data set name;
   %put;
   %goto syntax;
%end;
%let dsn=%qupcase(%superq(dsn));
%if %superq(dsn)=!HELP %then %do;
%syntax:
   %put &amp;amp;msgtype: &amp;amp;SYSMACRONAME macro help document:;
   %put &amp;amp;msgtype- Purpose: Converts a data set to a SAS DATA step.;
   %put &amp;amp;msgtype- Syntax: %nrstr(%%)&amp;amp;SYSMACRONAME(dsn&amp;lt;,lib,file,obs&amp;gt;);
   %put &amp;amp;msgtype- dsn:  Name of the dataset to be converted. Required.;
   %put &amp;amp;msgtype- lib:  LIBREF where the dataset resides. Optional.;
   %put &amp;amp;msgtype- file: Fully qulaified filename for the DATA step produced. Optional.;
   %put &amp;amp;msgtype-       Default is %nrstr(create_&amp;amp;lib._&amp;amp;dsn._data.sas) in the SAS default directory.;
   %put &amp;amp;msgtype- obs:  Max observations to include the created dataset. Optional.;
   %put &amp;amp;msgtype-       Default is MAX (all observations);
   %put;
   %put NOTE:   &amp;amp;SYSMACRONAME cannot be used in-line - it generates code.;
   %put NOTE-   Use !HELP to print these notes.;
   %return;
%end; 

%if %superq(lib)= %then %do;
    %let lib=%qscan(%superq(dsn),1,.);
    %if %superq(lib) = %superq(dsn) %then %let lib=WORK;
    %else %let dsn=%qscan(&amp;amp;dsn,2,.);
%end;
%let lib=%qupcase(%superq(lib));
%let dsn=%qupcase(%superq(dsn));

%if %sysfunc(exist(&amp;amp;lib..&amp;amp;dsn)) ne 1 %then %do;
   %put ERROR: (&amp;amp;SYSMACRONAME) - Dataset &amp;amp;lib..&amp;amp;dsn does not exist.;
   %let msgtype=NOTE;
   %GoTo syntax;
%end;

%if %superq(file)= %then %do;
   %let file=create_&amp;amp;lib._&amp;amp;dsn._data.sas;
   %if %symexist(USERDIR) %then %let file=&amp;amp;userdir/&amp;amp;file;
%end;

%if %symexist(USERDIR) %then %do;
   %if %qscan(%superq(file),-1,/\)=%superq(file) %then
      %let file=&amp;amp;userdir/&amp;amp;file;
%end;

proc sql noprint;
select Name
      into :varlist separated by ' '
   from dictionary.columns
   where libname="&amp;amp;lib"
     and memname="&amp;amp;dsn"
;
select case type
          when 'num' then 
             case 
                when missing(format) then cats(Name,':32.')
                else cats(Name,':',format)
             end 
          else cats(Name,':$',length,'.')
       end
      into :inputlist separated by ' '
   from dictionary.columns
   where libname="&amp;amp;lib"
     and memname="&amp;amp;dsn"
;
quit;

data _null_;
   file "&amp;amp;file" dsd;
   if _n_ =1 then do;
      put "data &amp;amp;lib..&amp;amp;dsn;";
      put @3 "infile datalines dsd truncover;";
      put @3 "input %superq(inputlist);";
      put "datalines4;";
   end;
   set &amp;amp;lib..&amp;amp;dsn(obs=&amp;amp;obs) end=last; 
   put &amp;amp;varlist @;
   if last then do;
      put;
      put ';;;;';
   end;
   else put;
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;like this (with my example data):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%data2datastep(have,work,$HOME/sascommunity/have.sas,3)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.HAVE;
  infile datalines dsd truncover;
  input id:$8. var1:32. var2:32. var3:32.;
datalines4;
X,1,,1
Y,2,2,2
Z,,,3
;;;;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;in the file named.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 11:06:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451457#M113817</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-05T11:06:47Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing all array variables to zero if they are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451460#M113825</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ var1 var2 var3;
cards;
X 1 . 1
Y 2 2 2
Z . . 3
;
run;
proc stdize data=have out=want missing=0 reponly;
var var1-var3;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Apr 2018 11:18:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451460#M113825</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-04-05T11:18:39Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing all array variables to zero if they are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451484#M113836</link>
      <description>&lt;P&gt;You can also use the missing() function rather than the =. if there are other types of missing:&lt;/P&gt;
&lt;PRE&gt;data outdb.outdata;
  set outdb.outdata;
  array change _numeric_;
  do over change;
    if missing(change) then change=0;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Though as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;has said, without seeing the data its impossible for us to diagnose any issue, hence why the guidance for posting a new question clearly states to post such test data.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 12:40:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451484#M113836</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-05T12:40:10Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing all array variables to zero if they are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451563#M113876</link>
      <description>&lt;P&gt;Doesn't work is awful vague.&lt;BR /&gt;&lt;BR /&gt;Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.&lt;BR /&gt;&lt;BR /&gt;No output? Post any log in a code box.&lt;BR /&gt;&lt;BR /&gt;Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat&lt;/A&gt;... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With that out of the way use of the input data set as the output data set will cause hard to diagnose problems because the first trial execution changes in the input data set and you cannot compare the results for validity of the code result. If there were an error or miscode it may be next to impossible to reverse it so you have to go back further in the process to rebuild the source data set.&lt;/P&gt;
&lt;PRE&gt;DATA OUTDB.OUTDATA;

SET OUTDB.OUTDATA;
&amp;lt;any code modifying variables from the incoming data&amp;gt;&lt;/PRE&gt;
&lt;P&gt;For testing of recodes you should direct the output to a different data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Apr 2018 15:20:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/451563#M113876</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-05T15:20:42Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing all array variables to zero if they are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/565771#M158900</link>
      <description>&lt;P&gt;will these code work for character variables&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 08:47:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/565771#M158900</guid>
      <dc:creator>Vijay77</dc:creator>
      <dc:date>2019-06-13T08:47:34Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing all array variables to zero if they are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/565778#M158903</link>
      <description>&lt;P&gt;Maxim 4. Try it.&lt;/P&gt;
&lt;P&gt;Then look at the log.&lt;/P&gt;
&lt;P&gt;Or follow Maxim 6 (Google Is Your Friend) to follow Maxim 1 (Read The Documentation). Do a search for "SAS proc stdize". You get a link to&amp;nbsp;&lt;A href="https://support.sas.com/documentation/cdl/en/statug/63962/HTML/default/viewer.htm#statug_stdize_sect001.htm" target="_blank" rel="noopener"&gt;https://support.sas.com/documentation/cdl/en/statug/63962/HTML/default/viewer.htm#statug_stdize_sect001.htm&lt;/A&gt;&amp;nbsp;and will see (in the Overview section)&lt;/P&gt;
&lt;H1 id="statug.stdize.stdizeov" class="title"&gt;Overview: STDIZE Procedure&lt;/H1&gt;
&lt;P&gt;The STDIZE procedure standardizes one or more &lt;STRONG&gt;numeric&lt;/STRONG&gt; variables&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(emphasis by me)&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/277769"&gt;@Vijay77&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;will these code work for character variables&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 09:01:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/565778#M158903</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-13T09:01:42Z</dc:date>
    </item>
    <item>
      <title>Re: Initializing all array variables to zero if they are missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/565803#M158910</link>
      <description>&lt;P&gt;The major reason this code will fail:&amp;nbsp; perhaps your data set already contains a variable named CHANGE.&amp;nbsp; It's illegal to use the same name for both a variable and an array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;pointed out, it is not a good idea to use this form of a DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data outdb.outdata;&lt;/P&gt;
&lt;P&gt;set outdb.outdata;&lt;/P&gt;
&lt;P&gt;.....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is dangerous, particularly when your code needs to be tested and debugged.&amp;nbsp; It is possible that your original version of the data did not contain a variable named CHANGE, but one of the tests inadvertently added such a variable to the data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any event, use an array name that does not duplicate the name of an existing variable.&amp;nbsp; If that's not the problem, post the log because the log messages are always an important clue.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 11:33:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Initializing-all-array-variables-to-zero-if-they-are-missing/m-p/565803#M158910</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-06-13T11:33:43Z</dc:date>
    </item>
  </channel>
</rss>

