<?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: Unable to subset a dataset in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851462#M37363</link>
    <description>&lt;P&gt;Actually, the variable I call "Date" is not actually a date. &amp;nbsp;It is an indicator indicating a participant's survey year, and I created it during the data cleaning stage. &amp;nbsp;However, in the process of writing up an explanation of how I created the variable, I figured out that the problem related to my use of the variable labels in the Where statement and not the actual values. &amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the process I used to create the variable: &amp;nbsp;Starting with two separate datasets,&amp;nbsp;one for survey year 2008 and one for survey year 2016, and I cleaned each one separately before merging them to create the dataset I called "dataset". &amp;nbsp;I created and formatted the variables I'm calling "year" using the simple code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;*While cleaning the 2008 data;

value year 0="2008" 1="2016;
year = 0    
format year year.;

*While cleaning the 2016 data;
value year 0="2008" 1="2016;
year = 1    
format year year.;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I then merged the 2008 and 2016 datasets into a single dataset, which included a binary variable called "year" with two values, 0 and 1, and two labels 2008 and 2016. &amp;nbsp;And that is the source of the problem -- I needed to use the values after the equal (=) sign in the Where statement and not the labels. &amp;nbsp;The following code worked perfectly:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data subset;
     set dataset;
     where year=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My apologies for the fire drill! &amp;nbsp;It didn't occur to me that the use of the name "date" would signal an actual date variable and not a plain vanilla numeric variable that happened to be a year. &amp;nbsp;And since I created the variable more than 6 months ago, I had forgotten the actual values were 0 and 1 and not 2008 and 2016. &amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks so much for your quick response -- it helped me figure out the problem.&lt;/P&gt;&lt;P&gt;Kelly&lt;/P&gt;</description>
    <pubDate>Thu, 29 Dec 2022 02:29:48 GMT</pubDate>
    <dc:creator>Kelly_K</dc:creator>
    <dc:date>2022-12-29T02:29:48Z</dc:date>
    <item>
      <title>Unable to subset a dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851440#M37359</link>
      <description>&lt;P&gt;Hi, SAS Community.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to subset a dataset so that only those observations with the year 2008 remain, but I get a data table with no values in any of the columns or there is no change in the output dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data looks something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id &amp;nbsp; &amp;nbsp;var1 &amp;nbsp; &amp;nbsp;var2 &amp;nbsp; &amp;nbsp;var3 &amp;nbsp; &amp;nbsp; &amp;nbsp;year&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; F &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25 &amp;nbsp; &amp;nbsp; &amp;nbsp; no &amp;nbsp; &amp;nbsp; &amp;nbsp; 2008&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;M &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;29 &amp;nbsp; &amp;nbsp; &amp;nbsp; yes &amp;nbsp; &amp;nbsp; 2016&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;M &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22 &amp;nbsp; &amp;nbsp; &amp;nbsp; no &amp;nbsp; &amp;nbsp; &amp;nbsp;2008&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp;F &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;no &amp;nbsp; &amp;nbsp; &amp;nbsp;2016&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the following two code variations but got the empty data table (i.e., all var columns are present but there are no observations in any of the cells):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data subset;
&amp;nbsp;&amp;nbsp; &amp;nbsp; set dataset;
&amp;nbsp;&amp;nbsp; &amp;nbsp; where year=2008;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data subset;
set dataset;
if year=2008;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the following code but the observations with year=2016 remained (i.e., dataset = subset).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data subset;
set dataset;
if year=2016 then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone know what is going on?&lt;SPAN class=""&gt;&amp;nbsp; &lt;/SPAN&gt;In this instance, Google is not being my friend!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kelly&lt;/P&gt;</description>
      <pubDate>Wed, 28 Dec 2022 23:39:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851440#M37359</guid>
      <dc:creator>Kelly_K</dc:creator>
      <dc:date>2022-12-28T23:39:34Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to subset a dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851442#M37360</link>
      <description>&lt;P&gt;Please post your complete SAS log. That should be the first thing to look at if your program doesn't work as expected.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Dec 2022 23:51:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851442#M37360</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-12-28T23:51:39Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to subset a dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851450#M37361</link>
      <description>&lt;P&gt;Your year variable could be one of below&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;type character and contain a string of digits&lt;/LI&gt;
&lt;LI&gt;type numeric and contain a number&lt;/LI&gt;
&lt;LI&gt;type numeric and contain a SAS date value with a format attached to print this count of days since 1/1/1960 as year&lt;/LI&gt;
&lt;LI&gt;type numeric and contain a SAS datetime value with a format attached to print this count of seconds since 1/1/1960 as year&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Below should point you into the right direction.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  year_c='2022';
  year_n=2022;
  year_dt='31dec2022'd;
  year_dttm='31dec2022:23:59:59'dt;
  format year_dt year4. year_dttm dtyear4.;
run;

proc print data=have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1672277133619.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78916i4488CE6DEF0F8133/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1672277133619.png" alt="Patrick_0-1672277133619.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;All the variables above print as year.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc Contents will tell you what you're actually dealing with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1672277315089.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78917i5C2981DA0F7CC0FD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_1-1672277315089.png" alt="Patrick_1-1672277315089.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Depending on the type and content of your source variable your selection syntax for a specific year needs to look differently. One of below options should be suitable for your case.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;

  /* one of below should work */
  if strip(year_c)='2022';

  if year_n=2022;

  if year(year_dt)=2022;
  if put(year_dt,year4.)='2022';

  if year(datepart(year_dttm))=2022;
  if put(year_dttm,dtyear4.)='2022';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 01:29:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851450#M37361</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-12-29T01:29:07Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to subset a dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851462#M37363</link>
      <description>&lt;P&gt;Actually, the variable I call "Date" is not actually a date. &amp;nbsp;It is an indicator indicating a participant's survey year, and I created it during the data cleaning stage. &amp;nbsp;However, in the process of writing up an explanation of how I created the variable, I figured out that the problem related to my use of the variable labels in the Where statement and not the actual values. &amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the process I used to create the variable: &amp;nbsp;Starting with two separate datasets,&amp;nbsp;one for survey year 2008 and one for survey year 2016, and I cleaned each one separately before merging them to create the dataset I called "dataset". &amp;nbsp;I created and formatted the variables I'm calling "year" using the simple code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;*While cleaning the 2008 data;

