<?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: Transpose Observations to Columns and add Flag in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Observations-to-Columns-and-add-Flag/m-p/962777#M375200</link>
    <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Location $;
datalines;
101 UK     
101 France 
101 Other  
102 UK     
102 Spain  
103 Other  
104 UK     
104 Other  
105 Other  
;

proc transpose data = have out = temp(drop = _:);
    by ID;
    id Location;
    var Location;
run;


data want;
   set temp;
   array a _character_;
   count = 0;
   do over a;
      a = ifc(a ne '', 'Y', a);
      if a = 'Y' then count + 1;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 27 Mar 2025 11:08:10 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2025-03-27T11:08:10Z</dc:date>
    <item>
      <title>Transpose Observations to Columns and add Flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Observations-to-Columns-and-add-Flag/m-p/962770#M375199</link>
      <description>&lt;P&gt;Dear Community,&lt;/P&gt;
&lt;P&gt;I have the following dataset. I would like to have my output dataset to be such a way that the unique observations(location) should become my columns and require a count column to suggest the no of values.&amp;nbsp; Appreciate your inputs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 96pt;" border="0" width="128" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL style="width: 48pt;" span="2" width="64" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" class="xl64" style="height: 15.0pt; width: 48pt;"&gt;ID&lt;/TD&gt;
&lt;TD width="64" class="xl64" style="border-left: none; width: 48pt;"&gt;Location&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl63" style="height: 15.0pt; border-top: none;"&gt;101&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;UK&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl63" style="height: 15.0pt; border-top: none;"&gt;101&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;France&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl63" style="height: 15.0pt; border-top: none;"&gt;101&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;Other&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl63" style="height: 15.0pt; border-top: none;"&gt;102&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;UK&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl63" style="height: 15.0pt; border-top: none;"&gt;102&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;Spain&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl63" style="height: 15.0pt; border-top: none;"&gt;103&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;Other&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl63" style="height: 15.0pt; border-top: none;"&gt;104&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;UK&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl63" style="height: 15.0pt; border-top: none;"&gt;104&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;Other&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl63" style="height: 15.0pt; border-top: none;"&gt;105&lt;/TD&gt;
&lt;TD class="xl63" style="border-top: none; border-left: none;"&gt;Other&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want the dataset to look like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 288pt;" border="0" width="384" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL style="width: 48pt;" span="6" width="64" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" class="xl66" style="height: 15.0pt; width: 48pt;"&gt;ID&lt;/TD&gt;
&lt;TD width="64" class="xl66" style="border-left: none; width: 48pt;"&gt;UK&lt;/TD&gt;
&lt;TD width="64" class="xl66" style="border-left: none; width: 48pt;"&gt;France&lt;/TD&gt;
&lt;TD width="64" class="xl66" style="border-left: none; width: 48pt;"&gt;Spain&lt;/TD&gt;
&lt;TD width="64" class="xl66" style="border-left: none; width: 48pt;"&gt;Other&lt;/TD&gt;
&lt;TD width="64" class="xl66" style="border-left: none; width: 48pt;"&gt;Count&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;101&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;102&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;103&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;104&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" class="xl65" style="height: 15.0pt; border-top: none;"&gt;105&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Thu, 27 Mar 2025 10:20:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Observations-to-Columns-and-add-Flag/m-p/962770#M375199</guid>
      <dc:creator>anandrc</dc:creator>
      <dc:date>2025-03-27T10:20:06Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose Observations to Columns and add Flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Observations-to-Columns-and-add-Flag/m-p/962777#M375200</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Location $;
datalines;
101 UK     
101 France 
101 Other  
102 UK     
102 Spain  
103 Other  
104 UK     
104 Other  
105 Other  
;

proc transpose data = have out = temp(drop = _:);
    by ID;
    id Location;
    var Location;
run;


