<?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: %str  fucntion in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/str-fucntion/m-p/460930#M117190</link>
    <description>&lt;P&gt;Hi Ronein.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In your example are a few issues, please see&amp;nbsp;my corrected&amp;nbsp; version below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. you need to use %NRSTR (because of the ; in your code)&lt;/P&gt;&lt;P&gt;2. a missing )&lt;/P&gt;&lt;P&gt;3. always use an explicit length statement, otherwise sas will&amp;nbsp;use the length from the FIRST assignment in the data stept (after resoving the macro variables)&lt;/P&gt;&lt;P&gt;groupCat1 ='&amp;lt;20' &amp;lt;- (as expression length is 3)&lt;/P&gt;&lt;P&gt;groupCat2='&amp;lt;35' &amp;lt;- (as expression length is 3)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;updated&amp;nbsp;example&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let age4=%nrstr(if age&amp;lt;20 then groupCat1 ='&amp;lt;20';&lt;BR /&gt;else if age&amp;lt;35 then groupCat1='20-34';&lt;BR /&gt;else if age&amp;lt;45 then groupCat1='35-44';&lt;BR /&gt;else groupCat1='45+';&lt;BR /&gt;);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let age2=%nrstr(if age&amp;lt;35 then groupCat2='&amp;lt;35';&lt;BR /&gt;else groupCat2='35+';&lt;BR /&gt;);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data test;&lt;BR /&gt;length groupcat1 groupcat2 $20;&lt;BR /&gt;input age @@;&lt;BR /&gt;&amp;amp;age4.;&lt;BR /&gt;&amp;amp;age2.;&lt;BR /&gt;datalines;&lt;BR /&gt;19 50 10 34 44 15 30&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I recommend to use %macro /%mend&amp;nbsp;instead of macro variables&amp;nbsp;having sas code.&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 09 May 2018 07:28:07 GMT</pubDate>
    <dc:creator>Guardian</dc:creator>
    <dc:date>2018-05-09T07:28:07Z</dc:date>
    <item>
      <title>%str  fucntion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/str-fucntion/m-p/460920#M117186</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I am trying to learn using str function.&lt;/P&gt;&lt;P&gt;Please see example below.&lt;/P&gt;&lt;P&gt;In data set&amp;nbsp;&lt;SPAN&gt;test &amp;nbsp;the length of varaible &amp;nbsp;groupCat1&amp;nbsp;groupCat2 is too short.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;How can I add length statement to following code?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data test;&lt;BR /&gt;input age @@;&lt;BR /&gt;&amp;amp;age4.;&lt;BR /&gt;&amp;amp;age2.;&lt;BR /&gt;datalines;&lt;BR /&gt;19 50 10 34 44 15 30&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/SPAN&gt;&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;&lt;P&gt;&lt;SPAN&gt;Here is the full code:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let age4=%str(if age&amp;lt;20 then groupCat1 ='&amp;lt;20';&lt;BR /&gt;else if age&amp;lt;35 then groupCat1='20-34';&lt;BR /&gt;else if age&amp;lt;45 then groupCat1='35-44';&lt;BR /&gt;else groupCat1='45+';&lt;BR /&gt;);&lt;/P&gt;&lt;P&gt;%let age2=%str(if age&amp;lt;35 then groupCat2='&amp;lt;35';&lt;BR /&gt;else groupCat2='35+';);&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data test;&lt;BR /&gt;input age @@;&lt;BR /&gt;&amp;amp;age4.;&lt;BR /&gt;&amp;amp;age2.;&lt;BR /&gt;datalines;&lt;BR /&gt;19 50 10 34 44 15 30&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 09 May 2018 06:06:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/str-fucntion/m-p/460920#M117186</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-05-09T06:06:46Z</dc:date>
    </item>
    <item>
      <title>Re: %str  fucntion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/str-fucntion/m-p/460928#M117189</link>
      <description>&lt;P&gt;I can understand your wanting to learn a function, but step 1 of any learning activity should be to learn how to do things as simply and efficiently as possible, and I am afraid that if you are resorting to %str or one of those types of macro functions, or multiple &amp;amp; signs, then that code is neither simple nor efficient.&amp;nbsp; For instance, in this example you give, the simplest method would be to create a format, then:&lt;/P&gt;
&lt;PRE&gt;proc format;
  value agrpfmt
    0-&amp;lt;20="&amp;lt;20"
    20-&amp;lt;35="20-34"
    ...;
