<?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: Adding a custom OBS variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-custom-OBS-variable/m-p/29521#M5563</link>
    <description>oooh! so &lt;I&gt;that's&lt;/I&gt; what _N_ is for? I had seen that before but I never quite understood what it was. Thank-you abdullala, that works. But I still can't get it into 1 data step. This is what I have so far:&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sort data=PROJET.TEMP; by Region descending GDP; run;&lt;BR /&gt;
DATA MAKETHIS;&lt;BR /&gt;
	length Region 3 Country $30 GDP 5;&lt;BR /&gt;
	set SRC;&lt;BR /&gt;
	keep Region Country GDP;&lt;BR /&gt;
	by Region;&lt;BR /&gt;
	if first.Region then output;&lt;BR /&gt;
RUN;&lt;BR /&gt;
DATA MAKETHIS; &lt;BR /&gt;
	length n 3 Region 3 Country $30 GDP 5;&lt;BR /&gt;
	set SRC;&lt;BR /&gt;
	n = 100 + _n_;&lt;BR /&gt;
RUN;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
How can I rewrite this into 1 data step? If I put n=_N_+100 anywhere after the length statement it doesn't work.</description>
    <pubDate>Fri, 11 Dec 2009 17:59:53 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2009-12-11T17:59:53Z</dc:date>
    <item>
      <title>Adding a custom OBS variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-custom-OBS-variable/m-p/29518#M5560</link>
      <description>Hello everyone,&lt;BR /&gt;
&lt;BR /&gt;
I have this simply code:&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sort data=SRC; by Region descending GDP; run;&lt;BR /&gt;
&lt;BR /&gt;
DATA MAKETHIS;&lt;BR /&gt;
    length Region 3 Country $30 GDP 5;&lt;BR /&gt;
    set SRC;&lt;BR /&gt;
    keep Region Country GDP;&lt;BR /&gt;
    by Region;&lt;BR /&gt;
    if first.Region then output;&lt;BR /&gt;
RUN;[/pre]&lt;BR /&gt;
&lt;BR /&gt;
... which takes the &lt;I&gt;Country&lt;/I&gt; with the highest &lt;I&gt;GDP&lt;/I&gt; for each &lt;I&gt;Region&lt;/I&gt; (so is the purpose of the &lt;I&gt;PROC SORT&lt;/I&gt;). This code fully works with no problems but I want to add a column/variable (specifically in first place) called &lt;I&gt;n&lt;/I&gt;. This variable is meant to be the observation number +100.&lt;BR /&gt;
&lt;BR /&gt;
And so I have 2 problems:&lt;BR /&gt;
&lt;BR /&gt;
1) Is there any instrustion in SAS that will allow me to get the row/obs number? Because then I could use that and add 100 to get what I need.&lt;BR /&gt;
&lt;BR /&gt;
2) I don't know how to change the code without creating another data step. If I do this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
DATA MAKETHIS;&lt;BR /&gt;
	length n 3 Region 3 Country $30 GDP 5;&lt;BR /&gt;
	set SRC;&lt;BR /&gt;
	n=100;&lt;BR /&gt;
	keep Region Country GDP;&lt;BR /&gt;
	by Region;&lt;BR /&gt;
	if first.Region then output;&lt;BR /&gt;
RUN;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
This doesn't work, this variable &lt;I&gt;n&lt;/I&gt; doesn't appear. But if I add this data step..:&lt;BR /&gt;
[pre]&lt;BR /&gt;
DATA MAKETHIS; &lt;BR /&gt;
	length n 3 Region 3 Country $30 GDP 5;&lt;BR /&gt;
	set MAKETHIS;&lt;BR /&gt;
	n = 100;&lt;BR /&gt;
