<?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: creating new rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/creating-new-rows/m-p/13526#M1604</link>
    <description>I think you still need some kind of transposing, but it can be don in one step - if your data is already sorted by your id column (Station). Something like this :&lt;BR /&gt;
&lt;BR /&gt;
data tn;&lt;BR /&gt;
set notn;&lt;BR /&gt;
by station;&lt;BR /&gt;
retain tknt noxt noxd;&lt;BR /&gt;
if first.station then do;&lt;BR /&gt;
	tknt = .;&lt;BR /&gt;
	noxt = .;&lt;BR /&gt;
	noxd = .;&lt;BR /&gt;
end;&lt;BR /&gt;
select(parameter);&lt;BR /&gt;
when ('TKNT')  tknt = value;&lt;BR /&gt;
when ('NOXT')  noxt = value;&lt;BR /&gt;
when ('NOXD')  noxd = value;otherwise;&lt;BR /&gt;
end;&lt;BR /&gt;
if last.station then do;&lt;BR /&gt;
/* your IF block goes here */&lt;BR /&gt;
output;&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
I think you can call this doing it directly?&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
    <pubDate>Fri, 03 Apr 2009 07:17:21 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2009-04-03T07:17:21Z</dc:date>
    <item>
      <title>creating new rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-new-rows/m-p/13523#M1601</link>
      <description>I’m switching my data source from an old matrix dataset over to columnar data from an Oracle table, but need some help on generating a new variable based on a series of conditions.&lt;BR /&gt;
&lt;BR /&gt;
In the old system:&lt;BR /&gt;
&lt;BR /&gt;
STATION DATETIME         TKNT  NOXT  NOXD &lt;BR /&gt;
ABC        200903251200    1.4       0.5     0.1&lt;BR /&gt;
DEF        200903251300    2.0       0.4     0.02&lt;BR /&gt;
GHI         200903251400      .         0.6     0.1&lt;BR /&gt;
JKL         200903251500    1.9         .       0.2&lt;BR /&gt;
&lt;BR /&gt;
Using this code in a DATA step&lt;BR /&gt;
&lt;BR /&gt;
DATA NEWMATRIX ; SET MATRIXDATA ; &lt;BR /&gt;
…stuff…&lt;BR /&gt;
   IF TN = . THEN DO ;&lt;BR /&gt;
        IF      TKNT EQ . THEN TN = .           ;&lt;BR /&gt;
        ELSE IF NOXD NE . THEN TN = TKNT + NOXD ;&lt;BR /&gt;
        ELSE IF NOXT NE . THEN TN = TKNT + NOXT ;&lt;BR /&gt;
        ELSE                   TN = TKNT        ;&lt;BR /&gt;
   END ;&lt;BR /&gt;
…more stuff…&lt;BR /&gt;
RUN ;&lt;BR /&gt;
&lt;BR /&gt;
Gives this:&lt;BR /&gt;
&lt;BR /&gt;
STATION DATETIME         TKNT  NOXT  NOXD  TN &lt;BR /&gt;
ABC        200903251200    1.4     0.5      0.1      1.5&lt;BR /&gt;
DEF        200903251300     2.0    0.3       .         2.3&lt;BR /&gt;
GHI         200903251400      .        0.6     0.1        .&lt;BR /&gt;
JKL         200903251500    1.3       .         .        1.3&lt;BR /&gt;
&lt;BR /&gt;
Now, my data looks like this:&lt;BR /&gt;
&lt;BR /&gt;
STATION DATETIME          PARAMETER  VALUE&lt;BR /&gt;
ABC        200903251200    TKNT              1.4     &lt;BR /&gt;
ABC        200903251200    NOXT             0.5&lt;BR /&gt;
ABC        200903251200    NOXD            0.1&lt;BR /&gt;
DEF        200903251300    TKNT              2.0     &lt;BR /&gt;
DEF        200903251300    NOXT             0.3&lt;BR /&gt;
GHI        200903251400    NOXT             0.6 &lt;BR /&gt;
…etc…&lt;BR /&gt;
&lt;BR /&gt;
So how do I get here?&lt;BR /&gt;
&lt;BR /&gt;
DATA NEWCOLUMN ; SET COLUMNDATA ;&lt;BR /&gt;
Then what???&lt;BR /&gt;
&lt;BR /&gt;
STATION DATETIME         PARAMETER   VALUE &lt;BR /&gt;
ABC        200903251200    TN                   1.5&lt;BR /&gt;
DEF        200903251300    TN                   2.3&lt;BR /&gt;
JKL         200903251500    TN                   1.3&lt;BR /&gt;
&lt;BR /&gt;
Thanks so much for any references or help you can give me!&lt;BR /&gt;
&lt;BR /&gt;
Wendy</description>
      <pubDate>Tue, 31 Mar 2009 16:45:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-new-rows/m-p/13523#M1601</guid>
      <dc:creator>WendyT</dc:creator>
      <dc:date>2009-03-31T16:45:04Z</dc:date>
    </item>
    <item>
      <title>Re: creating new rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-new-rows/m-p/13524#M1602</link>
      <description>I don't really follow you all the way. Which table is your desired final result?&lt;BR /&gt;
&lt;BR /&gt;
It seems that you want to transpose the data in some way. You can accomplish this by using multiple output statements, after you have assigned the new columns their appropriate values. You might also have look into PROC TRANSPOSE.&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
Linus</description>
      <pubDate>Tue, 31 Mar 2009 20:24:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-new-rows/m-p/13524#M1602</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2009-03-31T20:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: creating new rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-new-rows/m-p/13525#M1603</link>
      <description>Linus - I’m sorry that I didn’t make myself clear in the original post.  I'm looking for a way to do the same calculation without a transposition step. &lt;BR /&gt;
