<?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: selecting observations based on their relative position to other observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425625#M281181</link>
    <description>&lt;P&gt;You've been thinking in Excel terms, not SAS terms.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use RETAIN here to get what you want as well. I'm assuming you're scraping data from either a webpage or copy/pasted out of a report/PDF. There's often ways to move that data to get it in a better format in the first place but I'll consider that irrelevant for the moment.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you use RETAIN, you can trigger the write to your SAS data set once you find a new stock symbol.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't see how you know which is a new symbol yet, but it seems relatively straightforward to me.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; 
set have;
retain stock price &amp;lt;other variables&amp;gt;;

if &amp;lt;stock&amp;gt; then do; 
output;  *output values currently stored;
call missing(stock, price, &amp;lt;other variables&amp;gt;; *reset all values to missing to prevent carry over of values;
stock=value;   *set to new stock ticker;
end;

else if ... then price=value;
else if ... then &amp;lt;other_variables&amp;gt;=value; *since these are all retained, you get a single record for each 'stock' at the end;



run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 07 Jan 2018 21:02:13 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-01-07T21:02:13Z</dc:date>
    <item>
      <title>selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425599#M281172</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose I have a table with a somewhat complex data structure like the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="180"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;n&lt;/TD&gt;
&lt;TD width="116"&gt;line&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;…&lt;/TD&gt;
&lt;TD&gt;…&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;34&lt;/TD&gt;
&lt;TD&gt;text jdjdjf stock&amp;nbsp;IBM&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;35&lt;/TD&gt;
&lt;TD&gt;djdff&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;36&lt;/TD&gt;
&lt;TD&gt;25$&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;…&lt;/TD&gt;
&lt;TD&gt;…&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Whenever I have a line with the word "stock" in it, the actual price is always 2 lines below. So what I would like to have is the following structure:&lt;/P&gt;
&lt;TABLE width="244"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;n&lt;/TD&gt;
&lt;TD width="116"&gt;line&lt;/TD&gt;
&lt;TD width="64"&gt;price&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;34&lt;/TD&gt;
&lt;TD&gt;text jdjdjf stock IBM&lt;/TD&gt;
&lt;TD&gt;25$&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Like this the stock and its price are on the same observations and its possible to make an analysis.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jan 2018 16:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425599#M281172</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2018-01-07T16:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425602#M281173</link>
      <description>&lt;P&gt;1) Why on earth do you have data structure like this? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Where does "A" come from in your line variable instead of IBM?&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jan 2018 15:46:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425602#M281173</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-01-07T15:46:33Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425603#M281174</link>
      <description>&lt;P&gt;Use the @ character feature of the INPUT statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile datalines;
  input @ 'stock'  stock :$5. // price :$5.;
  put (_all_) (=);
datalines;
xxx
stock AAA
aaa
11$
xxx
xxx
stock BBBB
bbb
22$
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The slashes in the input statement tells it to advance one&amp;nbsp; line per slash.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jan 2018 15:52:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425603#M281174</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-01-07T15:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425604#M281175</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input n line $50.;
datalines;
34	text jdjdjf stock IBM
35	djdff 
36	25$
37	text jdjdjf stock IBM
38	djdff 
39	25$
40	text jdjdjf stock IBM
41	djdff 
42	25$
;

data want;
   merge have have(firstobs=3 rename=(line=price));
   if find(line, "stock")&amp;gt;0 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Jan 2018 15:55:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425604#M281175</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-01-07T15:55:09Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425609#M281176</link>
      <description>&lt;P&gt;Hi mkeintz,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;since my data is generated externally, lets assume that I already imported data&amp;nbsp;equivalent to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input line $ 1-10;  
datalines;
xxx
stock AAA
aaa
11$
xxx
xxx
stock BBBB
bbb
22$
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It is now that I need to do the process described in the question. I tried to guess based on your code by doing the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt; set have;&lt;BR /&gt; input @ 'stock' stock :$5. // price :$5.;&lt;BR /&gt; put (_all_) (=);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But unsurprisingly it didn't work&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jan 2018 16:22:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425609#M281176</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2018-01-07T16:22:05Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425611#M281177</link>
      <description>&lt;P&gt;Input is for raw data input, not sas data input.&amp;nbsp;&amp;nbsp; I'd suggest usings &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;'s solultion.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jan 2018 16:24:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425611#M281177</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-01-07T16:24:08Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425612#M281178</link>
      <description>&lt;P&gt;Hi draycut,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks for the code, worked perfectly!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If possible to expand the code, in my case I have more variables of interest. For example, the 4th line after the line that contains the word "stock" I have its industry:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
