<?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: Fill age by condition in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15688#M2778</link>
    <description>Hi.</description>
    <pubDate>Thu, 24 Feb 2011 09:34:37 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2011-02-24T09:34:37Z</dc:date>
    <item>
      <title>Fill age by condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15680#M2770</link>
      <description>Hi all,&lt;BR /&gt;
&lt;BR /&gt;
I have data set of three columns, name, year and age. My purpose is to fill up age record based on year.&lt;BR /&gt;
&lt;BR /&gt;
The data set looks like,&lt;BR /&gt;
&lt;BR /&gt;
Name   year   age&lt;BR /&gt;
A         1998   &lt;BR /&gt;
A         1999&lt;BR /&gt;
A         2000   44&lt;BR /&gt;
A         2001&lt;BR /&gt;
B         1996   36&lt;BR /&gt;
B         1997   &lt;BR /&gt;
B         1998&lt;BR /&gt;
..........................&lt;BR /&gt;
C         1999   &lt;BR /&gt;
C         2000   38&lt;BR /&gt;
C         2002   &lt;BR /&gt;
D         1994   33&lt;BR /&gt;
D         1995   34&lt;BR /&gt;
D         1996  &lt;BR /&gt;
D         1997   &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Here is the thing. For a specific person, I can always identify his/her age by year by one or more records, like A, if he is 44 in 2000, then in 1998 and 1999, he should be 42 and 43, respectively. &lt;BR /&gt;
The problem is that; the identifier (44 in A's case) can show up irregularly. In A's case, it shows up at the 3rd record; in B's case, the age identifier shows up at the 1st place. &lt;BR /&gt;
Another problem is that, year record can skip. In C's case, it goes from 1999, 2000, and then skip 2001, jumping to 2002 directly. &lt;BR /&gt;
&lt;BR /&gt;
Basically, I want my data look like, &lt;BR /&gt;
&lt;BR /&gt;
Name   year   age&lt;BR /&gt;
A         1998   42&lt;BR /&gt;
A         1999   43&lt;BR /&gt;
A         2000   44&lt;BR /&gt;
A         2001   45&lt;BR /&gt;
B         1996   36&lt;BR /&gt;
B         1997   37&lt;BR /&gt;
B         1998   38&lt;BR /&gt;
..........................&lt;BR /&gt;
C         1999   37&lt;BR /&gt;
C         2000   38&lt;BR /&gt;
C         2002   39&lt;BR /&gt;
D         1994   33&lt;BR /&gt;
D         1995   34&lt;BR /&gt;
D         1996   35&lt;BR /&gt;
D         1997   36&lt;BR /&gt;
&lt;BR /&gt;
Please help and many thanks!</description>
      <pubDate>Wed, 23 Feb 2011 15:02:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15680#M2770</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2011-02-23T15:02:24Z</dc:date>
    </item>
    <item>
      <title>Re: Fill age by condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15681#M2771</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
You provided a part of the desired outcome as:&lt;BR /&gt;
.........................&lt;BR /&gt;
C 1999 37&lt;BR /&gt;
C 2000 38&lt;BR /&gt;
C 2002 39&lt;BR /&gt;
&lt;BR /&gt;
Is that correct?&lt;BR /&gt;
Warm regards,&lt;BR /&gt;
Vasile</description>
      <pubDate>Wed, 23 Feb 2011 15:25:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15681#M2771</guid>
      <dc:creator>Vasile01</dc:creator>
      <dc:date>2011-02-23T15:25:08Z</dc:date>
    </item>
    <item>
      <title>Re: Fill age by condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15682#M2772</link>
      <description>Hello,&lt;BR /&gt;
&lt;BR /&gt;
On solution may be merging the data with itself:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data test;&lt;BR /&gt;
infile datalines missover;&lt;BR /&gt;
input Name $ year age;&lt;BR /&gt;
datalines;&lt;BR /&gt;
A 1998 &lt;BR /&gt;
A 1999&lt;BR /&gt;
A 2000 44&lt;BR /&gt;
A 2001&lt;BR /&gt;
B 1996 36&lt;BR /&gt;
B 1997 &lt;BR /&gt;
B 1998&lt;BR /&gt;
C 1999 &lt;BR /&gt;
C 2000 38&lt;BR /&gt;
C 2002 &lt;BR /&gt;
D 1994 33&lt;BR /&gt;
D 1995 34&lt;BR /&gt;
D 1996 &lt;BR /&gt;
D 1997&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
data out;&lt;BR /&gt;
merge test (drop=age) test(where=(^missing(_age)) rename=(age=_age year=_year));&lt;BR /&gt;
by name;&lt;BR /&gt;
if first.name then do born=_year-_age;&lt;BR /&gt;
age=year-born;&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
else age+1;&lt;BR /&gt;
&lt;BR /&gt;
drop born _age _year;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Marius</description>
      <pubDate>Wed, 23 Feb 2011 15:56:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15682#M2772</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2011-02-23T15:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: Fill age by condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15683#M2773</link>
      <description>My bad.&lt;BR /&gt;
&lt;BR /&gt;
It should be 40 for 2002.</description>
      <pubDate>Wed, 23 Feb 2011 16:11:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15683#M2773</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2011-02-23T16:11:38Z</dc:date>
    </item>
    <item>
      <title>Re: Fill age by condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15684#M2774</link>
      <description>It looks like Cartesian Product is helpful for you.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data test;&lt;BR /&gt;
infile datalines missover;&lt;BR /&gt;
input Name $ year age;&lt;BR /&gt;
datalines;&lt;BR /&gt;
A 1998 &lt;BR /&gt;
A 1999&lt;BR /&gt;
A 2000 44&lt;BR /&gt;
A 2001&lt;BR /&gt;
B 1996 36&lt;BR /&gt;
B 1997 &lt;BR /&gt;
B 1998&lt;BR /&gt;
C 1999 &lt;BR /&gt;
C 2000 38&lt;BR /&gt;
C 2002 &lt;BR /&gt;
D 1994 33&lt;BR /&gt;
D 1995 34&lt;BR /&gt;
D 1996 &lt;BR /&gt;
D 1997&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
data result;&lt;BR /&gt;
 declare hash hh(hashexp: 10 );&lt;BR /&gt;
 hh.definekey('name');&lt;BR /&gt;
 hh.definedata('_year','_age');&lt;BR /&gt;
 hh.definedone();&lt;BR /&gt;
&lt;BR /&gt;
 do until(last);&lt;BR /&gt;
  set test(rename=(year=_year age=_age)) end=last;&lt;BR /&gt;
  if not missing(_age) then hh.replace();&lt;BR /&gt;
 end;&lt;BR /&gt;
&lt;BR /&gt;
 do until(_last);&lt;BR /&gt;
  set test end=_last;&lt;BR /&gt;
  call missing(_year,_age);&lt;BR /&gt;
  hh.find();&lt;BR /&gt;
  if missing(age) then age=_age + ( year - _year);&lt;BR /&gt;
  output;&lt;BR /&gt;
 end;&lt;BR /&gt;
 drop _:;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Thu, 24 Feb 2011 01:53:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15684#M2774</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-02-24T01:53:14Z</dc:date>
    </item>
    <item>
      <title>Re: Fill age by condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15685#M2775</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
Here it is an idea using SQL statements:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
&lt;BR /&gt;
create table temp as&lt;BR /&gt;
		select name, year-age as yb from person&lt;BR /&gt;
		where age is not missing;&lt;BR /&gt;
&lt;BR /&gt;
create table temp2 as&lt;BR /&gt;
		select distinct name, yb from temp;&lt;BR /&gt;
&lt;BR /&gt;
update person &lt;BR /&gt;
		set age=year-(select yb from temp2&lt;BR /&gt;
						where name=person.name&lt;BR /&gt;
						 )  	&lt;BR /&gt;
		where age is missing&lt;BR /&gt;
		;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
Warm regards,&lt;BR /&gt;
Vasile</description>
      <pubDate>Thu, 24 Feb 2011 05:38:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15685#M2775</guid>
      <dc:creator>Vasile01</dc:creator>
      <dc:date>2011-02-24T05:38:44Z</dc:date>
    </item>
    <item>
      <title>Re: Fill age by condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15686#M2776</link>
      <description>Or the following code is more readable ,But it is not suited for large dataset.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data test;&lt;BR /&gt;
infile datalines missover;&lt;BR /&gt;
input Name $ year age;&lt;BR /&gt;
datalines;&lt;BR /&gt;
A 1998 &lt;BR /&gt;
A 1999&lt;BR /&gt;
A 2000 44&lt;BR /&gt;
A 2001&lt;BR /&gt;
B 1996 36&lt;BR /&gt;
B 1997 &lt;BR /&gt;
B 1998&lt;BR /&gt;
C 1999 &lt;BR /&gt;
C 2000 38&lt;BR /&gt;
C 2002 &lt;BR /&gt;
D 1994 33&lt;BR /&gt;
D 1995 34&lt;BR /&gt;
D 1996 &lt;BR /&gt;
D 1997&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
proc sort data=test;&lt;BR /&gt;
 by name;&lt;BR /&gt;
run;&lt;BR /&gt;
data result;&lt;BR /&gt;
 merge test test(rename=(year=_year age=_age) where=(_age is not missing));&lt;BR /&gt;
 by name;&lt;BR /&gt;
 if missing(age) then age=_age + (year - _year);&lt;BR /&gt;
 drop _:;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Thu, 24 Feb 2011 06:34:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15686#M2776</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-02-24T06:34:32Z</dc:date>
    </item>
    <item>
      <title>Re: Fill age by condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15687#M2777</link>
      <description>hello,&lt;BR /&gt;
&lt;BR /&gt;
I changed my code as you made  a correction to the output data:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data out;&lt;BR /&gt;
merge test (drop=age) test(where=(^missing(_age)) rename=(age=_age year=_year));&lt;BR /&gt;
by name;&lt;BR /&gt;
&lt;BR /&gt;
retain born;&lt;BR /&gt;
&lt;BR /&gt;
if first.name then do born=_year-_age;&lt;BR /&gt;
age=year-born;&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
else age=year-born;&lt;BR /&gt;
&lt;BR /&gt;
drop born _age _year;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Marius</description>
      <pubDate>Thu, 24 Feb 2011 08:27:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15687#M2777</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2011-02-24T08:27:59Z</dc:date>
    </item>
    <item>
      <title>Re: Fill age by condition</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15688#M2778</link>
      <description>Hi.</description>
      <pubDate>Thu, 24 Feb 2011 09:34:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Fill-age-by-condition/m-p/15688#M2778</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-02-24T09:34:37Z</dc:date>
    </item>
  </channel>
</rss>

