<?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 Updating multiple obs in BY group in Developers</title>
    <link>https://communities.sas.com/t5/Developers/Updating-multiple-obs-in-BY-group/m-p/7126#M2195</link>
    <description>I am working with data that has multiple observations per my BY statement; but has unique variables with data that is always unknown and changing.  I need to update one column for all occurrences of this data.  I've tried using UPDATE and MERGE and have come to the conclusion that those will not work.  Is there a command or option that enables me to do this without altering the data in the two source datasets?&lt;BR /&gt;
&lt;BR /&gt;
This sample code is a simplified concept of what I'm trying to do.  The desired output is that all observations for days 3 and 4 to equal 'weekend' regardless of the number of transactions that occur.  I cannot easily change set1; or know the number of transactions that occur on any given day.  &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data set1;&lt;BR /&gt;
   do days = 1 to 10;&lt;BR /&gt;
      %let NumOfTrans = int(rand('POISSON', 2)+1);&lt;BR /&gt;
      do transaction = 1 to &amp;amp;NumOfTrans;&lt;BR /&gt;
         daytype = 'workday';&lt;BR /&gt;
         output;&lt;BR /&gt;
      end;&lt;BR /&gt;
   end;&lt;BR /&gt;
run;&lt;BR /&gt;
data set2;&lt;BR /&gt;
   do days = 3 to 4;&lt;BR /&gt;
      daytype = 'weekend';&lt;BR /&gt;
      output;&lt;BR /&gt;
   end;&lt;BR /&gt;
run;&lt;BR /&gt;
data set3;&lt;BR /&gt;
   UPDATE set1 set2;&lt;BR /&gt;
   BY days;&lt;BR /&gt;
run;</description>
    <pubDate>Tue, 26 Feb 2008 02:19:16 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2008-02-26T02:19:16Z</dc:date>
    <item>
      <title>Updating multiple obs in BY group</title>
      <link>https://communities.sas.com/t5/Developers/Updating-multiple-obs-in-BY-group/m-p/7126#M2195</link>
      <description>I am working with data that has multiple observations per my BY statement; but has unique variables with data that is always unknown and changing.  I need to update one column for all occurrences of this data.  I've tried using UPDATE and MERGE and have come to the conclusion that those will not work.  Is there a command or option that enables me to do this without altering the data in the two source datasets?&lt;BR /&gt;
&lt;BR /&gt;
This sample code is a simplified concept of what I'm trying to do.  The desired output is that all observations for days 3 and 4 to equal 'weekend' regardless of the number of transactions that occur.  I cannot easily change set1; or know the number of transactions that occur on any given day.  &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data set1;&lt;BR /&gt;
   do days = 1 to 10;&lt;BR /&gt;
      %let NumOfTrans = int(rand('POISSON', 2)+1);&lt;BR /&gt;
      do transaction = 1 to &amp;amp;NumOfTrans;&lt;BR /&gt;
         daytype = 'workday';&lt;BR /&gt;
         output;&lt;BR /&gt;
      end;&lt;BR /&gt;
   end;&lt;BR /&gt;
run;&lt;BR /&gt;
data set2;&lt;BR /&gt;
   do days = 3 to 4;&lt;BR /&gt;
      daytype = 'weekend';&lt;BR /&gt;
      output;&lt;BR /&gt;
   end;&lt;BR /&gt;
run;&lt;BR /&gt;
data set3;&lt;BR /&gt;
   UPDATE set1 set2;&lt;BR /&gt;
   BY days;&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 26 Feb 2008 02:19:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Updating-multiple-obs-in-BY-group/m-p/7126#M2195</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-02-26T02:19:16Z</dc:date>
    </item>
    <item>
      <title>Re: Updating multiple obs in BY group</title>
      <link>https://communities.sas.com/t5/Developers/Updating-multiple-obs-in-BY-group/m-p/7127#M2196</link>
      <description>Hi:&lt;BR /&gt;
  Using a %LET statement may not be having the effect that you want. Essentially, at compile time (not execution time), the macro variable &amp;amp;NUMOFTRANS is being set to the string that is the function call. You can see this by turning on the SYMBOLGEN option and then examining the LOG for how &amp;amp;NUMOFTRANS was resolved:&lt;BR /&gt;
