<?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: Changing the variable levels for a character variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752991#M237252</link>
    <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;!&lt;/P&gt;
&lt;P&gt;Using strip(var) instead of var solved my problem!&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, I tried the proc freq and the levels of the variable for frequencies turn out as the three levels I was expecting and I assume SAS recovers extra spacing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just need to introduce var_dv with the most number of characters first because that is how many levels reserved for var_dv so I do not get truncated on variable level values.&lt;/P&gt;
&lt;P&gt;Thank you both!&lt;/P&gt;</description>
    <pubDate>Thu, 08 Jul 2021 19:50:43 GMT</pubDate>
    <dc:creator>Emma_at_SAS</dc:creator>
    <dc:date>2021-07-08T19:50:43Z</dc:date>
    <item>
      <title>Changing the variable levels for a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752943#M237225</link>
      <description>&lt;P&gt;I have a character variable with 3 levels&lt;/P&gt;
&lt;P&gt;1/4 lb&lt;/P&gt;
&lt;P&gt;1/2 lb&lt;/P&gt;
&lt;P&gt;3/4 lb&lt;/P&gt;
&lt;P&gt;and I want to change the levels to show them with decimilas&lt;/P&gt;
&lt;P&gt;0.25 lb&lt;/P&gt;
&lt;P&gt;0.50 lb&lt;/P&gt;
&lt;P&gt;0.75 lb&lt;/P&gt;
&lt;P&gt;I tried&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if var= "1/4 lb" then new_var="0.25 lb";
if var= "1/2 lb" then new_var="0.50 lb";
if var= "3/4 lb" then new_var="0.75 lb";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but my new_var is all missing observations. I wonder if that is because I have a space between the numbers and lb. I wonder where I am making a mistake and thank you for your suggestions.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 17:32:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752943#M237225</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-07-08T17:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: Changing the variable levels for a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752956#M237231</link>
      <description>&lt;P&gt;Character variables when using = are compared first character to first character, 2nd to 2nd and so on. As soon as one does not match SAS says "not equal". So if your variable starts with a space in the data then it is not equal. If any of the other characters are not exactly the same then they are not equal. If you variable does not have a space and you compare it to "1/4 lb" with a space, then it is not equal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sometimes what you see misses leading spaces because they are suppressed.&lt;/P&gt;
&lt;P&gt;You can try code like this to see some values;&lt;/P&gt;
&lt;PRE&gt;data _null_;
    set yourdataset obs=10;
    newvar=quote(var);
   put newvar=;
run;&lt;/PRE&gt;
&lt;P&gt;Data _null_ does not create a data set. This just writes some information to the log for the first 10 records (the Obs=10) so you can see some examples of your data. The value of newvar will have quotes around your current Var values.&lt;/P&gt;
&lt;P&gt;If you see leading blanks before the text such as possibly "&amp;nbsp;&amp;nbsp; 1/4 lb" you can modify your comparison to use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;if strip(var)= "1/4 lb" then new_var="0.25 lb";&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if your values do not have the space, or more spaces between 1/4 and lb then you need to make your comparison string match what is actually in the data for Var.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another potential problem since you did not share an entire data step is if you have used New_var somewhere before this in the code that would make SAS think the variable could be numeric as you can't assign ".25 lb" to a numeric variable. This would likely generate a warning in the log one way or another.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 20:21:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752956#M237231</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-08T20:21:17Z</dc:date>
    </item>
    <item>
      <title>Re: Changing the variable levels for a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752970#M237238</link>
      <description>Quick trick, run a proc freq on the VAR column. Copy the values from the output into your VAR="value copied from proc freq". That helps you avoid these issues. &lt;BR /&gt;&lt;BR /&gt;Note that it's also case sensitive - so if it's Lb or LB or lbs that won't match either.</description>
      <pubDate>Thu, 08 Jul 2021 18:29:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752970#M237238</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-07-08T18:29:18Z</dc:date>
    </item>
    <item>
      <title>Re: Changing the variable levels for a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752985#M237248</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Quick trick, run a proc freq on the VAR column. Copy the values from the output into your VAR="value copied from proc freq". That helps you avoid these issues. &lt;BR /&gt;&lt;BR /&gt;Note that it's also case sensitive - so if it's Lb or LB or lbs that won't match either.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Unless the issue is leading blanks. The output for Proc Freq will be left justified and hide the leading spaces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data source that plays all sorts of stupidity on their data and had a range type value of "16000-30000" show up with two rows for each range in my proc freq and found values with leading spaces depending on which sub-agency collected the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 19:31:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752985#M237248</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-08T19:31:25Z</dc:date>
    </item>
    <item>
      <title>Re: Changing the variable levels for a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752990#M237251</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84351"&gt;@Emma_at_SAS&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;One approach is to use proc format and it should serve your purpose&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value $val
 "1/4lb" =0.250
 "1/2lb"=0.500
 "3/4lb"=0.750;
 run;
