<?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: Loop user-defined formats in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680515#M19520</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;has the right idea here.&amp;nbsp; Because you have a larger problem than you can post, here's a link to more information that shows a few variations on this technique.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/sastraining/2015/01/30/sas-authors-tip-getting-the-macro-language-to-perform-a-do-loop-over-a-list-of-values/" target="_blank"&gt;https://blogs.sas.com/content/sastraining/2015/01/30/sas-authors-tip-getting-the-macro-language-to-perform-a-do-loop-over-a-list-of-values/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 31 Aug 2020 19:08:05 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2020-08-31T19:08:05Z</dc:date>
    <item>
      <title>Loop user-defined formats</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680496#M19514</link>
      <description>&lt;P&gt;I want to loop through user-defined formats like below:&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Formats:&lt;/P&gt;&lt;PRE&gt;proc format;
value cutstest_fmt
0 = '0'
1 - 199 = '001 to 199'
200 - 998 = '200 to 998'
999 = '999 to 999'
;

value cuts1_fmt
0 = '0'
1 - 349 = '001 to 349'
350 - 998 = '350 to 998'
999 = '999 to 999'
;
run;&lt;/PRE&gt;&lt;P&gt;Loop:&lt;/P&gt;&lt;PRE&gt;DO i=1 to formats._NAME_;
proc freq data = dt;
	tables col1* col2/MISSING norow nocol nopercent;
	format col1 i.;
run;
end;&lt;/PRE&gt;&lt;P&gt;I got errors. I think I miss something here. Any help?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Explain more here:&lt;/P&gt;&lt;P&gt;The above codes are incorrect. My question is how can I loop through user-defined formats value names.&lt;/P&gt;&lt;P&gt;I just take proc freq here for example that I want to loop formats in this proc freq instead of copy/paste proc freq multiple times and change the formats names.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Aug 2020 18:18:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680496#M19514</guid>
      <dc:creator>PeterChen</dc:creator>
      <dc:date>2020-08-31T18:18:56Z</dc:date>
    </item>
    <item>
      <title>Re: Loop user-defined formats</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680500#M19515</link>
      <description>That's not how you loop a proc in SAS. What are you trying to do overall? You likely need macros if you want a loop but if it's only two that's not what I would recommend. There are also Multilevel formats (MLF) which may be more appropriate here depending on what you're trying to accomplish.</description>
      <pubDate>Mon, 31 Aug 2020 17:59:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680500#M19515</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-08-31T17:59:56Z</dc:date>
    </item>
    <item>
      <title>Re: Loop user-defined formats</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680502#M19516</link>
      <description>Actually this loop is in a large macro.&lt;BR /&gt;And the formats will also be used in another place. I just use proc freq here for asking question about how to loop through multiple different formats on variable col1.</description>
      <pubDate>Mon, 31 Aug 2020 18:03:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680502#M19516</guid>
      <dc:creator>PeterChen</dc:creator>
      <dc:date>2020-08-31T18:03:27Z</dc:date>
    </item>
    <item>
      <title>Re: Loop user-defined formats</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680503#M19517</link>
      <description>&lt;P&gt;Copy&amp;nbsp; the LOG and paste into a code box opened on the forum with the &amp;lt;/&amp;gt; icon.&lt;/P&gt;
