<?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: Tricky transpose task in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354097#M82773</link>
    <description>&lt;P&gt;It looks like you have N products. There are N ProductX_Category variables and the first N rows have the Product_ID values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So read the values and do a normal array based transform of the category values. &amp;nbsp;But add in a statement to reset the PRODUCT_ID value based on the index into the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dsd;
  input Application_ID Product_ID $ ID $ Product1_Category $ Product2_Category $;
datalines;
11111,a,1,m1,m3
11111,b,2,n2,n4
11111,,3,k3,k5
11111,,4,t3,t7
;

data want ;
  set have ;
  array p product1_Category product2_category ;
  do i=1 to dim(p);
    if i &amp;lt;= nobs then set have(keep=product_id) point=i nobs=nobs;
    Product_Category=p(i);
    output;
  end;
  drop product1_Category product2_category ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/8578i30953E31358420B4/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Apr 2017 13:09:48 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-04-27T13:09:48Z</dc:date>
    <item>
      <title>Tricky transpose task</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/353972#M82702</link>
      <description>&lt;P&gt;I have a dataset in the following structure:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Application_ID  Product_ID  Product1_Category   Product2_Category
11111           a           m1                  m3
11111           b           n2                  n4
11111                       k3                  k5
11111                       t3                  t7&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As you can see, there are two product_id in this application. Each product correspond to their product_category. As the product id come in sequence, the first product id will correspond to Product1_Category, and so on. Due to the weird data structure, I can't use the proc transpose procedure. Here is what the result should be:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Application_ID  Product_ID  Product_Category
11111           a           m1
11111           a           n2
11111           a           k3
11111           a           t3
11112           b           m3
11113           b           n4
11114           b           k5
11115           b           t7&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have tried to use proc transpose but the result is not what I want.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 04:00:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/353972#M82702</guid>
      <dc:creator>cypresswang</dc:creator>
      <dc:date>2017-04-27T04:00:10Z</dc:date>
    </item>
    <item>
      <title>Re: Tricky transpose task</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/353974#M82704</link>
      <description>&lt;P&gt;What's the logic?&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 04:41:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/353974#M82704</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-04-27T04:41:32Z</dc:date>
    </item>
    <item>
      <title>Re: Tricky transpose task</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/353980#M82707</link>
      <description>there are many application ids and each application can have up to 10 products. There are actually 10 Product Categories (Product1_Category to Product10_Category) and each category correspond to one product. For example, product_id "a" correspond to Product1_Category and product_id "b" correspond to Product10_Category. For simplicity, the example provided only has two product ids.</description>
      <pubDate>Thu, 27 Apr 2017 05:01:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/353980#M82707</guid>
      <dc:creator>cypresswang</dc:creator>
      <dc:date>2017-04-27T05:01:41Z</dc:date>
    </item>
    <item>
      <title>Re: Tricky transpose task</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354010#M82726</link>
      <description>&lt;P&gt;This problem is created by the steps leading up to this. &amp;nbsp;It will be quite tricky to get the output you want from that data, it will be far easier to fix previous steps to give good output. &amp;nbsp;Can you post: Test data in the form of a datastep (we don't need to be typing this in for you), and the code steps which create the data as you present it here.&lt;/P&gt;
&lt;P&gt;The alternative is you start writign very messy, hardcoded code like below which I really don't recommend just to fix bad processing before this:&lt;/P&gt;
&lt;PRE&gt;data have;
  infile datalines dsd;
  input Application_ID Product_ID $ Product1_Category $ Product2_Category $;
datalines;
11111,a,m1,m3
11111,b,n2,n4
11111,,k3,k5
11111,,t3,t7
;
run;
data first;
  set have (keep=application_id product_id product1_category rename=(product1_category=cat));
  product_id="a";
run;
data second;
  set have (keep=application_id product_id product2_category rename=(product2_category=cat));
  product_id="b";
run;

data want;
  set first second;
run; &lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Apr 2017 08:32:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354010#M82726</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-27T08:32:07Z</dc:date>
    </item>
    <item>
      <title>Re: Tricky transpose task</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354059#M82753</link>
      <description>&lt;P&gt;Here's a method that doesn't rely on knowing the PRODUCT_ID values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have (where=(product &amp;gt; ' '));&lt;/P&gt;
&lt;P&gt;array prods {2} product1_category product2_category;&lt;/P&gt;
&lt;P&gt;do _i_=1 to _nobs_;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have (drop=product_id) point=_i_ nobs=_nobs_;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;product_category = prods{_n_};&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if _n_ &amp;gt; 1 then application_id + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;keep application_id product_id product;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's not clear if you meant to increment APPLICATION_ID in your example, so that part can always be adjusted. &amp;nbsp;Other adjustments (such as more products) can also be handled ... it's just not clear how much of your example data set is the real structure and how much might be a simplified example.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 11:21:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354059#M82753</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-27T11:21:05Z</dc:date>
    </item>
    <item>
      <title>Re: Tricky transpose task</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354076#M82761</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your response. Unfortunately, I can't fix the bad process as I was given the data as is. Fortunately, I have come up with a way to clean is up, which I will post against my original questions.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 12:15:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354076#M82761</guid>
      <dc:creator>cypresswang</dc:creator>
      <dc:date>2017-04-27T12:15:00Z</dc:date>
    </item>
    <item>
      <title>Re: Tricky transpose task</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354077#M82762</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;, Thanks for your solution. However, it is not what I'm after. I will post my code and the outcome shortly.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 12:16:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354077#M82762</guid>
      <dc:creator>cypresswang</dc:creator>
      <dc:date>2017-04-27T12:16:48Z</dc:date>
    </item>
    <item>
      <title>Re: Tricky transpose task</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354087#M82767</link>
      <description>&lt;P&gt;I have come up with a solution. This may not be the most elegant way but it works. Please feel free to provide any improvement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
  infile datalines dsd;
  input Application_ID Product_ID $ ID $ Product1_Category $ Product2_Category $;
datalines;
11111,a,1,m1,m3
11111,b,2,n2,n4
11111,,3,k3,k5
11111,,4,t3,t7
;
run;

proc transpose data= Have out=Want(drop=_NAME_) Prefix=Prod_;
	by Application_id;
	var Product_ID;
	copy Product1_Category Product2_Category;
	id ID;
run;
	 

data want(drop = Prod_1 - Prod_4 prodtemp_1-prodtemp_4 product1_category product2_category i);
set want;
by Application_ID;
	retain prodtemp_1-prodtemp_4;
	array prodtemp {4} $ prodtemp_1-prodtemp_4;
	array prod {4} $ prod_1 - prod_4;
		do i = 1 to 4;
		if prod{i} ne '' then prodtemp{i} = prod{i};
		prod{i} = prodtemp{i};
		end;
	array category {2} $ Product1_category Product2_category; 
		do i = 1 to 2;
		if category {i} ='' then leave;
		product_category = category{i};
		product = prod{i};
		output;
		end;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 12:34:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354087#M82767</guid>
      <dc:creator>cypresswang</dc:creator>
      <dc:date>2017-04-27T12:34:58Z</dc:date>
    </item>
    <item>
      <title>Re: Tricky transpose task</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354097#M82773</link>
      <description>&lt;P&gt;It looks like you have N products. There are N ProductX_Category variables and the first N rows have the Product_ID values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So read the values and do a normal array based transform of the category values. &amp;nbsp;But add in a statement to reset the PRODUCT_ID value based on the index into the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dsd;
  input Application_ID Product_ID $ ID $ Product1_Category $ Product2_Category $;
datalines;
11111,a,1,m1,m3
11111,b,2,n2,n4
11111,,3,k3,k5
11111,,4,t3,t7
;

data want ;
  set have ;
  array p product1_Category product2_category ;
  do i=1 to dim(p);
    if i &amp;lt;= nobs then set have(keep=product_id) point=i nobs=nobs;
    Product_Category=p(i);
    output;
  end;
  drop product1_Category product2_category ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/8578i30953E31358420B4/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 13:09:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354097#M82773</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-27T13:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: Tricky transpose task</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354381#M82907</link>
      <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;. Your have the best answer</description>
      <pubDate>Fri, 28 Apr 2017 09:58:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Tricky-transpose-task/m-p/354381#M82907</guid>
      <dc:creator>cypresswang</dc:creator>
      <dc:date>2017-04-28T09:58:04Z</dc:date>
    </item>
  </channel>
</rss>

