<?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 How to populate a variable based on what part of date is missing form a date variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-populate-a-variable-based-on-what-part-of-date-is-missing/m-p/699016#M213825</link>
    <description>&lt;P&gt;Dear,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to prepare a code for the below situation. There is a character variable with yymmdd10. format. For some OBS, either year or month or date or all are missing. I need to create a variable for what part is missing. Please suggest in data step two below code. Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if date is complete or missing&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dtf should be blank (for id=&amp;nbsp; a and f)&lt;/P&gt;
&lt;P&gt;if day is missing&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; then dtf=d&amp;nbsp; &amp;nbsp; (id=&amp;nbsp; &amp;nbsp;b)&lt;/P&gt;
&lt;P&gt;if day and month is missing&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; then dtf=m&amp;nbsp; &amp;nbsp; (id=&amp;nbsp; &amp;nbsp;c)&lt;/P&gt;
&lt;P&gt;if year is missing&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; then dtf=y&amp;nbsp; &amp;nbsp; (id=&amp;nbsp; &amp;nbsp;d)&lt;/P&gt;
&lt;P&gt;if month is missing&amp;nbsp; and day is present&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;then dtf=m&amp;nbsp; &amp;nbsp; (id=&amp;nbsp; &amp;nbsp;e)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id$ date $10.;
datalines;
a 2017-07-25
b 2017-07
c 2017
d -07-25
e 2017--25
f
;

data two;
set one;
want=choosec(countw(date, "-"),"M", "D", " ");
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;output needed&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dtf&lt;/P&gt;
&lt;P&gt;a 2017-07-25&lt;BR /&gt;b 2017-07&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;D&lt;BR /&gt;c 2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; M&lt;BR /&gt;d -07-25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Y&lt;BR /&gt;e 2017--25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; M&lt;BR /&gt;f&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;;&lt;/P&gt;</description>
    <pubDate>Mon, 16 Nov 2020 03:31:55 GMT</pubDate>
    <dc:creator>knveraraju91</dc:creator>
    <dc:date>2020-11-16T03:31:55Z</dc:date>
    <item>
      <title>How to populate a variable based on what part of date is missing form a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-populate-a-variable-based-on-what-part-of-date-is-missing/m-p/699016#M213825</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to prepare a code for the below situation. There is a character variable with yymmdd10. format. For some OBS, either year or month or date or all are missing. I need to create a variable for what part is missing. Please suggest in data step two below code. Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if date is complete or missing&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dtf should be blank (for id=&amp;nbsp; a and f)&lt;/P&gt;
&lt;P&gt;if day is missing&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; then dtf=d&amp;nbsp; &amp;nbsp; (id=&amp;nbsp; &amp;nbsp;b)&lt;/P&gt;
&lt;P&gt;if day and month is missing&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; then dtf=m&amp;nbsp; &amp;nbsp; (id=&amp;nbsp; &amp;nbsp;c)&lt;/P&gt;
&lt;P&gt;if year is missing&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; then dtf=y&amp;nbsp; &amp;nbsp; (id=&amp;nbsp; &amp;nbsp;d)&lt;/P&gt;
&lt;P&gt;if month is missing&amp;nbsp; and day is present&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;then dtf=m&amp;nbsp; &amp;nbsp; (id=&amp;nbsp; &amp;nbsp;e)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id$ date $10.;
datalines;
a 2017-07-25
b 2017-07
c 2017
d -07-25
e 2017--25
f
;

data two;
set one;
want=choosec(countw(date, "-"),"M", "D", " ");
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;output needed&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dtf&lt;/P&gt;
&lt;P&gt;a 2017-07-25&lt;BR /&gt;b 2017-07&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;D&lt;BR /&gt;c 2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; M&lt;BR /&gt;d -07-25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Y&lt;BR /&gt;e 2017--25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; M&lt;BR /&gt;f&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 03:31:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-populate-a-variable-based-on-what-part-of-date-is-missing/m-p/699016#M213825</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2020-11-16T03:31:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate a variable based on what part of date is missing form a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-populate-a-variable-based-on-what-part-of-date-is-missing/m-p/699017#M213826</link>
      <description>&lt;P&gt;What are your rules for populating the missing data?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example if day is missing, but month and year are what day do you want - day 1 or something else?&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 03:52:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-populate-a-variable-based-on-what-part-of-date-is-missing/m-p/699017#M213826</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2020-11-16T03:52:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate a variable based on what part of date is missing form a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-populate-a-variable-based-on-what-part-of-date-is-missing/m-p/699021#M213828</link>
      <description>&lt;P&gt;Why not just use the logic in the question in the code instead of trying to generate some type of one liner?&lt;/P&gt;
&lt;P&gt;I added an additional category to handle the case when none of the values are missing but they do not represent a valid date.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input id $ date $10.;
  if missing(date) or not missing(input(date,??yymmdd10.)) then dtf=' ';
  else if missing(scan(date,1,'-','m')) then dtf='Y';
  else if missing(scan(date,2,'-','m')) then dtf='M';
  else if missing(scan(date,3,'-','m')) then dtf='D';
  else dtf='X';
datalines;
a 2017-07-25
b 2017-07
c 2017
d -07-25
e 2017--25
f
g 2017-02-31
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    id    date          dtf

 1     a     2017-07-25
 2     b     2017-07        D
 3     c     2017           M
 4     d     -07-25         Y
 5     e     2017--25       M
 6     f
 7     g     2017-02-31     X&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Nov 2020 04:03:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-populate-a-variable-based-on-what-part-of-date-is-missing/m-p/699021#M213828</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-11-16T04:03:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate a variable based on what part of date is missing form a date variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-populate-a-variable-based-on-what-part-of-date-is-missing/m-p/699058#M213849</link>
      <description>&lt;P&gt;See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set one;
year = input(scan(date,1,"-","m"),4.);
month = input(scan(date,2,"-","m"),2.);
day = input(scan(date,3,"-","m"),2.);
if year and month and day or not year and not month and not day
then dtf = " ";
else if not year
then dtf = "y";
else if not month
then dtf = "m";
else dtf = "d";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With incomplete dates, I would keep separate year/month/day variables, to enable end users to deal with the missing values in a way that is appropriate for their current needs.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 08:10:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-populate-a-variable-based-on-what-part-of-date-is-missing/m-p/699058#M213849</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-16T08:10:02Z</dc:date>
    </item>
  </channel>
</rss>

