<?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: Transforming an unbalanced dataset into a balanced one in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Transforming-an-unbalanced-dataset-into-a-balanced-one/m-p/663811#M22810</link>
    <description>&lt;P&gt;If you've got SAS/ETS licensed then you could use Proc Expand and generate the missing data points instead of throwing away actual data points.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below one way to get what you've asked for.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input CompanyID $ Year Week;
	datalines;
A 2014 1
A 2014 2
A 2014 3
A 2015 1
A 2015 2
A 2015 3
A 2017 1
A 2017 2
B 2014 1
B 2014 3
B 2016 3
B 2017 1
B 2017 2
;

proc sql;
  create table want as
  select
    CompanyID,
    Year,
    Week
  from
  (
    select 
      *, 
      count(*) as n_obs_perYearWeek
    from have
    group by Year,Week
  )
  group by CompanyID
  having max(n_obs_perYearWeek)=n_obs_perYearWeek
  order by CompanyID, year, week
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 21 Jun 2020 08:22:35 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2020-06-21T08:22:35Z</dc:date>
    <item>
      <title>Transforming an unbalanced dataset into a balanced one</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transforming-an-unbalanced-dataset-into-a-balanced-one/m-p/663788#M22808</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;I have a dataset which includes weekly information about several companies from 2014 to 2018 (Identifier variables are: CompanyID, Year, Week).&lt;/P&gt;
&lt;P&gt;This dataset is unbalanced, in the sense that for some of the companies there is no observation (and hence, no row) for some of the weeks of some of the years. I need to transform this dataset into a balanced one. In other words, I need to drop the year/week observations that do not exist for all of the firms. How can I do this in SAS?&lt;/P&gt;
&lt;P&gt;Thanks so much in advance.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jun 2020 02:51:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transforming-an-unbalanced-dataset-into-a-balanced-one/m-p/663788#M22808</guid>
      <dc:creator>AlG</dc:creator>
      <dc:date>2020-06-21T02:51:08Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming an unbalanced dataset into a balanced one</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transforming-an-unbalanced-dataset-into-a-balanced-one/m-p/663808#M22809</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/315691"&gt;@AlG&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an approach to achieve this, using a PROC FREQ and the SPARSE option to r&lt;SPAN&gt;eport all possible combinations of the variable values, even if a combination does not occur in the data.&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input CompanyID $ Year Week;
	datalines;
A 2014 1
A 2014 2
A 2014 3
A 2015 1
A 2015 2
A 2015 3
A 2017 1
A 2017 2
B 2014 1
B 2014 3
B 2016 3
B 2017 1
B 2017 2
;

/* Retrieve list of couples Year-Week where at list a company as no observation */
proc freq data=have noprint;
	table CompanyID*Year*Week / sparse out=have_freq (drop=percent);
run;

proc sort data=have_freq (where=(count=0) drop=CompanyID) out=list_couples (drop=count) nodupkey;
	by Year Week;
run;

/* Create table want */
proc sql;
	create table want as
	select a.*
	from have as a 
		 right join
		 (select Year, Week from have
		 except
		 select Year, Week from list_couples) as b
	on a.Year=b.Year and a.Week = b.Week
	order by CompanyID, Year, Week;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jun 2020 07:55:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transforming-an-unbalanced-dataset-into-a-balanced-one/m-p/663808#M22809</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-06-21T07:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming an unbalanced dataset into a balanced one</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transforming-an-unbalanced-dataset-into-a-balanced-one/m-p/663811#M22810</link>
      <description>&lt;P&gt;If you've got SAS/ETS licensed then you could use Proc Expand and generate the missing data points instead of throwing away actual data points.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below one way to get what you've asked for.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input CompanyID $ Year Week;
	datalines;
A 2014 1
A 2014 2
A 2014 3
A 2015 1
A 2015 2
A 2015 3
A 2017 1
A 2017 2
B 2014 1
B 2014 3
B 2016 3
B 2017 1
B 2017 2
;

proc sql;
  create table want as
  select
    CompanyID,
    Year,
    Week
  from
  (
    select 
      *, 
      count(*) as n_obs_perYearWeek
    from have
    group by Year,Week
  )
  group by CompanyID
  having max(n_obs_perYearWeek)=n_obs_perYearWeek
  order by CompanyID, year, week
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 21 Jun 2020 08:22:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transforming-an-unbalanced-dataset-into-a-balanced-one/m-p/663811#M22810</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-06-21T08:22:35Z</dc:date>
    </item>
    <item>
      <title>Re: Transforming an unbalanced dataset into a balanced one</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transforming-an-unbalanced-dataset-into-a-balanced-one/m-p/663886#M22813</link>
      <description>&lt;P&gt;This query does it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select 
    a.*
from 
    have as a inner join
    (select year, week from have group by year, week having count(distinct companyID) = 
        (select count(distinct companyID) from have)) as b
            on a.year=b.year and a.week=b.week;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 21 Jun 2020 22:20:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transforming-an-unbalanced-dataset-into-a-balanced-one/m-p/663886#M22813</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-06-21T22:20:50Z</dc:date>
    </item>
  </channel>
</rss>