&lt;P&gt;That way we can organize a response in order to your errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It appears that you may have been attempting to use the macro language constructs but without any macro coding.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What value would you expect "formats._name_" to have in the following code and where would the value come from?&lt;/P&gt;
&lt;PRE&gt;DO i=1 to formats._NAME_; &lt;/PRE&gt;
&lt;P&gt;Since "i" would be numeric&amp;nbsp; then you would be using a format 1. in the first iteration of&lt;/P&gt;
&lt;PRE&gt;format col1 i.;&lt;/PRE&gt;
&lt;P&gt;if the "i" resolved at all, which it can't currently. &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>Mon, 31 Aug 2020 18:05:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680503#M19517</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-31T18:05:00Z</dc:date>
    </item>
    <item>
      <title>Re: Loop user-defined formats</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680504#M19518</link>
      <description>My code is wrong. I just want to explain the question I face...&lt;BR /&gt;There is no formats._NAME_. I just want to loop the formats value name in the proc freq once instead of typing/coping multiple times and change the formats name.</description>
      <pubDate>Mon, 31 Aug 2020 18:08:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680504#M19518</guid>
      <dc:creator>PeterChen</dc:creator>
      <dc:date>2020-08-31T18:08:46Z</dc:date>
    </item>
    <item>
      <title>Re: Loop user-defined formats</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680510#M19519</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/319830"&gt;@PeterChen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;My code is wrong. I just want to explain the question I face...&lt;BR /&gt;There is no formats._NAME_. I just want to loop the formats value name in the proc freq once instead of typing/coping multiple times and change the formats name.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;To loop over a list of values place the list into a macro variable and use %SCAN(). You can use %SYSFUNC(COUNTW()) to find out how many items are in the list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let fmt_list=fmta. fmtb. ;
%do i=1 %to %sysfunc(countw(&amp;amp;fmt_list,%str( )));
  %let fmt=%scan(&amp;amp;fmt_list,&amp;amp;i,%str( ));
   .... format x &amp;amp;fmt. ...
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 31 Aug 2020 18:48:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680510#M19519</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-31T18:48:16Z</dc:date>
    </item>
    <item>
      <title>Re: Loop user-defined formats</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680515#M19520</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;has the right idea here.&amp;nbsp; Because you have a larger problem than you can post, here's a link to more information that shows a few variations on this technique.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/sastraining/2015/01/30/sas-authors-tip-getting-the-macro-language-to-perform-a-do-loop-over-a-list-of-values/" target="_blank"&gt;https://blogs.sas.com/content/sastraining/2015/01/30/sas-authors-tip-getting-the-macro-language-to-perform-a-do-loop-over-a-list-of-values/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Aug 2020 19:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680515#M19520</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-08-31T19:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: Loop user-defined formats</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680516#M19521</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/319830"&gt;@PeterChen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;My code is wrong. I just want to explain the question I face...&lt;BR /&gt;There is no formats._NAME_. I just want to loop the formats value name in the proc freq once instead of typing/coping multiple times and change the formats name.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;To loop over a list of values place the list into a macro variable and use %SCAN(). You can use %SYSFUNC(COUNTW()) to find out how many items are in the list.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let fmt_list=fmta. fmtb. ;
%do i=1 %to %sysfunc(countw(&amp;amp;fmt_list,%str( )));
  %let fmt=%scan(&amp;amp;fmt_list,&amp;amp;i,%str( ));
   .... format x &amp;amp;fmt. ...
%end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;PRE&gt;proc catalog catalog=formats;&lt;BR /&gt;contents out=fmtcontents(keep=name);
quit;

proc sql noprint;
select name into :fmtnames
from fmtcontents;
quit;

%let fmt_list=&amp;amp;fmtnames. ;&lt;BR /&gt;%do i=1 %to %sysfunc(countw(&amp;amp;fmt_list,%str( )));&lt;BR /&gt;  %let fmt=%scan(&amp;amp;fmt_list,&amp;amp;i,%str( ));
  proc freq data = data;
    tables col1 * col2/MISSING norow nocol nopercent;
    format col1 &amp;amp;fmt.;
  run;
%end;&lt;/PRE&gt;&lt;P&gt;I put the unique format names into macro variable fmtnames but got incorrect result:&lt;/P&gt;&lt;P&gt;The col1 did not format...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Aug 2020 19:17:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680516#M19521</guid>
      <dc:creator>PeterChen</dc:creator>
      <dc:date>2020-08-31T19:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: Loop user-defined formats</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680518#M19522</link>
      <description>&lt;P&gt;Make sure to test every step of your code. The following step will only return a single format, not the list because you don't have a separated by. If you check how that variable resolves you'll see that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select name into :fmtnames
