<?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: Create a comment (string) column based on a set of &amp;quot;if&amp;quot; statements in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865383#M341750</link>
    <description>&lt;P&gt;Yep, another case of Maxim 46.&lt;/P&gt;</description>
    <pubDate>Tue, 21 Mar 2023 08:02:43 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-03-21T08:02:43Z</dc:date>
    <item>
      <title>Create a comment (string) column based on a set of "if" statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865355#M341744</link>
      <description>&lt;P&gt;Hi everyone.&lt;/P&gt;
&lt;P&gt;I am establishing a program to check missing data points based on&amp;nbsp;a set of "if" statements, and I need to generate a "Comment" column to indicate which variables are missing. But I stuck right at the beginning of the program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider that I have a set of demographic characteristics (like age, day/month/year of birth, day/month/year of visit, height, weight, ...) and if a variable is missing, I need to add a string like "Missing age" (for example) into the "Comment" column. If all variables are not missing,&amp;nbsp;"Comment" column is expected to be blank.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The way I think is that, I will use CAT function to add a new string into&amp;nbsp;"Comment" column if a variable meet the missing condition like:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if AGE    = .   then Comment = cat(Comment,"Missing Age");&lt;BR /&gt;if BRTHDY = " " then Comment = cat(Comment,"Missing Birth date");&lt;BR /&gt;if BRTHMO = " " then Comment = cat(Comment,"Missing Birth month");&lt;BR /&gt;if BRTHYR = " " then Comment = cat(Comment,"Missing Birth year");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But after running the data step, the "Comment" column is blank. I can only think that I need a way to pre-define one value of&amp;nbsp;"Comment" column before the "IF" statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could anyone please give me an idea to deal with this situation ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 04:45:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865355#M341744</guid>
      <dc:creator>James_Yu</dc:creator>
      <dc:date>2023-03-21T04:45:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create a comment (string) column based on a set of "if" statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865380#M341747</link>
      <description>&lt;P&gt;The following a bit different from what you've started doing but that's how I'd implement DQ.&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The Error table contains all rows from source where at least one DQ issue has been found&lt;/LI&gt;
&lt;LI&gt;The Exception table contains a row per DQ issue found&lt;/LI&gt;
&lt;LI&gt;Variable _row_num in the Error and Exception table is what allows you to link the tables together.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Below sample code meant as a starting point for you to implement what you need in case you want to take the proposed approach.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.class;
  set sashelp.class;
  if _n_=3 then call missing(name);
  if _n_=3 then call missing(age);
  if _n_=5 then call missing(of _all_);
  if _n_=8 then call missing(height);
run;

data 
  work.error(drop=__:)
  work.exception(keep=_row_num __vname __dq_issue)
  ;
  set work.class;
  array _char {*} _character_;
  array _nums {*} _numeric_;
  __err_flg=0;
  _row_num=_n_;
  length __vname $32;
  length __dq_issue $20;

  do __i=1 to dim(_char);
    if missing(_char(__i)) then 
      do;
        __err_flg=1;
        __vname=vname(_char(__i));
        __dq_issue='Missing';
        output exception;
      end;
  end;

  do __i=1 to dim(_nums);
    if missing(_nums(__i)) then 
      do;
        __err_flg=1;
        __vname=vname(_nums(__i));
        __dq_issue='Missing';
        output exception;
      end;
  end;  

  if cmiss(of _all_)=sum(dim(_char),dim(_nums)) then
    do;
        __err_flg=1;
        __vname='n/a';
        __dq_issue='All vars missing';
        output exception;
    end;

  if __err_flg=1 then output error;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Mar 2023 07:52:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865380#M341747</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-03-21T07:52:36Z</dc:date>
    </item>
    <item>
      <title>Re: Create a comment (string) column based on a set of "if" statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865381#M341748</link>
      <description>&lt;P&gt;The Comment column is blank for a very simple reason: It is not long enough to hold all the data that you are trying to put into it. Why? because you are using the CAT function, and not CATS or CATX, which trim the parameters before appending. And before the CAT call, the COMMENT variable is already full (of blanks).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would rewrite your code as something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if AGE    = .   then call catx(';',Comment,"Missing Age");
if BRTHDY = " " then call catx(';',Comment,"Missing Birth date");
if BRTHMO = " " then call catx(';',Comment,"Missing Birth month");
if BRTHYR = " " then call catx(';',Comment,"Missing Birth year");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I used CATX because you may want a delimiter between the different texts (the ';'). I changed the code to CALL CATX instead of the assignment, because it is faster, CPU-wise.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 08:00:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865381#M341748</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-03-21T08:00:18Z</dc:date>
    </item>
    <item>
      <title>Re: Create a comment (string) column based on a set of "if" statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865382#M341749</link>
      <description>&lt;P&gt;Define the comment variable first with a suitable length, and then study the log after running your step. Also post the complete log (code and messages) here.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length comment $1000;