run;

data want;
  set have;
  groupcat1=put(age,grpfmt.);
run;&lt;/PRE&gt;
&lt;P&gt;So much simpler.&amp;nbsp; The format statement can be managed externally to the program so as to provide expandability and managability without re-validating the other code.&amp;nbsp; Much easier to read the code, and support the code.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;to note, Base SAS is the programming language, it can do everything.&amp;nbsp; Macro is a helper component which is there to build text strings, nothing more.&amp;nbsp; Do not use it as an alternative to Base SAS.&lt;/P&gt;</description>
      <pubDate>Wed, 09 May 2018 07:12:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/str-fucntion/m-p/460928#M117189</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-05-09T07:12:31Z</dc:date>
    </item>
    <item>
      <title>Re: %str  fucntion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/str-fucntion/m-p/460930#M117190</link>
      <description>&lt;P&gt;Hi Ronein.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In your example are a few issues, please see&amp;nbsp;my corrected&amp;nbsp; version below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. you need to use %NRSTR (because of the ; in your code)&lt;/P&gt;&lt;P&gt;2. a missing )&lt;/P&gt;&lt;P&gt;3. always use an explicit length statement, otherwise sas will&amp;nbsp;use the length from the FIRST assignment in the data stept (after resoving the macro variables)&lt;/P&gt;&lt;P&gt;groupCat1 ='&amp;lt;20' &amp;lt;- (as expression length is 3)&lt;/P&gt;&lt;P&gt;groupCat2='&amp;lt;35' &amp;lt;- (as expression length is 3)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;updated&amp;nbsp;example&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let age4=%nrstr(if age&amp;lt;20 then groupCat1 ='&amp;lt;20';&lt;BR /&gt;else if age&amp;lt;35 then groupCat1='20-34';&lt;BR /&gt;else if age&amp;lt;45 then groupCat1='35-44';&lt;BR /&gt;else groupCat1='45+';&lt;BR /&gt;);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let age2=%nrstr(if age&amp;lt;35 then groupCat2='&amp;lt;35';&lt;BR /&gt;else groupCat2='35+';&lt;BR /&gt;);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data test;&lt;BR /&gt;length groupcat1 groupcat2 $20;&lt;BR /&gt;input age @@;&lt;BR /&gt;&amp;amp;age4.;&lt;BR /&gt;&amp;amp;age2.;&lt;BR /&gt;datalines;&lt;BR /&gt;19 50 10 34 44 15 30&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I recommend to use %macro /%mend&amp;nbsp;instead of macro variables&amp;nbsp;having sas code.&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 May 2018 07:28:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/str-fucntion/m-p/460930#M117190</guid>
      <dc:creator>Guardian</dc:creator>
      <dc:date>2018-05-09T07:28:07Z</dc:date>
    </item>
    <item>
      <title>Re: %str  fucntion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/str-fucntion/m-p/460949#M117196</link>
      <description>&lt;P&gt;You could easily add it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;%let age4=%str(length groupCat1 $ 5; if age&amp;lt;20 then groupCat1 ='&amp;lt;20';&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;else if age&amp;lt;35 then groupCat1='20-34';&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;else if age&amp;lt;45 then groupCat1='35-44';&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;else groupCat1='45+';&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;As you mentioned, this is a practice exercise.&amp;nbsp; It would be rare for %str to be used this way in practice just as a matter of style.&amp;nbsp; But it is perfectly valid for learning purposes.&amp;nbsp; If this logic were re-usable code that would apply to many DATA steps, you might actually create a macro instead of a macro variable:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;%macro age4;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;length groupCat1 $ 5;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if age&amp;lt;20 then groupCat1 ='&amp;lt;20';&lt;BR /&gt;else if age&amp;lt;35 then groupCat1='20-34';&lt;BR /&gt;else if age&amp;lt;45 then groupCat1='35-44';&lt;BR /&gt;else groupCat1='45+';&lt;BR /&gt;%mend age4;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It's re-usable, since macros can be stored permanently.&amp;nbsp; It's easier to read.&amp;nbsp; And calling the macro is just as easy as using a macro variable:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;%age4&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 09 May 2018 10:59:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/str-fucntion/m-p/460949#M117196</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-05-09T10:59:23Z</dc:date>
    </item>
  </channel>
</rss>

