<?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 Data step: using variable from 1 data set to create new var in another set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Data-step-using-variable-from-1-data-set-to-create-new-var-in/m-p/44506#M9110</link>
    <description>Hello,&lt;BR /&gt;
I have 2 data sets.  One is called Def_Total, the other called PlayerIDs.  In the PlayerIDs data set is a variable called "Bats" which corresponds to the player in question, denoted by the variable "PlayerID".&lt;BR /&gt;
&lt;BR /&gt;
In the Def_Total data set, there is a variable called "BatterID".  This corresponds to the player (variable "PlayerID") in the PlayerIDs dataset.  I want to create a new variable in the Def_Total data set which corresponds to the "Bats" variable from PlayerIDs.&lt;BR /&gt;
&lt;BR /&gt;
So it looks like this:&lt;BR /&gt;
Take the "BatterID", match it to "PlayerID", then have the new variable, "BatterHand" = "Bats" from the PlayerIDs data set.&lt;BR /&gt;
&lt;BR /&gt;
Now, the following code isn't correct, but gives the gist of the idea:&lt;BR /&gt;
&lt;BR /&gt;
[pre] &lt;BR /&gt;
proc sort data=def_total;&lt;BR /&gt;
 by BatterID;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data PlayerIDs Def_total;&lt;BR /&gt;
	set Def_total;&lt;BR /&gt;
	do BatterID = 1 to 10234;&lt;BR /&gt;
		if BatterID = PlayerID then&lt;BR /&gt;
		BatterHand = Bats;&lt;BR /&gt;
	end;&lt;BR /&gt;
run;[/pre]&lt;BR /&gt;
&lt;BR /&gt;
but the other thing is that this would take forever.  It would sort through every PlayerID when setting the variable.  &lt;BR /&gt;
&lt;BR /&gt;
So 2 questions:&lt;BR /&gt;
1.)  Is there a better way to point to the PlayerID to set "BatterHand"?  &lt;BR /&gt;
2.) What's the correct syntax of the data step to make this work?&lt;BR /&gt;
&lt;BR /&gt;
Thanks!!</description>
    <pubDate>Tue, 05 Apr 2011 19:23:40 GMT</pubDate>
    <dc:creator>CharlesR</dc:creator>
    <dc:date>2011-04-05T19:23:40Z</dc:date>
    <item>
      <title>Data step: using variable from 1 data set to create new var in another set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-step-using-variable-from-1-data-set-to-create-new-var-in/m-p/44506#M9110</link>
      <description>Hello,&lt;BR /&gt;
I have 2 data sets.  One is called Def_Total, the other called PlayerIDs.  In the PlayerIDs data set is a variable called "Bats" which corresponds to the player in question, denoted by the variable "PlayerID".&lt;BR /&gt;
&lt;BR /&gt;
In the Def_Total data set, there is a variable called "BatterID".  This corresponds to the player (variable "PlayerID") in the PlayerIDs dataset.  I want to create a new variable in the Def_Total data set which corresponds to the "Bats" variable from PlayerIDs.&lt;BR /&gt;
&lt;BR /&gt;
So it looks like this:&lt;BR /&gt;
Take the "BatterID", match it to "PlayerID", then have the new variable, "BatterHand" = "Bats" from the PlayerIDs data set.&lt;BR /&gt;
&lt;BR /&gt;
Now, the following code isn't correct, but gives the gist of the idea:&lt;BR /&gt;
&lt;BR /&gt;
[pre] &lt;BR /&gt;
proc sort data=def_total;&lt;BR /&gt;
 by BatterID;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data PlayerIDs Def_total;&lt;BR /&gt;
	set Def_total;&lt;BR /&gt;
	do BatterID = 1 to 10234;&lt;BR /&gt;
		if BatterID = PlayerID then&lt;BR /&gt;
		BatterHand = Bats;&lt;BR /&gt;
	end;&lt;BR /&gt;
run;[/pre]&lt;BR /&gt;
&lt;BR /&gt;
but the other thing is that this would take forever.  It would sort through every PlayerID when setting the variable.  &lt;BR /&gt;
&lt;BR /&gt;
So 2 questions:&lt;BR /&gt;
1.)  Is there a better way to point to the PlayerID to set "BatterHand"?  &lt;BR /&gt;
2.) What's the correct syntax of the data step to make this work?&lt;BR /&gt;
&lt;BR /&gt;
Thanks!!</description>
      <pubDate>Tue, 05 Apr 2011 19:23:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-step-using-variable-from-1-data-set-to-create-new-var-in/m-p/44506#M9110</guid>
      <dc:creator>CharlesR</dc:creator>
      <dc:date>2011-04-05T19:23:40Z</dc:date>
    </item>
    <item>
      <title>Re: Data step: using variable from 1 data set to create new var in another set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-step-using-variable-from-1-data-set-to-create-new-var-in/m-p/44507#M9111</link>
      <description>It sounds like you're doing a lookup.  You have info in one table that you'd like to map to another based on a common variable. &lt;BR /&gt;
&lt;BR /&gt;
If that's correct you can do it multiple ways:&lt;BR /&gt;
&lt;BR /&gt;
1) Proc SQL Merge&lt;BR /&gt;
2) Data Step Merge&lt;BR /&gt;
3) Proc Format&lt;BR /&gt;
4) More that I can't think of at the moment. &lt;BR /&gt;
&lt;BR /&gt;
See an example of PROC SQL here &lt;BR /&gt;
&lt;A href="http://www.ats.ucla.edu/stat/sas/modules/sqlmerge.htm" target="_blank"&gt;http://www.ats.ucla.edu/stat/sas/modules/sqlmerge.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
HTH, &lt;BR /&gt;
Reeza</description>
      <pubDate>Tue, 05 Apr 2011 21:32:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-step-using-variable-from-1-data-set-to-create-new-var-in/m-p/44507#M9111</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2011-04-05T21:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: Data step: using variable from 1 data set to create new var in another set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-step-using-variable-from-1-data-set-to-create-new-var-in/m-p/44508#M9112</link>
      <description>Not sure if I get it correct, but I think your looking for a merge (or SQL join). Have a look at this sample.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data Def_Total;&lt;BR /&gt;
  input BatterID;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
1&lt;BR /&gt;
3&lt;BR /&gt;
4&lt;BR /&gt;
6&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
 &lt;BR /&gt;
data PlayerIDs;&lt;BR /&gt;
  input PlayerID Bats;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
1 11&lt;BR /&gt;
2 22&lt;BR /&gt;
3 33&lt;BR /&gt;
4 44&lt;BR /&gt;
5 55&lt;BR /&gt;
6 66&lt;BR /&gt;
7 77&lt;BR /&gt;
8 88&lt;BR /&gt;
9 99&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
 &lt;BR /&gt;
data Def_Total;&lt;BR /&gt;
  merge Def_Total(in=in_Def_Total &lt;BR /&gt;
                  keep=BatterID)&lt;BR /&gt;
        PlayerIDs(in=in_PlayerIDs &lt;BR /&gt;
                  keep=PlayerID Bats &lt;BR /&gt;
                  rename=(PlayerID=BatterID bats=BatterHand));&lt;BR /&gt;
  by BatterID;&lt;BR /&gt;
  if in_PlayerIDs and in_Def_Total then output;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 06 Apr 2011 12:08:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-step-using-variable-from-1-data-set-to-create-new-var-in/m-p/44508#M9112</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2011-04-06T12:08:45Z</dc:date>
    </item>
  </channel>
</rss>

