<?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: count of similar named values in column while transposing data long to wide in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/count-of-similar-named-values-in-column-while-transposing-data/m-p/562661#M157636</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have(drop=store_id);
	input ID $ year_month	monthly_spending	store_id $15.;
	store=scan(store_id,1);

cards;
123 201107 25 storeA 5555
123 201107 10 storeA 444
123 201108 50 storeB 33
1234 201010 45 storeA 5555
1234 201010 40 storeA 5555
1234 201011 30 storeC 222
12345 200906 24 storeC 222
12345 200907 3 storeC 222
12345 200907 27 storeB  33
12345 200908 15 storeA 555
;

run;


proc sort data=have;
	by Id year_month store;
run;

data avg_vis;
	sum_tot=0;
	visit=0;
	do until(last.store);
		set have;
		by Id year_month store;
		sum_tot+monthly_spending;
		visit+1;
	end;
	avg_mnth=sum_tot/visit;
	drop sum_tot;
run;


proc transpose data=avg_vis out=avg_mth prefix=avg_mth_;
	by id year_month;
	var  avg_mnth ;
	id store;
run;


proc transpose data=avg_vis out=visit suffix=_visit;
	by id year_month;
	var  visit ;
	id store;
run;

data want;
	merge avg_mth visit;
	by id year_month;
	
	array init{*} _numeric_;

	do i=1 to dim(init);
		if missing(of init[i]) then init[i]=0;
	end;
	drop i _name_;;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 30 May 2019 19:05:22 GMT</pubDate>
    <dc:creator>r_behata</dc:creator>
    <dc:date>2019-05-30T19:05:22Z</dc:date>
    <item>
      <title>count of similar named values in column while transposing data long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-of-similar-named-values-in-column-while-transposing-data/m-p/562634#M157627</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am new to SAS and I am able to manipulate my dataframe pretty well but need help counting values in a column and then transposing the data from long to wide.&amp;nbsp; My data looks like&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;year_month&lt;/TD&gt;&lt;TD&gt;monthly_spending&lt;/TD&gt;&lt;TD&gt;store&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;201107&lt;/TD&gt;&lt;TD&gt;25&lt;/TD&gt;&lt;TD&gt;storeA&amp;nbsp; 5555&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;201107&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;storeA&amp;nbsp; 444&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;201108&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;storeB&amp;nbsp; 33&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1234&lt;/TD&gt;&lt;TD&gt;201010&lt;/TD&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;TD&gt;storeA&amp;nbsp; 5555&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1234&lt;/TD&gt;&lt;TD&gt;201010&lt;/TD&gt;&lt;TD&gt;40&lt;/TD&gt;&lt;TD&gt;storeA&amp;nbsp; 5555&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1234&lt;/TD&gt;&lt;TD&gt;201011&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;storeC 222&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12345&lt;/TD&gt;&lt;TD&gt;200906&lt;/TD&gt;&lt;TD&gt;24&lt;/TD&gt;&lt;TD&gt;storeC 222&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12345&lt;/TD&gt;&lt;TD&gt;200907&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;storeC 222&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12345&lt;/TD&gt;&lt;TD&gt;200907&lt;/TD&gt;&lt;TD&gt;27&lt;/TD&gt;&lt;TD&gt;storeB&amp;nbsp; 33&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12345&lt;/TD&gt;&lt;TD&gt;200908&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;storeA&amp;nbsp; 555&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And what I need is&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;year_month&lt;/TD&gt;&lt;TD&gt;avg_mnth_storeA&lt;/TD&gt;&lt;TD&gt;storeA_visits&lt;/TD&gt;&lt;TD&gt;avg_mnth_storeB&lt;/TD&gt;&lt;TD&gt;storeB_visits&lt;/TD&gt;&lt;TD&gt;avg_mnth_storeC&lt;/TD&gt;&lt;TD&gt;storeC_visits&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;123&lt;/TD&gt;&lt;TD&gt;201107&lt;/TD&gt;&lt;TD&gt;17.5&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&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12345&lt;/TD&gt;&lt;TD&gt;200908&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In these example each store has a name(storeA) and the number following it is its location identifier.&amp;nbsp; I know how to count each store with its identifier but I would like to be able to ignore the location identifier and only have a count of stores with the same name.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also know how to transpose my data from long to wide like this but not how to do it so that the count of the stores is what is in each column and not another value from my dataframe associated with it.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I get a count of each store based on similar names and then transpose that from long to wide where those counts are now in the column?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions are much appreciated!!&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2019 17:54:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-of-similar-named-values-in-column-while-transposing-data/m-p/562634#M157627</guid>
      <dc:creator>dwagner</dc:creator>
      <dc:date>2019-05-30T17:54:14Z</dc:date>
    </item>
    <item>
      <title>Re: count of similar named values in column while transposing data long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-of-similar-named-values-in-column-while-transposing-data/m-p/562651#M157635</link>
      <description>&lt;P&gt;There are a few steps you will need to take.&amp;nbsp; Begin by getting the store name without the location identifier.&amp;nbsp; If the identifier is always the last word within STORE,&amp;nbsp; you could use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
   set have;
   id_len = length(scan(store, -1));
   store_name = substr(store, 1, length(store) - id_len);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You will eventually have to consider whether the STORE_NAME could be a valid portion of a variable name, and whether it is spelled consistently.&amp;nbsp; We're going to ignore that (since only you have the data), but it will be an issue sooner or later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, get statistics, using variable names that are similar to the names you want to use eventually.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=temp nway;
   by id year_month;
   class store_name;
   var monthly_spending;
   output out=temp2 (drop=_type_ _freq_)
   mean = _avg_month
   n=_visits
   ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that transposing will be much easier if you select consistent names ... should the STORE_NAME be a prefix, or should it be a suffix.&amp;nbsp; Here, I'm expecting it to be a prefix that will eventually have either "_avg_month" or "_visits" appended to the name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See if that much makes sense, then we can talk about transposing from long to wide and address the remaining issues.&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2019 18:56:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-of-similar-named-values-in-column-while-transposing-data/m-p/562651#M157635</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-30T18:56:25Z</dc:date>
    </item>
    <item>
      <title>Re: count of similar named values in column while transposing data long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-of-similar-named-values-in-column-while-transposing-data/m-p/562661#M157636</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have(drop=store_id);
	input ID $ year_month	monthly_spending	store_id $15.;
	store=scan(store_id,1);