value year 0="2008" 1="2016;
year = 0    
format year year.;

*While cleaning the 2016 data;
value year 0="2008" 1="2016;
year = 1    
format year year.;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I then merged the 2008 and 2016 datasets into a single dataset, which included a binary variable called "year" with two values, 0 and 1, and two labels 2008 and 2016. &amp;nbsp;And that is the source of the problem -- I needed to use the values after the equal (=) sign in the Where statement and not the labels. &amp;nbsp;The following code worked perfectly:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data subset;
     set dataset;
     where year=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My apologies for the fire drill! &amp;nbsp;It didn't occur to me that the use of the name "date" would signal an actual date variable and not a plain vanilla numeric variable that happened to be a year. &amp;nbsp;And since I created the variable more than 6 months ago, I had forgotten the actual values were 0 and 1 and not 2008 and 2016. &amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks so much for your quick response -- it helped me figure out the problem.&lt;/P&gt;&lt;P&gt;Kelly&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 02:29:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851462#M37363</guid>
      <dc:creator>Kelly_K</dc:creator>
      <dc:date>2022-12-29T02:29:48Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to subset a dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851465#M37364</link>
      <description>&lt;P&gt;Actually, the variable I call "Date" is not actually a date. It is an indicator indicating a participant's survey year, and I created it during the data cleaning stage. However,&lt;/P&gt;&lt;P&gt;in the process of writing up an explanation of how I created the variable, I figured out that the problem related to my use of the variable labels in the Where statement&lt;/P&gt;&lt;P&gt;and not the actual values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the process I used to create the variable: Starting with two separate datasets, one for survey year 2008 and one for survey year 2016, and I cleaned each one&lt;/P&gt;&lt;P&gt;separately before merging them to create the dataset I called "dataset". I created and formatted the variables I'm calling "year" using the simple code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;*WHILE CLEANING THE 2008 DATA; 
value year 0="2008" 1="2016; 
*As part of a data step; 
year = 0; 
format year year.; 

*WHILE CLEANING THE 2016 DATA; 
value year 0="2008" 1="2016; 
*As part of a data step; 
year = 1 
format year year.;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I then merged the 2008 and 2016 datasets into a single dataset, which included a binary variable called "year" with two values, 0 and 1, and two labels 2008 and 2016&lt;/P&gt;&lt;P&gt;And that is the source of the problem -- I needed to use the values after the equal (=) sign in the Where statement and not the labels. The following code worked perfectly.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data sample2008;
     set combined;
     where year=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So sorry for the fire drill.&lt;SPAN class=""&gt;&amp;nbsp; &lt;/SPAN&gt;It hadn't occurred to me that naming the variable "Year" would signal to others that it was a year variable and not a plain vanilla numeric variable.&lt;SPAN class=""&gt;&amp;nbsp; &lt;/SPAN&gt;And since I created the variable more than 6 months ago, I had forgotten that the actual values were 0 and 1. &lt;SPAN class=""&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your quick reply — it helped me figure out the problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kelly&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 02:42:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851465#M37364</guid>
      <dc:creator>Kelly_K</dc:creator>
      <dc:date>2022-12-29T02:42:46Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to subset a dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851548#M37376</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/371959"&gt;@Kelly_K&lt;/a&gt; - Perhaps this is also a lesson about naming your variables correctly based on their contents &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 20:09:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851548#M37376</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-12-29T20:09:59Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to subset a dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851549#M37377</link>
      <description>&lt;P&gt;Gee, thanks for the unsolicited advice. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 20:13:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851549#M37377</guid>
      <dc:creator>Kelly_K</dc:creator>
      <dc:date>2022-12-29T20:13:38Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to subset a dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851550#M37378</link>
      <description>&lt;P&gt;Your terminology usage is off.&lt;/P&gt;
&lt;P&gt;A variable can only have one LABEL.&amp;nbsp; It is used to assign a more descriptive string to a variable that can be used for example as the column header when printing a report.&amp;nbsp; So in your example you might have wanted to attach the a label like "Survey YEAR" to the variable YEAR.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you are calling a LABEL is instead the FORMATTED value.&amp;nbsp; So your variable is numeric with values of 0 or 1.&amp;nbsp; You have associated a format with the variable so that the values are displayed using the result of applying the format to the values the variable contains.&amp;nbsp; So 0 is printed as the text string "2008" and 1 is printed as the string "2016".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to test based on the formatted value of a variable you use the PUT() (or putn() or putc()) function&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data subset;
     set dataset;
     where put(year,year.)='2008';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or if you use the VVALUE() function you don't need to know the name of the format attached to the variable, but you cannot use that in a WHERE.&amp;nbsp; So use a subsetting IF instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data subset;
     set dataset;
     if vvalue(year)='2008';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 20:41:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Unable-to-subset-a-dataset/m-p/851550#M37378</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-12-29T20:41:52Z</dc:date>
    </item>
  </channel>
</rss>