input n line $50.;
datalines;
30  doidn
31  fifeniofe
32  doewiij
33  fofugrugr
34	text jdjdjf stock IBM
35	djdff 
36	25$
37  fg fgg
38	computers
39	text jdjdjf stock AAA
40	djdff 
41	22$
42  fggh
43	food
44	text jdjdjf stock BBB
45	djdff 
46	55$
47  duehuefhu
48	automotive
49  dfeoufeijer
50  ierhde43848
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Using your code I did the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
   merge have2 have2(firstobs=3 rename=(line=price)) have2(firstobs=5 rename=(line=industry));
   if find(line, "stock")&amp;gt;0 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And also got the result that I wanted!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to ask for your opinion on the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) In my data I have tens to hundreds of thousands of lines (most of them useless) and 5 to 7 variables that I need to extract, so this would mean merging 5 to 7 times the same data table just like above. Is this a good approach, i.e, could there be efficiency or some other problems (like the computer freezing for example)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Recently I worked on some data where I also had to restructure it into a more analysis-friendly form using Excel VBA. Looking at my data I would perhaps do the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Loop through my original data until I find the word "stock". Once I find it, remember the cells row r and paste the cell's content into&lt;/P&gt;
&lt;P&gt;a new spreadsheet in cell (1,2) (the first column in the new sheet is the stock, second column price etc).&lt;/P&gt;
&lt;P&gt;In the original sheet the contents of the cell with row = r+2 are the stock's price and so they will be pasted into the new sheet's cell (2,2), and so on for the other variables...&lt;/P&gt;
&lt;P&gt;And when I reach a new cell with the word "stock" I go to the next line in the new sheet.&lt;/P&gt;
&lt;P&gt;It seems, at least to me, that using cells is easier conceptually (because I couldn't figure out how to do the above mentioned data restructuring using SAS), so is it possible to "cellulize" data in SAS? Is it even worth it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jan 2018 17:19:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425612#M281178</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2018-01-07T17:19:24Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425616#M281179</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12982"&gt;@ilikesas&lt;/a&gt;&amp;nbsp; Good afternoon, IMO unless your sample pattern resembles exactly your real, I'd think the merge might not suffice. You prolly will need a sound method at some point to make it squeaky clean to handle varying patterns.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jan 2018 19:46:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425616#M281179</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-07T19:46:15Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425622#M281180</link>
      <description>&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; have2;&lt;/P&gt;&lt;P&gt;input n line $50.;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;30&amp;nbsp; doidn&lt;/P&gt;&lt;P&gt;31&amp;nbsp; fifeniofe&lt;/P&gt;&lt;P&gt;32&amp;nbsp; doewiij&lt;/P&gt;&lt;P&gt;33&amp;nbsp; fofugrugr&lt;/P&gt;&lt;P&gt;34&amp;nbsp;&amp;nbsp;&amp;nbsp; text jdjdjf stock IBM&lt;/P&gt;&lt;P&gt;35&amp;nbsp;&amp;nbsp;&amp;nbsp; djdff&lt;/P&gt;&lt;P&gt;36&amp;nbsp;&amp;nbsp;&amp;nbsp; 25$&lt;/P&gt;&lt;P&gt;37&amp;nbsp; fg fgg&lt;/P&gt;&lt;P&gt;38&amp;nbsp;&amp;nbsp;&amp;nbsp; computers&lt;/P&gt;&lt;P&gt;39&amp;nbsp;&amp;nbsp;&amp;nbsp; text jdjdjf stock AAA&lt;/P&gt;&lt;P&gt;40&amp;nbsp;&amp;nbsp;&amp;nbsp; djdff&lt;/P&gt;&lt;P&gt;41&amp;nbsp;&amp;nbsp;&amp;nbsp; 22$&lt;/P&gt;&lt;P&gt;42&amp;nbsp; fggh&lt;/P&gt;&lt;P&gt;43&amp;nbsp;&amp;nbsp;&amp;nbsp; food&lt;/P&gt;&lt;P&gt;44&amp;nbsp;&amp;nbsp;&amp;nbsp; text jdjdjf stock BBB&lt;/P&gt;&lt;P&gt;45&amp;nbsp;&amp;nbsp;&amp;nbsp; djdff&lt;/P&gt;&lt;P&gt;46&amp;nbsp;&amp;nbsp;&amp;nbsp; 55$&lt;/P&gt;&lt;P&gt;47&amp;nbsp; duehuefhu&lt;/P&gt;&lt;P&gt;48&amp;nbsp;&amp;nbsp;&amp;nbsp; automotive&lt;/P&gt;&lt;P&gt;49&amp;nbsp; dfeoufeijer&lt;/P&gt;&lt;P&gt;50&amp;nbsp; ierhde43848&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; _null_;&lt;/P&gt;&lt;P&gt;if _N_ = &lt;STRONG&gt;1&lt;/STRONG&gt; then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; if &lt;STRONG&gt;0&lt;/STRONG&gt; then set have2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; length price $&lt;STRONG&gt;10&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash h( );&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.defineKey('_n','_line');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.defineData('_n','_line','price');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.defineDone();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; call missing(price);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;do until(last);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have2 end=last;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if find(line, "stock")&amp;gt;&lt;STRONG&gt;0&lt;/STRONG&gt; then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _n=n;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _line=line;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.add();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if index(line,'$')&amp;gt;&lt;STRONG&gt;0&lt;/STRONG&gt; and&amp;nbsp; h.check()=&lt;STRONG&gt;0&lt;/STRONG&gt; then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; price=line;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.replace();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;if last then h.output(dataset:'want');&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jan 2018 20:42:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425622#M281180</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-07T20:42:20Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425625#M281181</link>
      <description>&lt;P&gt;You've been thinking in Excel terms, not SAS terms.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use RETAIN here to get what you want as well. I'm assuming you're scraping data from either a webpage or copy/pasted out of a report/PDF. There's often ways to move that data to get it in a better format in the first place but I'll consider that irrelevant for the moment.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you use RETAIN, you can trigger the write to your SAS data set once you find a new stock symbol.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't see how you know which is a new symbol yet, but it seems relatively straightforward to me.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; 
set have;
retain stock price &amp;lt;other variables&amp;gt;;

if &amp;lt;stock&amp;gt; then do; 
output;  *output values currently stored;
call missing(stock, price, &amp;lt;other variables&amp;gt;; *reset all values to missing to prevent carry over of values;
stock=value;   *set to new stock ticker;
end;

else if ... then price=value;
else if ... then &amp;lt;other_variables&amp;gt;=value; *since these are all retained, you get a single record for each 'stock' at the end;



run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jan 2018 21:02:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425625#M281181</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-07T21:02:13Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425626#M281182</link>
      <description>&lt;P&gt;Reeza is right, yeah a simple retain does it like-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have2 end=last;&lt;BR /&gt;retain _line _n;&lt;BR /&gt;if find(line, "stock")&amp;gt;0 then do;&lt;BR /&gt;_n=n;&lt;BR /&gt;_line=line;&lt;BR /&gt;end;&lt;BR /&gt;else if index(line,'$')&amp;gt;0 then do;&lt;BR /&gt;n=_n;&lt;BR /&gt;price=line;&lt;BR /&gt;line=_line;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;drop _:;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jan 2018 21:10:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425626#M281182</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-07T21:10:57Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425671#M281183</link>
      <description>&lt;P&gt;Hi novinosrin,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for the line in your code:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;else if index(line,'$')&amp;gt;0 then do;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if I understand correctly the code looks for the symbol "$", but in my case I have many useless lines that contain the symbol "$", so it is really the relative position of the lines from the line that contains the word "stock" that are important&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2018 03:06:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425671#M281183</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2018-01-08T03:06:48Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425681#M281184</link>
      <description>&lt;P&gt;Yes, your understanding is correct. Good.&lt;/P&gt;&lt;P&gt;Ok, still just a&amp;nbsp; minor change&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set have2 ;&lt;BR /&gt;retain _flag 0 _line _n ;&lt;BR /&gt;if find(line, "stock")&amp;gt;0 then do;&lt;BR /&gt;__n=0;&lt;BR /&gt;_flag=1;&lt;BR /&gt;_n=n;&lt;BR /&gt;_line=line;&lt;BR /&gt;end;&lt;BR /&gt;if _flag then __n+1;&lt;BR /&gt;if __n=3 then do;&lt;BR /&gt;n=_n;&lt;BR /&gt;price=line;&lt;BR /&gt;line=_line;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;drop _: ;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2018 04:02:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/425681#M281184</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-08T04:02:25Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/427491#M281185</link>
      <description>&lt;P&gt;Thanks for the code.&lt;/P&gt;
&lt;P&gt;I managed to include an additional "industry" variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have2 ;
retain _flag 0 _line _n _line2 _n2 ;
if find(line, "stock")&amp;gt;0 then do;
__n=0;
_flag=1;
_n=n;
_line=line;
end;
if _flag then __n+1;
if __n=3 then do;
_n2=n;
_line2=line;
end;
if __n=5 then do;
n=_n;
industry=line;
price=_line2;
line=_line;
output;
end;
drop _: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;so I guess that if I had more variables to create I would just repeat the process&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jan 2018 00:45:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/427491#M281185</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2018-01-14T00:45:28Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/428249#M281186</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12982"&gt;@ilikesas&lt;/a&gt;&amp;nbsp;Sorry mate!, I have been busy with school. Did it work? Are you able to progress and make the necessary changes?&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2018 04:21:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/428249#M281186</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-17T04:21:45Z</dc:date>
    </item>
    <item>
      <title>Re: selecting observations based on their relative position to other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/429480#M281187</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;as you can see I included the additional "industry" variable.&lt;/P&gt;
&lt;P&gt;So I guess that for more variables I will have to repeat the process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jan 2018 17:34:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-observations-based-on-their-relative-position-to-other/m-p/429480#M281187</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2018-01-21T17:34:30Z</dc:date>
    </item>
  </channel>
</rss>