if AGE    = .   then Comment = catx(',',Comment,"Missing Age");
if BRTHDY = " " then Comment = catx(',',Comment,"Missing Birth date");
if BRTHMO = " " then Comment = catx(',',Comment,"Missing Birth month");
if BRTHYR = " " then Comment = catx(',',Comment,"Missing Birth year");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Why is a day, month, year stored as character?&lt;/P&gt;
&lt;P&gt;Also consider the use of the MISSING function to determine missing values.&lt;/P&gt;
&lt;P&gt;CATX will insert a delimiter for better readability.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 08:00:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865382#M341749</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-21T08:00:27Z</dc:date>
    </item>
    <item>
      <title>Re: Create a comment (string) column based on a set of "if" statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865383#M341750</link>
      <description>&lt;P&gt;Yep, another case of Maxim 46.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 08:02:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865383#M341750</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-21T08:02:43Z</dc:date>
    </item>
    <item>
      <title>Re: Create a comment (string) column based on a set of "if" statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865387#M341752</link>
      <description>&lt;P&gt;A note on my previous submission: When using CALL CATX, the COMMENT variable must be defined first, so if you did not define it otherwise, the code I suggested should be changed to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length comment $200;                                                                                                                    
if AGE    = .   then call catx(';',Comment,"Missing Age");                                                                              
if BRTHDY = " " then call catx(';',Comment,"Missing Birth date");                                                                       
if BRTHMO = " " then call catx(';',Comment,"Missing Birth month");                                                                      
if BRTHYR = " " then call catx(';',Comment,"Missing Birth year");          
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;assuming that you are not putting more stuff into the comment variable, 200 characters should be enough.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 08:47:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865387#M341752</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-03-21T08:47:27Z</dc:date>
    </item>
    <item>
      <title>Re: Create a comment (string) column based on a set of "if" statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865633#M341846</link>
      <description>&lt;P&gt;Thank for your help.&lt;/P&gt;
&lt;P&gt;I really appreciate your response.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2023 02:32:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865633#M341846</guid>
      <dc:creator>James_Yu</dc:creator>
      <dc:date>2023-03-22T02:32:47Z</dc:date>
    </item>
    <item>
      <title>Re: Create a comment (string) column based on a set of "if" statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865634#M341847</link>
      <description>&lt;P&gt;Thanks for your help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I will d&lt;SPAN&gt;efine the variable first. The reason why day, month, year stored as character is that we have an option of Unknown (stored as U) with of these variable.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2023 02:34:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865634#M341847</guid>
      <dc:creator>James_Yu</dc:creator>
      <dc:date>2023-03-22T02:34:45Z</dc:date>
    </item>
    <item>
      <title>Re: Create a comment (string) column based on a set of "if" statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865648#M341848</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/383618"&gt;@James_Yu&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for your help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I will d&lt;SPAN&gt;efine the variable first. The reason why day, month, year stored as character is that we have an option of Unknown (stored as U) with of these variable.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In SAS, such a value would be a missing value, represented by a dot for numeric variables. You can handle this with a custom informat when you read the data into SAS.&lt;/P&gt;
&lt;P&gt;If you want to make a difference between "nothing was entered" and "U", you can use a special missing value like&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;.U&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Mar 2023 05:07:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865648#M341848</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-22T05:07:40Z</dc:date>
    </item>
    <item>
      <title>Re: Create a comment (string) column based on a set of "if" statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865680#M341857</link>
      <description>&lt;P&gt;Thanks for your suggestion.&lt;/P&gt;
&lt;P&gt;Currently our system allows personnel to mark a variable as Unknown by clicking some buttons (not by entering data) and in the extracted dataset, the unknown cells are filled with "U". This is the reason why I specify my variables as text.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2023 09:22:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865680#M341857</guid>
      <dc:creator>James_Yu</dc:creator>
      <dc:date>2023-03-22T09:22:29Z</dc:date>
    </item>
    <item>
      <title>Re: Create a comment (string) column based on a set of "if" statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865743#M341882</link>
      <description>&lt;P&gt;How do you get the data from this data entry system into SAS? This the point where you convert "U" to a missing value.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2023 14:29:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-comment-string-column-based-on-a-set-of-quot-if-quot/m-p/865743#M341882</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-22T14:29:46Z</dc:date>
    </item>
  </channel>
</rss>