[pre]&lt;BR /&gt;
SYMBOLGEN:  Macro variable NUMOFTRANS resolves to int(rand('POISSON', 2)+1)&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
If you want to use these functions to set the end point of your inner loop, you'd be just as well off using a data step variable. In this case, the value of X would change with each iteration through the outer loop.&lt;BR /&gt;
[pre]&lt;BR /&gt;
data set1;&lt;BR /&gt;
  do days = 1 to 10;&lt;BR /&gt;
&lt;BR /&gt;
    x = int(rand('POISSON', 2)+1);&lt;BR /&gt;
    putlog '-------&amp;gt; ' x=; &lt;BR /&gt;
&lt;BR /&gt;
    do transaction = 1 to x;&lt;BR /&gt;
       daytype = 'workday';&lt;BR /&gt;
       output;&lt;BR /&gt;
    end;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
    &lt;BR /&gt;
proc print data=set1;&lt;BR /&gt;
title 'Set1';&lt;BR /&gt;
run;&lt;BR /&gt;
   &lt;BR /&gt;
data set2;&lt;BR /&gt;
  do days = 3 to 4;&lt;BR /&gt;
     daytype = 'weekend';&lt;BR /&gt;
     output;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc print data=set2;&lt;BR /&gt;
  title 'Set2';&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Output for SET1:&lt;BR /&gt;
[pre]   &lt;BR /&gt;
                        Set1&lt;BR /&gt;
     &lt;BR /&gt;
Obs    days    x    transaction    daytype&lt;BR /&gt;
&lt;BR /&gt;
  1      1     2         1         workday&lt;BR /&gt;
  2      1     2         2         workday&lt;BR /&gt;
  3      2     2         1         workday&lt;BR /&gt;
  4      2     2         2         workday&lt;BR /&gt;
  5      3     3         1         workday&lt;BR /&gt;
  6      3     3         2         workday&lt;BR /&gt;
  7      3     3         3         workday&lt;BR /&gt;
  8      4     4         1         workday&lt;BR /&gt;
  9      4     4         2         workday&lt;BR /&gt;
 10      4     4         3         workday&lt;BR /&gt;
 11      4     4         4         workday&lt;BR /&gt;
 12      5     2         1         workday&lt;BR /&gt;
 13      5     2         2         workday&lt;BR /&gt;
 14      6     5         1         workday&lt;BR /&gt;
 15      6     5         2         workday&lt;BR /&gt;
 16      6     5         3         workday&lt;BR /&gt;
 17      6     5         4         workday&lt;BR /&gt;
 18      6     5         5         workday&lt;BR /&gt;
 19      7     2         1         workday&lt;BR /&gt;
 20      7     2         2         workday&lt;BR /&gt;
 21      8     3         1         workday&lt;BR /&gt;
 22      8     3         2         workday&lt;BR /&gt;
 23      8     3         3         workday&lt;BR /&gt;
 24      9     6         1         workday&lt;BR /&gt;
 25      9     6         2         workday&lt;BR /&gt;
 26      9     6         3         workday&lt;BR /&gt;
 27      9     6         4         workday&lt;BR /&gt;
 28      9     6         5         workday&lt;BR /&gt;
 29      9     6         6         workday&lt;BR /&gt;
 30     10     1         1         workday&lt;BR /&gt;
&lt;BR /&gt;
                [/pre]&lt;BR /&gt;
