<?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: Handle variables with spaces in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/510084#M137237</link>
    <description>Have a look APPROX FORMAT.&lt;BR /&gt;It allows you to provide a description for multiple  individual codes and ranges. With this kind of user defined format all / most procedures combine values with  codes which have the same formatted description</description>
    <pubDate>Fri, 02 Nov 2018 21:41:57 GMT</pubDate>
    <dc:creator>Peter_C</dc:creator>
    <dc:date>2018-11-02T21:41:57Z</dc:date>
    <item>
      <title>Handle variables with spaces</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/508588#M136606</link>
      <description>&lt;P&gt;I've a data set with a long list of numerical variables with spaces (all starting with numerical 7). I need to sum up some of the variables into 5-6 new variables. Here is a sample data with just 4 variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; &lt;BR /&gt;  input FacNo 1 '71190 food'n 3-5  '71230 adm act'n 7-9 '71340 occ therapy'n 11-13 '71350 physio therapy'n 15-16;
datalines;
1 100     600  
2 700 400 400  
3 300 200 
4 750         40
5 350 150	    
6 500     400 50
7 250         70
;
proc print noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is what I'd like to have&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 336pt;" border="0" width="448" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 38.25pt;"&gt;
&lt;TD width="64" height="51" class="xl70" style="height: 38.25pt; width: 48pt;"&gt;FacNo&lt;/TD&gt;
&lt;TD width="64" class="xl69" style="border-left: none; width: 48pt;"&gt;71190 food&lt;/TD&gt;
&lt;TD width="64" class="xl68" style="width: 48pt;"&gt;71230 adm act&lt;/TD&gt;
&lt;TD width="64" class="xl68" style="width: 48pt;"&gt;71340 occ therapy&lt;/TD&gt;
&lt;TD width="64" class="xl68" style="width: 48pt;"&gt;71350 physio therapy&lt;/TD&gt;
&lt;TD width="64" class="xl65" style="width: 48pt;"&gt;Adm_cost&lt;/TD&gt;
&lt;TD width="64" class="xl65" style="width: 48pt;"&gt;Physio_cost&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;1&lt;/TD&gt;
&lt;TD class="xl67"&gt;100&lt;/TD&gt;
&lt;TD class="xl67"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl67"&gt;600&lt;/TD&gt;
&lt;TD class="xl67"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl67"&gt;100&lt;/TD&gt;
&lt;TD align="right"&gt;600&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;2&lt;/TD&gt;
&lt;TD class="xl67"&gt;700&lt;/TD&gt;
&lt;TD class="xl67"&gt;400&lt;/TD&gt;
&lt;TD class="xl67"&gt;400&lt;/TD&gt;
&lt;TD class="xl67"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl67"&gt;1100&lt;/TD&gt;
&lt;TD align="right"&gt;400&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;3&lt;/TD&gt;
&lt;TD class="xl67"&gt;300&lt;/TD&gt;
&lt;TD class="xl67"&gt;200&lt;/TD&gt;
&lt;TD class="xl67"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl67"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl67"&gt;500&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;4&lt;/TD&gt;
&lt;TD class="xl67"&gt;750&lt;/TD&gt;
&lt;TD class="xl67"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl67"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl67"&gt;40&lt;/TD&gt;
&lt;TD class="xl67"&gt;750&lt;/TD&gt;
&lt;TD align="right"&gt;40&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;5&lt;/TD&gt;
&lt;TD class="xl67"&gt;350&lt;/TD&gt;
&lt;TD class="xl67"&gt;150&lt;/TD&gt;
&lt;TD class="xl67"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl67"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl67"&gt;500&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;6&lt;/TD&gt;
&lt;TD class="xl67"&gt;500&lt;/TD&gt;
&lt;TD class="xl67"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl67"&gt;400&lt;/TD&gt;
&lt;TD class="xl67"&gt;50&lt;/TD&gt;
&lt;TD class="xl67"&gt;500&lt;/TD&gt;
&lt;TD align="right"&gt;450&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD height="21" class="xl66" style="height: 15.75pt;"&gt;7&lt;/TD&gt;
&lt;TD class="xl67"&gt;250&lt;/TD&gt;
&lt;TD class="xl67"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl67"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl67"&gt;70&lt;/TD&gt;
&lt;TD class="xl67"&gt;250&lt;/TD&gt;
&lt;TD align="right"&gt;70&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&lt;BR /&gt;I'd like to have a variable "Adm_cost" based on sum of variables with prefix 71190 and 71230, and Physio_cost based on variables with prefix 71340 and 71350. I know I can sum them up by just listing e.g. adm_cost=sum('&lt;SPAN&gt;71190 food'&lt;/SPAN&gt;n, '&lt;SPAN&gt;71230 adm act&lt;/SPAN&gt;' n), but&amp;nbsp;since the actual data I have&lt;/P&gt;
&lt;P&gt;- has a long list of variables with spaces (all starting with 7, having 5 numerical values, a space, then description),&lt;/P&gt;
&lt;P&gt;- I need to create 5 variables out of the list of variables&lt;/P&gt;
&lt;P&gt;So I'm hoping to find a better way to handle the code, using such as array, to make the code more concise, so that I can easily change how I want to group variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance. I really appreciate your help in this community.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 07:37:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/508588#M136606</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2018-10-30T07:37:45Z</dc:date>
    </item>
    <item>
      <title>Re: Handle variables with spaces</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/508592#M136607</link>
      <description>&lt;P&gt;Better way is to name the variables as V1 V2 V3 ..... Vn&lt;/P&gt;