&lt;BR /&gt;
The point I was trying to make is that my data used to be in matrix format, and to make a new parameter (TN), it was quite simple to combine existing parameters when they were all in separate columns with a unique ID for each dataline.&lt;BR /&gt;
&lt;BR /&gt;
STATION DATETIME    TKNT NOXT NOXD&lt;BR /&gt;
&lt;BR /&gt;
Thus, to make the new column of TN, I used&lt;BR /&gt;
&lt;BR /&gt;
IF TN = . THEN DO ;&lt;BR /&gt;
IF TKNT EQ . THEN TN = . ;&lt;BR /&gt;
ELSE IF NOXD NE . THEN TN = TKNT + NOXD ;&lt;BR /&gt;
ELSE IF NOXT NE . THEN TN = TKNT + NOXT ;&lt;BR /&gt;
ELSE TN = TKNT ;&lt;BR /&gt;
END ;&lt;BR /&gt;
&lt;BR /&gt;
My old matrix dataset no longer exists, and my data now exists in columnar format, so I have multiple rows per id, one parameter per line.  So the values TKNT, NOXT, &amp;amp; NOXD now are contained in the parameter column, and the numeric value is contained in the column VALUE.&lt;BR /&gt;
&lt;BR /&gt;
STATION DATETIME  PARAMETER VALUE&lt;BR /&gt;
&lt;BR /&gt;
My question is:  how do I perform the same calculation from above without pulling out the relevant rows and flipping them to matrix format first?&lt;BR /&gt;
&lt;BR /&gt;
The main dataset currently runs about 1.1 million records, so it would be really nice to do this directly. &lt;BR /&gt;
&lt;BR /&gt;
Thanks for any help you can give me!&lt;BR /&gt;
&lt;BR /&gt;
Wendy</description>
      <pubDate>Thu, 02 Apr 2009 22:04:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-new-rows/m-p/13525#M1603</guid>
      <dc:creator>WendyT</dc:creator>
      <dc:date>2009-04-02T22:04:17Z</dc:date>
    </item>
    <item>
      <title>Re: creating new rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-new-rows/m-p/13526#M1604</link>
      <description>I think you still need some kind of transposing, but it can be don in one step - if your data is already sorted by your id column (Station). Something like this :&lt;BR /&gt;
&lt;BR /&gt;
data tn;&lt;BR /&gt;
set notn;&lt;BR /&gt;
by station;&lt;BR /&gt;
retain tknt noxt noxd;&lt;BR /&gt;
if first.station then do;&lt;BR /&gt;
	tknt = .;&lt;BR /&gt;
	noxt = .;&lt;BR /&gt;
	noxd = .;&lt;BR /&gt;
end;&lt;BR /&gt;
select(parameter);&lt;BR /&gt;
when ('TKNT')  tknt = value;&lt;BR /&gt;
when ('NOXT')  noxt = value;&lt;BR /&gt;
when ('NOXD')  noxd = value;otherwise;&lt;BR /&gt;
end;&lt;BR /&gt;
if last.station then do;&lt;BR /&gt;
/* your IF block goes here */&lt;BR /&gt;
output;&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
I think you can call this doing it directly?&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Fri, 03 Apr 2009 07:17:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-new-rows/m-p/13526#M1604</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2009-04-03T07:17:21Z</dc:date>
    </item>
    <item>
      <title>Re: creating new rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-new-rows/m-p/13527#M1605</link>
      <description>Linus-&lt;BR /&gt;
&lt;BR /&gt;
Thanks so much for your help!&lt;BR /&gt;
&lt;BR /&gt;
Your code worked beautifully, and I was working on cleaning up the output and how to get the rest of the unique identifiers from the original dataset.   &lt;BR /&gt;
&lt;BR /&gt;
While I was in that process, one of our lab guys came up with a subquery in SQL that did the trick while keeping everything in place, and I thought you might like to see it.&lt;BR /&gt;
&lt;BR /&gt;
Wendy&lt;BR /&gt;
&lt;BR /&gt;
Here it is:  (&amp;amp;UNIVARS is the list of identifers that generates uniqueness)&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL NOPRINT ;&lt;BR /&gt;
CREATE TABLE TN AS&lt;BR /&gt;
 SELECT &lt;BR /&gt;
 &amp;amp;UNIVARS,&lt;BR /&gt;
 SUM(VALUE) AS VALUE,&lt;BR /&gt;
  FROM EDDATA.IRLDATA&lt;BR /&gt;
   WHERE PARAMETER IN('TKNT','NOXD') AND VALUE NE . &lt;BR /&gt;
         AND EXISTS &lt;BR /&gt;
           (SELECT VALUE FROM EDDATA.IRLDATA WHERE IRLDATA.PARAMETER='NOXD') &lt;BR /&gt;
                    OR IRLDATA.PARAMETER IN('TKNT','NOXT') &lt;BR /&gt;
         AND NOT EXISTS &lt;BR /&gt;
            (SELECT VALUE FROM EDDATA.IRLDATA WHERE IRLDATA.PARAMETER = 'NOXD')  &lt;BR /&gt;
    GROUP BY &amp;amp;UNIVARS &lt;BR /&gt;
    HAVING IRLDATA.PARAMETER = 'TKNT' AND VALUE NE . ;</description>
      <pubDate>Tue, 07 Apr 2009 14:51:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-new-rows/m-p/13527#M1605</guid>
      <dc:creator>WendyT</dc:creator>
      <dc:date>2009-04-07T14:51:27Z</dc:date>
    </item>
  </channel>
</rss>