&lt;BR /&gt;
The output for SET2:&lt;BR /&gt;
[pre]&lt;BR /&gt;
                Set2&lt;BR /&gt;
     &lt;BR /&gt;
   Obs    days    daytype&lt;BR /&gt;
    1       3     weekend&lt;BR /&gt;
    2       4     weekend&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
So if I understand what you want, you'd like to see ALL days 3 and 4 in SET1 changed to WEEKEND????? You could do that with a second data step program and an IF statement -- you don't need a MERGE or an UPDATE statement:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data newset;&lt;BR /&gt;
  set set1;&lt;BR /&gt;
  if days = 3 or days = 4 then do;&lt;BR /&gt;
    daytype = 'weekend';&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
A PROC PRINT for the NEWSET dataset shows that only days=3 or days=4 have been changed:&lt;BR /&gt;
[pre]&lt;BR /&gt;
           NEWSET from SET1&lt;BR /&gt;
&lt;BR /&gt;
Obs    days    x    transaction    daytype&lt;BR /&gt;
&lt;BR /&gt;
  1      1     2         1         workday&lt;BR /&gt;
  2      1     2         2         workday&lt;BR /&gt;
  3      2     2         1         workday&lt;BR /&gt;
  4      2     2         2         workday&lt;BR /&gt;
  5      3     3         1         weekend&lt;BR /&gt;
  6      3     3         2         weekend&lt;BR /&gt;
  7      3     3         3         weekend&lt;BR /&gt;
  8      4     4         1         weekend&lt;BR /&gt;
  9      4     4         2         weekend&lt;BR /&gt;
 10      4     4         3         weekend&lt;BR /&gt;
 11      4     4         4         weekend&lt;BR /&gt;
 12      5     2         1         workday&lt;BR /&gt;
 13      5     2         2         workday&lt;BR /&gt;
 14      6     5         1         workday&lt;BR /&gt;
 15      6     5         2         workday&lt;BR /&gt;
 16      6     5         3         workday&lt;BR /&gt;
 17      6     5         4         workday&lt;BR /&gt;
 18      6     5         5         workday&lt;BR /&gt;
 19      7     2         1         workday&lt;BR /&gt;
 20      7     2         2         workday&lt;BR /&gt;
 21      8     3         1         workday&lt;BR /&gt;
 22      8     3         2         workday&lt;BR /&gt;
 23      8     3         3         workday&lt;BR /&gt;
 24      9     6         1         workday&lt;BR /&gt;
 25      9     6         2         workday&lt;BR /&gt;
 26      9     6         3         workday&lt;BR /&gt;
 27      9     6         4         workday&lt;BR /&gt;
 28      9     6         5         workday&lt;BR /&gt;
 29      9     6         6         workday&lt;BR /&gt;
 30     10     1         1         workday&lt;BR /&gt;
&lt;BR /&gt;
 [/pre]&lt;BR /&gt;
 &lt;BR /&gt;
Or, you could even just change your original program for SET1 to do this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data altset1;&lt;BR /&gt;
  do days = 1 to 10;&lt;BR /&gt;
    x = int(rand('POISSON', 2)+1);&lt;BR /&gt;
    putlog '-------&amp;gt; ' x=;&lt;BR /&gt;
 &lt;BR /&gt;
    do transaction = 1 to x;&lt;BR /&gt;
       daytype = 'workday';&lt;BR /&gt;
       if days = 3 or days = 4 then do;&lt;BR /&gt;
          daytype = 'weekend';&lt;BR /&gt;
       end;&lt;BR /&gt;
       output;&lt;BR /&gt;
    end;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
   &lt;BR /&gt;
proc print data=altset1;&lt;BR /&gt;
title 'AltSet1';&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
With this version of the program, no matter how many transactions you have, ALL the transactions for days 3 and 4 will be set to WEEKEND and all the other days will be set to WEEKDAY.&lt;BR /&gt;
 &lt;BR /&gt;