data want;
   set temp;
   array a _character_;
   count = 0;
   do over a;
      a = ifc(a ne '', 'Y', a);
      if a = 'Y' then count + 1;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Mar 2025 11:08:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Observations-to-Columns-and-add-Flag/m-p/962777#M375200</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2025-03-27T11:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose Observations to Columns and add Flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Observations-to-Columns-and-add-Flag/m-p/962779#M375201</link>
      <description>&lt;P&gt;Thanks for your input.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, ID column also gets replaced with "Y".&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 288pt;" border="0" width="384" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL style="width: 48pt;" span="6" width="64" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" class="xl66" style="height: 15.0pt; width: 48pt;"&gt;ID&lt;/TD&gt;
&lt;TD width="64" class="xl66" style="border-left: none; width: 48pt;"&gt;UK&lt;/TD&gt;
&lt;TD width="64" class="xl66" style="border-left: none; width: 48pt;"&gt;France&lt;/TD&gt;
&lt;TD width="64" class="xl66" style="border-left: none; width: 48pt;"&gt;Spain&lt;/TD&gt;
&lt;TD width="64" class="xl66" style="border-left: none; width: 48pt;"&gt;Other&lt;/TD&gt;
&lt;TD width="64" class="xl66" style="border-left: none; width: 48pt;"&gt;Count&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl65" style="height: 15.0pt; border-top: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl65" style="height: 15.0pt; border-top: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl65" style="height: 15.0pt; border-top: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl65" style="height: 15.0pt; border-top: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl65" style="height: 15.0pt; border-top: none;"&gt;Y&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;Y&lt;/TD&gt;
&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Thu, 27 Mar 2025 11:16:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Observations-to-Columns-and-add-Flag/m-p/962779#M375201</guid>
      <dc:creator>anandrc</dc:creator>
      <dc:date>2025-03-27T11:16:53Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose Observations to Columns and add Flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Observations-to-Columns-and-add-Flag/m-p/962780#M375202</link>
      <description>&lt;P&gt;Only if ID is character (it is not in the sample data that I posted).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ Location $;
datalines;
101 UK     
101 France 
101 Other  
102 UK     
102 Spain  
103 Other  
104 UK     
104 Other  
105 Other  
;

proc transpose data = have out = temp(drop = _:);
    by ID;
    id Location;
    var Location;
run;

data want;
   set temp;
   array a UK -- Spain;
   count = 0;
   do over a;
      a = ifc(a ne '', 'Y', a);
      if a = 'Y' then count + 1;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Mar 2025 11:24:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Observations-to-Columns-and-add-Flag/m-p/962780#M375202</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2025-03-27T11:24:13Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose Observations to Columns and add Flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Observations-to-Columns-and-add-Flag/m-p/962817#M375220</link>
      <description>&lt;P&gt;Thanks for your suggetion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Except for ID variable, how do I rename the all the transposed location variables with prefix m_?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, in my final dataset i want&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="354"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;ID&lt;/TD&gt;
&lt;TD width="64"&gt;m_UK&lt;/TD&gt;
&lt;TD width="75"&gt;m_France&lt;/TD&gt;
&lt;TD width="77"&gt;m_Spain&lt;/TD&gt;
&lt;TD width="74"&gt;m_Other&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Fri, 28 Mar 2025 10:50:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Observations-to-Columns-and-add-Flag/m-p/962817#M375220</guid>
      <dc:creator>anandrc</dc:creator>
      <dc:date>2025-03-28T10:50:43Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose Observations to Columns and add Flag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Observations-to-Columns-and-add-Flag/m-p/962818#M375221</link>
      <description>&lt;P&gt;Use the prefix= option in Proc Transpose like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ Location $;
datalines;
101 UK     
101 France 
101 Other  
102 UK     
102 Spain  
103 Other  
104 UK     
104 Other  
105 Other  
;

proc transpose data = have out = temp(drop = _:) prefix = m_;
    by ID;
    id Location;
    var Location;
run;

data want;
   set temp;
   array a m_:;
   count = 0;
   do over a;
      a = ifc(a ne '', 'Y', a);
      if a = 'Y' then count + 1;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Mar 2025 10:54:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Observations-to-Columns-and-add-Flag/m-p/962818#M375221</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2025-03-28T10:54:30Z</dc:date>
    </item>
  </channel>
</rss>

