<?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: Facing Problem while using format in a data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Facing-Problem-while-using-format-in-a-data-step/m-p/270515#M53764</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/40271"&gt;@GunnerEP﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The issue is: Macro variable REQD_FLDS presumably contains a list of names of character variables, including &lt;FONT face="courier new,courier"&gt;name&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;address&lt;/FONT&gt;. When you check the lengths of the variable values (e.g. 'Miller' [length 6] and '123 Main St' [length 11]) with&amp;nbsp;&lt;SPAN&gt;&lt;FONT face="courier new,courier"&gt;length(&amp;amp;&amp;amp;reqdFld&amp;amp;i)&lt;/FONT&gt;, this is fine. But when you want to apply your formats to the&amp;nbsp;variable&lt;EM&gt; names&lt;/EM&gt;, you must put the macro variable reference &lt;FONT face="courier new,courier"&gt;&amp;amp;&amp;amp;reqdFld&amp;amp;i&lt;/FONT&gt; between double quotes. Otherwise, you apply the formats to the&amp;nbsp;variable&lt;EM&gt; values&lt;/EM&gt;, which makes no sense.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Therefore, please try:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;inlgth = input(put("&amp;amp;&amp;amp;reqdFld&amp;amp;i.",$lgth.),3.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;and&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;err_cd = put("&amp;amp;&amp;amp;reqdFld&amp;amp;i.",$err.);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 14 May 2016 14:51:16 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2016-05-14T14:51:16Z</dc:date>
    <item>
      <title>Facing Problem while using format in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Facing-Problem-while-using-format-in-a-data-step/m-p/270504#M53758</link>
      <description>&lt;P&gt;proc format;&lt;BR /&gt;value $err&lt;BR /&gt;'name' = '0001'&lt;BR /&gt;'address' = '0002'&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc format;&lt;BR /&gt;value $lgth&lt;BR /&gt;'name' = 40&lt;BR /&gt;'address' = 48&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%macro check;&lt;/P&gt;&lt;P&gt;%let rcnt = %sysfunc(countw(&amp;amp;reqd_flds.,':'));&lt;BR /&gt;/*where reqd_flds are extracted using proc sql from a table */&lt;/P&gt;&lt;P&gt;%do x = 1 %to &amp;amp;rcnt.;&lt;BR /&gt;%let reqdFld&amp;amp;x. = %scan(&amp;amp;reqd_flds., %eval(&amp;amp;x+0), ':');&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data input_data_2_output error_file_output;&lt;BR /&gt;set input_data_orig;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%do i = 1 %to %eval(&amp;amp;rcnt. - 1);&lt;BR /&gt;inlgth = input(put(&amp;amp;&amp;amp;reqdFld&amp;amp;i.,$lgth.),3.);&lt;/P&gt;&lt;P&gt;if length(&amp;amp;&amp;amp;reqdFld&amp;amp;i) &amp;gt; inlgth then do;&lt;BR /&gt;err_cd = put(&amp;amp;&amp;amp;reqdFld&amp;amp;i.,$err.);&lt;BR /&gt;err_cnt = 1;&lt;BR /&gt;rec_num = _n_;&lt;BR /&gt;record = 'BAD RECORD';&lt;BR /&gt;output error_file_output;&lt;BR /&gt;end;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;%mend check_records;&lt;/P&gt;&lt;P&gt;%check_records;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not getting &lt;SPAN&gt;inlgth &amp;amp;&lt;/SPAN&gt;&amp;nbsp;err_cd getting resolved correctly. Can someone please guide on what am i doing wrong here ?&lt;/P&gt;</description>
      <pubDate>Sat, 14 May 2016 11:35:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Facing-Problem-while-using-format-in-a-data-step/m-p/270504#M53758</guid>
      <dc:creator>GunnerEP</dc:creator>
      <dc:date>2016-05-14T11:35:09Z</dc:date>
    </item>
    <item>
      <title>Re: Facing Problem while using format in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Facing-Problem-while-using-format-in-a-data-step/m-p/270506#M53760</link>
      <description>&lt;P&gt;Dificult to know with out test data. The code looks liek it should work.&lt;/P&gt;