from fmtcontents;
quit;&lt;BR /&gt;&lt;BR /&gt;%put&amp;nbsp;&amp;amp;fmtnames;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Try this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select name into :fmtnames separated by " "
from fmtcontents;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 31 Aug 2020 19:21:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680518#M19522</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-08-31T19:21:46Z</dc:date>
    </item>
    <item>
      <title>Re: Loop user-defined formats</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680519#M19523</link>
      <description>&lt;P&gt;Make sure to generate valid code.&amp;nbsp; A format specification needs a period.&amp;nbsp; Like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format varname fmtname. ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A statement like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format varname fmtname ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Will remove any attached formats from the &lt;STRONG&gt;two&lt;/STRONG&gt; variable names listed.&amp;nbsp; If the variable fmtname does not exist you will get a note/warning/error in the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Aug 2020 19:25:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680519#M19523</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-31T19:25:26Z</dc:date>
    </item>
    <item>
      <title>Re: Loop user-defined formats</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680522#M19524</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/319830"&gt;@PeterChen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;My code is wrong. I just want to explain the question I face...&lt;BR /&gt;There is no formats._NAME_. I just want to loop the formats value name in the proc freq once instead of typing/coping multiple times and change the formats name.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;To loop over a list of values place the list into a macro variable and use %SCAN(). You can use %SYSFUNC(COUNTW()) to find out how many items are in the list.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let fmt_list=fmta. fmtb. ;
%do i=1 %to %sysfunc(countw(&amp;amp;fmt_list,%str( )));
  %let fmt=%scan(&amp;amp;fmt_list,&amp;amp;i,%str( ));
   .... format x &amp;amp;fmt. ...
%end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;This is correct answer.&amp;nbsp;&lt;BR /&gt;I tested it and only one thing to mention: remember to add one more "." after "&amp;amp;fmt."&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%macro test;
%do i=1 %to %sysfunc(countw(&amp;amp;fmtnames.,%str( )));
   %let fmt = %scan(&amp;amp;fmtnames., &amp;amp;i, %str( ));
  proc freq data = dt;
	tables col1 * col2/MISSING norow nocol nopercent;
	format col1 &amp;amp;fmt..;
  run;
%end;
%mend test;
%test;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Aug 2020 19:29:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680522#M19524</guid>
      <dc:creator>PeterChen</dc:creator>
      <dc:date>2020-08-31T19:29:14Z</dc:date>
    </item>
    <item>
      <title>Re: Loop user-defined formats</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680525#M19525</link>
      <description>&lt;P&gt;If the list already has the periods in the format names then your change will generate invalid code. So if you want to loop over:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let fmt_list = best12.3;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;you will end up generating code like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format xxx best12.3. ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to make the loop smart enough to handle lists that might just have the format name instead of a full format specification then have it check whether the word in the list has the period or not.&amp;nbsp; Then it will be more forgiving of lists that don't quite have the right types of values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=1 %to %sysfunc(countw(&amp;amp;fmt_list,%str( )));
  %let fmt=%scan(&amp;amp;fmt_list,&amp;amp;i,%str( ));
  %if not %index(&amp;amp;fmt,.) %then %let fmt=&amp;amp;fmt..;
   
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Aug 2020 19:40:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680525#M19525</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-31T19:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: Loop user-defined formats</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680571#M19526</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/319830"&gt;@PeterChen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS has a really nifty feature called multilabel formats. If it's the kind of thing that can help solve your problem, it could be REALLY useful.&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
	value cutstest_all_fmt (multilabel)
		0 = '0'
		1 - 199 = '001 to 199'
		1 - 349 = '001 to 349'
		200 - 998 = '200 to 998'
		350 - 998 = '350 to 998'
		999 = '999 to 999'
	;

data have;
	input col1 col2;
	cards;
998 160
0 473
998 473
0 160
350 473
200 110
200 38
998 38
350 110
200 110
0 39
998 39
350 160
0 175
1 308
200 473
1 160
1 1
0 473
349 308
199 39
350 1
199 308
0 38
999 38
998 39
349 308
200 39
998 473
998 160
1 473
1 1
run;

proc summary data=have nway noprint;
	class col1 / mlf;
	class col2;
	output out=work.results;
	format col1 cutstest_all_fmt.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 31 Aug 2020 22:12:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Loop-user-defined-formats/m-p/680571#M19526</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2020-08-31T22:12:36Z</dc:date>
    </item>
  </channel>
</rss>