cards;
123 201107 25 storeA 5555
123 201107 10 storeA 444
123 201108 50 storeB 33
1234 201010 45 storeA 5555
1234 201010 40 storeA 5555
1234 201011 30 storeC 222
12345 200906 24 storeC 222
12345 200907 3 storeC 222
12345 200907 27 storeB  33
12345 200908 15 storeA 555
;

run;


proc sort data=have;
	by Id year_month store;
run;

data avg_vis;
	sum_tot=0;
	visit=0;
	do until(last.store);
		set have;
		by Id year_month store;
		sum_tot+monthly_spending;
		visit+1;
	end;
	avg_mnth=sum_tot/visit;
	drop sum_tot;
run;


proc transpose data=avg_vis out=avg_mth prefix=avg_mth_;
	by id year_month;
	var  avg_mnth ;
	id store;
run;


proc transpose data=avg_vis out=visit suffix=_visit;
	by id year_month;
	var  visit ;
	id store;
run;

data want;
	merge avg_mth visit;
	by id year_month;
	
	array init{*} _numeric_;

	do i=1 to dim(init);
		if missing(of init[i]) then init[i]=0;
	end;
	drop i _name_;;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 May 2019 19:05:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-of-similar-named-values-in-column-while-transposing-data/m-p/562661#M157636</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2019-05-30T19:05:22Z</dc:date>
    </item>
    <item>
      <title>Re: count of similar named values in column while transposing data long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-of-similar-named-values-in-column-while-transposing-data/m-p/562689#M157641</link>
      <description>&lt;P&gt;Here is the logic that I came up with to accomplish your request:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE WORK.Calculations AS
	SELECT DISTINCT
	  	  ID
		, Store
		, year_month
		, COUNT(ID)											AS Visits
		, (SUM(monthly_spending)/ CALCULATED Visits)		AS Avg_Mnth_Spend

	FROM WORK.StoreData
	GROUP BY ID, Store, year_month;
QUIT;

PROC SORT DATA=Calculations; BY ID year_month; RUN;

PROC TRANSPOSE DATA=WORK.Calculations out=AvgMnthly (drop=_name_) prefix=avg_mnth_;
	var Avg_Mnth_Spend ;
	by ID year_month;
	ID Store;
run;

PROC TRANSPOSE DATA=WORK.Calculations out=VisitCt (drop=_name_) suffix=_Visits;
	var Visits ;
	by ID year_month;
	ID Store;
run;


PROC SQL;
CREATE TABLE WORK.FullDtls	AS
	SELECT
		  a.ID				
		, a.year_month		
		, a.avg_mnth_storeA	
		, b.storeA_Visits	
		, a.avg_mnth_storeB	
		, b.storeB_Visits	
		, a.avg_mnth_storeC	
		, b.storeC_Visits

	FROM 		WORK.AvgMnthly	AS a
	INNER JOIN	WORK.VisitCt	AS b	ON a.ID=b.ID
										AND a.year_month=b.year_month
	ORDER BY YEAR(a.year_month) DESC, MONTH(a.year_month);
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The resulting output looks like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="StoreBreakoutData.PNG" style="width: 758px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/29913i6F018840BDB9D5A4/image-dimensions/758x149?v=v2" width="758" height="149" role="button" title="StoreBreakoutData.PNG" alt="StoreBreakoutData.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2019 19:37:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-of-similar-named-values-in-column-while-transposing-data/m-p/562689#M157641</guid>
      <dc:creator>tsap</dc:creator>
      <dc:date>2019-05-30T19:37:32Z</dc:date>
    </item>
    <item>
      <title>Re: count of similar named values in column while transposing data long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-of-similar-named-values-in-column-while-transposing-data/m-p/563691#M158052</link>
      <description>&lt;P&gt;The consistency of the store name did become an issue.&amp;nbsp; I was able to drop the numbers associated with the location but there are instances where there is a different spelling.&amp;nbsp; Is there a way to assign similar names to one variable?&amp;nbsp; For example, if I had a store name SaveMore and in my data I had; SaveMore, SaveMr, SaveM - would I be able to use substr to take the first four letter and say that if the substr='save' then assign to SaveMore?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2019 13:24:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-of-similar-named-values-in-column-while-transposing-data/m-p/563691#M158052</guid>
      <dc:creator>dwagner</dc:creator>
      <dc:date>2019-06-05T13:24:14Z</dc:date>
    </item>
  </channel>
</rss>