&lt;P&gt;and define labels with the names you gave:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;  
    input   v1    1 
               v2    3-5
               v3    7-9
             ..........
     ;
LABEL
   v1 = "FacNo"
   v2 =  '71190 food'
   v3 =  '71230 adm act'
  .......................
 ;
datalines;
1 100     600  
2 700 400 400  
3 300 200 
4 750         40
5 350 150	    
6 500     400 50
7 250         70
;
proc print noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Oct 2018 07:52:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/508592#M136607</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-10-30T07:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: Handle variables with spaces</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/508601#M136613</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt; suggested, you may be better off with more standard variable names. But if that is possible, rename e.g. '71190 food' to something more similar, such as V791190_food - then you can sum those variable using&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;adm_cost=sum(of V71190:,of V71230:)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;That approach will not work with variable names that start with numbers.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you cannot (or will not) rename your variables, here's a possible solution:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array nums _numeric_;
  adm_cost=0;
  physio_cost=0;
  do _N_=1 to dim(nums);
    prefix=substr(vname(nums(_N_)),1,5);
	select(prefix);
	  when('71190','71230') adm_cost+nums(_N_);
	  when('71340','71350') physio_cost+nums(_N_);
	  otherwise;
	  end;
    end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 08:47:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/508601#M136613</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-10-30T08:47:57Z</dc:date>
    </item>
    <item>
      <title>Re: Handle variables with spaces</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/510082#M137236</link>
      <description>&lt;P&gt;Thanks for the replies. Yes,&amp;nbsp;I was left with no choice but to handle variable names with spaces. S_lassen's work perfectly. I only have one follow up questions That is what if my values are not a list of single values (A), but a range of values (B), a single value along with a range of values (C), or multiple ranges of values. How do I write the code. I have the following code written but the output values aren't right for B and C cases. Thanks in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do _N_=1 to dim(nums);
    prefix=substr(vname(nums(_N_)),1,5);
	select (prefix);
           when ('71205','71206','71207') IPadm+nums(_N_);   /*A*/&lt;BR /&gt;	   when ('71360','71362','71365','71367','71369') ACdaysurg+nums(_N_);  /*A*/
	   when ('71410' &amp;lt;=prefix&amp;lt;= '71434') Diagnostic_Clinic_vst+nums(_N_);   /*B*/
           when ('71435', '71440' &amp;lt; prefix&amp;lt;= '71499') Allied_health+nums(_N_);  /*C*/ &lt;BR /&gt;	   otherwise;
		end;
    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>Fri, 02 Nov 2018 21:32:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/510082#M137236</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2018-11-02T21:32:30Z</dc:date>
    </item>
    <item>
      <title>Re: Handle variables with spaces</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/510084#M137237</link>
      <description>Have a look APPROX FORMAT.&lt;BR /&gt;It allows you to provide a description for multiple  individual codes and ranges. With this kind of user defined format all / most procedures combine values with  codes which have the same formatted description</description>
      <pubDate>Fri, 02 Nov 2018 21:41:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/510084#M137237</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2018-11-02T21:41:57Z</dc:date>
    </item>
    <item>
      <title>Re: Handle variables with spaces</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/510086#M137238</link>
      <description>PROC FORMAT</description>
      <pubDate>Fri, 02 Nov 2018 21:42:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/510086#M137238</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2018-11-02T21:42:29Z</dc:date>
    </item>
    <item>
      <title>Re: Handle variables with spaces</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/510092#M137241</link>
      <description>&lt;P&gt;Yes I can use format, but I've a horizontal data and&amp;nbsp;I prefer to stay as is, not to output it into a&amp;nbsp;vertical data, apply format, and then transpose it back to a horizontal data, after summing. s_lassen's code works well, but I just need to know how to tweak the code to allow specifying 1) a range of values, and 2) a range of values along with multiple single values. Hope the explanation helps.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Nov 2018 22:11:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/510092#M137241</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2018-11-02T22:11:53Z</dc:date>
    </item>
    <item>
      <title>Re: Handle variables with spaces</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/510094#M137243</link>
      <description>You can use a user defined format in a put() function, instead of &lt;BR /&gt;&lt;BR /&gt;    prefix=substr(vname(nums(_N_)),1,5);&lt;BR /&gt;	select(prefix);&lt;BR /&gt;	  when('71190','71230') adm_cost+nums(_N_);&lt;BR /&gt;	  when('71340','71350') &lt;BR /&gt;&lt;BR /&gt;Use &lt;BR /&gt;        Group_ID = put( prefix, $your_fmt. ) ;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 02 Nov 2018 22:20:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Handle-variables-with-spaces/m-p/510094#M137243</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2018-11-02T22:20:30Z</dc:date>
    </item>
  </channel>
</rss>