&lt;P&gt;This should work better as it's simpler:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc format;&lt;BR /&gt;value $err&lt;BR /&gt;'name' = '0001'&lt;BR /&gt;'address' = '0002'&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;value $lgth&lt;BR /&gt;'name' = '40'&lt;BR /&gt;'address' = '48'&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;%macro check;&lt;BR /&gt;&lt;BR /&gt;%let rcnt = %sysfunc(countw(&amp;amp;reqd_flds.,':'));&lt;BR /&gt;/*where reqd_flds are extracted using proc sql from a table */&lt;BR /&gt;&lt;BR /&gt;data input_data_2_output error_file_output;&lt;BR /&gt;set input_data_orig;&lt;BR /&gt;&lt;BR /&gt;%do i = 1 %to %eval(&amp;amp;rcnt. - 1);&lt;BR /&gt;%let reqdFld = %scan(&amp;amp;reqd_flds., &amp;amp;i , ':');&lt;BR /&gt;inlgth = input(put(&amp;amp;reqdFld.,$lgth.),3.);&lt;BR /&gt;if length(&amp;amp;reqdFld.) &amp;gt; inlgth then do;&lt;BR /&gt;err_cd = put(&amp;amp;reqdFld.,$err.);&lt;BR /&gt;err_cnt = 1;&lt;BR /&gt;rec_num = _n_;&lt;BR /&gt;record = 'BAD RECORD';&lt;BR /&gt;output error_file_output;&lt;BR /&gt;end;&lt;BR /&gt;%end;&lt;BR /&gt;&lt;BR /&gt;%mend check_records;&lt;BR /&gt;&lt;BR /&gt;%check_records;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(untested)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 May 2016 11:56:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Facing-Problem-while-using-format-in-a-data-step/m-p/270506#M53760</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2016-05-14T11:56:48Z</dc:date>
    </item>
    <item>
      <title>Re: Facing Problem while using format in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Facing-Problem-while-using-format-in-a-data-step/m-p/270515#M53764</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/40271"&gt;@GunnerEP﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The issue is: Macro variable REQD_FLDS presumably contains a list of names of character variables, including &lt;FONT face="courier new,courier"&gt;name&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;address&lt;/FONT&gt;. When you check the lengths of the variable values (e.g. 'Miller' [length 6] and '123 Main St' [length 11]) with&amp;nbsp;&lt;SPAN&gt;&lt;FONT face="courier new,courier"&gt;length(&amp;amp;&amp;amp;reqdFld&amp;amp;i)&lt;/FONT&gt;, this is fine. But when you want to apply your formats to the&amp;nbsp;variable&lt;EM&gt; names&lt;/EM&gt;, you must put the macro variable reference &lt;FONT face="courier new,courier"&gt;&amp;amp;&amp;amp;reqdFld&amp;amp;i&lt;/FONT&gt; between double quotes. Otherwise, you apply the formats to the&amp;nbsp;variable&lt;EM&gt; values&lt;/EM&gt;, which makes no sense.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Therefore, please try:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;inlgth = input(put("&amp;amp;&amp;amp;reqdFld&amp;amp;i.",$lgth.),3.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;and&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;err_cd = put("&amp;amp;&amp;amp;reqdFld&amp;amp;i.",$err.);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 May 2016 14:51:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Facing-Problem-while-using-format-in-a-data-step/m-p/270515#M53764</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-05-14T14:51:16Z</dc:date>
    </item>
    <item>
      <title>Re: Facing Problem while using format in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Facing-Problem-while-using-format-in-a-data-step/m-p/270516#M53765</link>
      <description>&lt;P&gt;Do you want to apply the format to the value of the variable named by the macro variable? Or to the NAME of the variable, that is the value of the macro variable. &amp;nbsp;Also there is no need to constantly call the PUT() function for every observation. &amp;nbsp;Just call it when the macro is running and generate the value as a literal for the DATA step to use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro check(varlist);
%local x field;

data input_data_2_output error_file_output;
   set input_data_orig;
 
%do x = 1 %to %sysfunc(countw(&amp;amp;varlist,:));
  %let field = %scan(&amp;amp;varlist, &amp;amp;x, :);

if length(&amp;amp;field) &amp;gt; %sysfunc(putc(&amp;amp;field,$lgth)) then do;
  err_cd = "%sysfunc(putc(&amp;amp;field,$err))";
  err_cnt = 1;
  rec_num = _n_;
  record = 'BAD RECORD';
  output error_file_output;
end;

%end;

run;
%mend check_records;

%let &amp;amp;reqd_flds=name:address ;
%check_records(&amp;amp;reqd_flds);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 May 2016 15:07:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Facing-Problem-while-using-format-in-a-data-step/m-p/270516#M53765</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-05-14T15:07:54Z</dc:date>
    </item>
    <item>
      <title>Re: Facing Problem while using format in a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Facing-Problem-while-using-format-in-a-data-step/m-p/270877#M53873</link>
      <description>&lt;P&gt;Thanks for your inputs mate. It worked.&lt;/P&gt;</description>
      <pubDate>Tue, 17 May 2016 06:09:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Facing-Problem-while-using-format-in-a-data-step/m-p/270877#M53873</guid>
      <dc:creator>GunnerEP</dc:creator>
      <dc:date>2016-05-17T06:09:31Z</dc:date>
    </item>
  </channel>
</rss>