At any rate, I'm not sure how this is related to stored processes. Perhaps you meant to post this in the other forum on macro variables and DATA step processing. If you have a particular need to use macro variables, then you might read about the %SYSFUNC macro function, which allows you to invokve data step functions to set the value of a macro variable. However, there are a couple of downsides to %SYSFUNC -- one thing is that you can't nest 2 %SYSFUNC calls. But I don't think you need it.&lt;BR /&gt;
 &lt;BR /&gt;
 If you have other issues with creating a test data set or deciding how to use SAS functions, you might consider contacting Tech Support for help deciding how to create your test data set.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Tue, 26 Feb 2008 06:47:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Updating-multiple-obs-in-BY-group/m-p/7127#M2196</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-02-26T06:47:48Z</dc:date>
    </item>
    <item>
      <title>Re: Updating multiple obs in BY group</title>
      <link>https://communities.sas.com/t5/Developers/Updating-multiple-obs-in-BY-group/m-p/7128#M2197</link>
      <description>maybe adding in some simiplified sample code only confused the situation.  &lt;BR /&gt;
&lt;BR /&gt;
So to begin, I added the %let and rand() only to quickly produce a random number to simulate the effect of having an unknown number of transactions per day.&lt;BR /&gt;
&lt;BR /&gt;
Also the two data sets are much more complex than these and as I said before I have no ability to change set1 because of variability.  &lt;BR /&gt;
&lt;BR /&gt;
I am fairly familiar with SAS so simple solutions like an if statment would have been implemented and maybe implemented if it were feasible or become my only choice.&lt;BR /&gt;
&lt;BR /&gt;
In general, I feel that if I am doing something that requires a lot of time and involved coding, I find that I am doing it the hard/wrong way.  And it is likely that SAS already has a method of doing what i'd like to do.  I'm fairly certain that method will look like an update or a merge, though obviously neither of those.  I was just hoping that someone could point me in the right direction.  Is there a 1-to-MANY merge/update method?</description>
      <pubDate>Tue, 26 Feb 2008 15:38:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Updating-multiple-obs-in-BY-group/m-p/7128#M2197</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-02-26T15:38:22Z</dc:date>
    </item>
    <item>
      <title>Re: Updating multiple obs in BY group</title>
      <link>https://communities.sas.com/t5/Developers/Updating-multiple-obs-in-BY-group/m-p/7129#M2198</link>
      <description>GOT IT!  Since I had no ability to change set1 I looked at changing set2.  I noticed that when I merge a new (not previously existing) variable to set1 that it merged for all occurences of the BY group.  Then with an if statement after the merge I was able to assign the value to the proper variable and do some clean up.  Thanks for your help.  Here is what the example would look like...&lt;BR /&gt;
&lt;BR /&gt;
data set1;&lt;BR /&gt;
	do days = 1 to 10;&lt;BR /&gt;
		%let NumOfTrans = int(rand('POISSON', 2)+1);&lt;BR /&gt;
		do transaction = 1 to &amp;amp;NumOfTrans;&lt;BR /&gt;
			daytype = 'workday';&lt;BR /&gt;
			output;&lt;BR /&gt;
		end;&lt;BR /&gt;
	end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data set2;&lt;BR /&gt;
	do days = 3 to 4;&lt;BR /&gt;
		daytypeholder = 'weekend';&lt;BR /&gt;
		output;&lt;BR /&gt;
	end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data set3;&lt;BR /&gt;
	merge set1 set2;&lt;BR /&gt;
	by days;&lt;BR /&gt;
	if daytypeholder = 'weekend' then daytype = 'weekend';&lt;BR /&gt;
	drop daytypeholder;&lt;BR /&gt;
run;

Message was edited by: Johnny.Key</description>
      <pubDate>Tue, 26 Feb 2008 17:11:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Updating-multiple-obs-in-BY-group/m-p/7129#M2198</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-02-26T17:11:58Z</dc:date>
    </item>
  </channel>
</rss>