data test;
format var $val.;
input var1 $ var2 $;
var=cats(var1,var2);
drop var1 var2;
datalines;
1/4 lb
1/2 lb
3/4 lb
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The values will be appear as shown below. The original values remain intact.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_0-1625773790102.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/61080iD1C0FB4A725D30EE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Sajid01_0-1625773790102.png" alt="Sajid01_0-1625773790102.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 19:49:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752990#M237251</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-07-08T19:49:58Z</dc:date>
    </item>
    <item>
      <title>Re: Changing the variable levels for a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752991#M237252</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;!&lt;/P&gt;
&lt;P&gt;Using strip(var) instead of var solved my problem!&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, I tried the proc freq and the levels of the variable for frequencies turn out as the three levels I was expecting and I assume SAS recovers extra spacing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just need to introduce var_dv with the most number of characters first because that is how many levels reserved for var_dv so I do not get truncated on variable level values.&lt;/P&gt;
&lt;P&gt;Thank you both!&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 19:50:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752991#M237252</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-07-08T19:50:43Z</dc:date>
    </item>
    <item>
      <title>Re: Changing the variable levels for a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752994#M237254</link>
      <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/131732"&gt;@Sajid01&lt;/a&gt;. It is like another approach to solve my problem. Thanks</description>
      <pubDate>Thu, 08 Jul 2021 19:55:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/752994#M237254</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-07-08T19:55:48Z</dc:date>
    </item>
    <item>
      <title>Re: Changing the variable levels for a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/753003#M237258</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84351"&gt;@Emma_at_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;!&lt;/P&gt;
&lt;P&gt;Using strip(var) instead of var solved my problem!&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, I tried the proc freq and the levels of the variable for frequencies turn out as the three levels I was expecting and I assume SAS recovers extra spacing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just need to introduce var_dv with the most number of characters first because that is how many levels reserved for var_dv so I do not get truncated on variable level values.&lt;/P&gt;
&lt;P&gt;Thank you both!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Instead of worrying about order of assignment use a LENGTH statement to specify how many characters you expect a variable to hold before using it the first time. Its very simple:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Length var_dv $ 10;&lt;/P&gt;
&lt;P&gt;for example the $ says it will be a character variable and the 10 is how many characters you expect to use. If you need to later modify the program with new values later you check to see if that is long enough to hold the new values and increase as needed.&lt;/P&gt;
&lt;P&gt;Caution: same named variables with different lengths will cause warnings and possible truncation of values when combining data sets.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 20:25:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/753003#M237258</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-08T20:25:32Z</dc:date>
    </item>
    <item>
      <title>Re: Changing the variable levels for a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/753004#M237259</link>
      <description>Great. Thank you, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;!</description>
      <pubDate>Thu, 08 Jul 2021 20:28:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/753004#M237259</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-07-08T20:28:11Z</dc:date>
    </item>
    <item>
      <title>Re: Changing the variable levels for a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/753129#M237332</link>
      <description>&lt;PRE&gt;data have;
input have $20.;
want=catx(' ',put(input(resolve(cats('%sysevalf(',scan(have,1,' '),')')),best.),8.2 -l),scan(have,-1,' '));
cards;
1/4 lb
1/2 lb
3/4 lb
;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Jul 2021 12:52:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/753129#M237332</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-07-09T12:52:43Z</dc:date>
    </item>
    <item>
      <title>Re: Changing the variable levels for a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/753136#M237334</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&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/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Quick trick, run a proc freq on the VAR column. Copy the values from the output into your VAR="value copied from proc freq". That helps you avoid these issues. &lt;BR /&gt;&lt;BR /&gt;Note that it's also case sensitive - so if it's Lb or LB or lbs that won't match either.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Unless the issue is leading blanks. The output for Proc Freq will be left justified and hide the leading spaces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data source that plays all sorts of stupidity on their data and had a range type value of "16000-30000" show up with two rows for each range in my proc freq and found values with leading spaces depending on which sub-agency collected the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The plain old text output of PROC FREQ does not suppress the leading spaces.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;                                   Cumulative    Cumulative
x         Frequency     Percent     Frequency      Percent
-----------------------------------------------------------
  1              1       25.00             1        25.00
  aaaa           1       25.00             2        50.00
1                1       25.00             3        75.00
aaaa             1       25.00             4       100.00
&lt;/PRE&gt;
&lt;P&gt;It is just the "pretty" output in ODS that is confused.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 463px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/61100i3F6B0345B5F961ED/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jul 2021 13:23:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/753136#M237334</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-07-09T13:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: Changing the variable levels for a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/753182#M237355</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;for your discussion on spaces and leading spaces! very helpful&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jul 2021 14:57:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-the-variable-levels-for-a-character-variable/m-p/753182#M237355</guid>
      <dc:creator>Emma_at_SAS</dc:creator>
      <dc:date>2021-07-09T14:57:28Z</dc:date>
    </item>
  </channel>
</rss>