RUN;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
...no problem. Ofcourse this doesn't seem like the best of all options.&lt;BR /&gt;
&lt;BR /&gt;
Any suggestion?&lt;BR /&gt;
Thank-you in advance.</description>
      <pubDate>Fri, 11 Dec 2009 17:31:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-custom-OBS-variable/m-p/29518#M5560</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-12-11T17:31:21Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a custom OBS variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-custom-OBS-variable/m-p/29519#M5561</link>
      <description>n=_n_+100;&lt;BR /&gt;
&lt;BR /&gt;
does this work?</description>
      <pubDate>Fri, 11 Dec 2009 17:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-custom-OBS-variable/m-p/29519#M5561</guid>
      <dc:creator>abdullala</dc:creator>
      <dc:date>2009-12-11T17:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a custom OBS variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-custom-OBS-variable/m-p/29520#M5562</link>
      <description>Hi:&lt;BR /&gt;
  Carefully examine your KEEP statement. If you want the variable N to be included in the output dataset WORK.MAKETHIS, then ALL the variables you want to KEEP in WORK.MAKETHIS must be listed in your KEEP statement.&lt;BR /&gt;
 &lt;BR /&gt;
  Also, investigate in the documentation the automatic variable _N_, which counts the number of times a DATA step has executed or has looped. If the DATA step loops one time for every observation being read, then _N_ can sometimes be used as an observation counter -- for simple programs. &lt;BR /&gt;
&lt;BR /&gt;
However, what if you have more than 100 observations in your INPUT file??? If the _N_ value is 523, then your calculated variable N will be 623. Is this what you really want??? Or do you know that your data will always have less than 100 obs?&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Fri, 11 Dec 2009 17:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-custom-OBS-variable/m-p/29520#M5562</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-12-11T17:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a custom OBS variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-custom-OBS-variable/m-p/29521#M5563</link>
      <description>oooh! so &lt;I&gt;that's&lt;/I&gt; what _N_ is for? I had seen that before but I never quite understood what it was. Thank-you abdullala, that works. But I still can't get it into 1 data step. This is what I have so far:&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sort data=PROJET.TEMP; by Region descending GDP; run;&lt;BR /&gt;
DATA MAKETHIS;&lt;BR /&gt;
	length Region 3 Country $30 GDP 5;&lt;BR /&gt;
	set SRC;&lt;BR /&gt;
	keep Region Country GDP;&lt;BR /&gt;
	by Region;&lt;BR /&gt;
	if first.Region then output;&lt;BR /&gt;
RUN;&lt;BR /&gt;
DATA MAKETHIS; &lt;BR /&gt;
	length n 3 Region 3 Country $30 GDP 5;&lt;BR /&gt;
	set SRC;&lt;BR /&gt;
	n = 100 + _n_;&lt;BR /&gt;
RUN;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
How can I rewrite this into 1 data step? If I put n=_N_+100 anywhere after the length statement it doesn't work.</description>
      <pubDate>Fri, 11 Dec 2009 17:59:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-custom-OBS-variable/m-p/29521#M5563</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-12-11T17:59:53Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a custom OBS variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-custom-OBS-variable/m-p/29522#M5564</link>
      <description>Never-mind my post. I got it, I was forgetting to add n to the KEEP statement as well as the LENGTH statement... silly me.</description>
      <pubDate>Fri, 11 Dec 2009 18:01:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-custom-OBS-variable/m-p/29522#M5564</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-12-11T18:01:34Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a custom OBS variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-custom-OBS-variable/m-p/29523#M5565</link>
      <description>Please help by clarifying what is meant with "...anywhere after the length statement it doesn't work."&lt;BR /&gt;
&lt;BR /&gt;
If you want the _N_ value of the first input file (+100), then the suggested assignment statement will work, as explained.  However, if you are only counting the intermediate file's "output observations", then you will need to increment a count variable "_OUTOBS_+1" or something within a DO/END code paragraph, before your OUTPUT, and add add +100 to that variable to create your "N" value.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Fri, 11 Dec 2009 18:05:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-custom-OBS-variable/m-p/29523#M5565</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-12-11T18:05:30Z</dc:date>
    </item>
  </channel>
</rss>

