<?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 Identifying all first occurrences of a value in a variable in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533465#M6184</link>
    <description>&lt;P&gt;Hello. I would like to flag all first occurrences of a value in Numberofcars when Numberofcars is &amp;gt;1 by participant id. I have data that looks like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID Numberofcars WantFlag HaveFlag&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 1&amp;nbsp; . .&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 1 . .&lt;/P&gt;&lt;P&gt;1 2 1 1&lt;/P&gt;&lt;P&gt;1 3 1 1&lt;/P&gt;&lt;P&gt;1 3&amp;nbsp; . 1&lt;/P&gt;&lt;P&gt;2 1 . .&lt;/P&gt;&lt;P&gt;2 2 1 1&lt;/P&gt;&lt;P&gt;2 2 . 1&lt;/P&gt;&lt;P&gt;2 3 1 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wrote this code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=have out=want;&lt;/P&gt;&lt;P&gt;by ID Numberofcars;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set want;&lt;/P&gt;&lt;P&gt;retain test;&lt;/P&gt;&lt;P&gt;if first.ID &amp;amp; first.Numberofcars then test=0;&lt;/P&gt;&lt;P&gt;haveflag=0;&lt;/P&gt;&lt;P&gt;if Numberofcars&amp;gt;1 &amp;amp; test=0 then do;&lt;/P&gt;&lt;P&gt;haveflag=1;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see in haveflag (which is generated from the above code), it equals 1 for ALL occurrences of Numberofcars=3. I only want it to do this for the first one and any other unique values of Numberofcars that does not equal 1.&lt;/P&gt;</description>
    <pubDate>Thu, 07 Feb 2019 03:03:46 GMT</pubDate>
    <dc:creator>Wafflecakes</dc:creator>
    <dc:date>2019-02-07T03:03:46Z</dc:date>
    <item>
      <title>Identifying all first occurrences of a value in a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533465#M6184</link>
      <description>&lt;P&gt;Hello. I would like to flag all first occurrences of a value in Numberofcars when Numberofcars is &amp;gt;1 by participant id. I have data that looks like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID Numberofcars WantFlag HaveFlag&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 1&amp;nbsp; . .&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 1 . .&lt;/P&gt;&lt;P&gt;1 2 1 1&lt;/P&gt;&lt;P&gt;1 3 1 1&lt;/P&gt;&lt;P&gt;1 3&amp;nbsp; . 1&lt;/P&gt;&lt;P&gt;2 1 . .&lt;/P&gt;&lt;P&gt;2 2 1 1&lt;/P&gt;&lt;P&gt;2 2 . 1&lt;/P&gt;&lt;P&gt;2 3 1 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wrote this code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=have out=want;&lt;/P&gt;&lt;P&gt;by ID Numberofcars;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set want;&lt;/P&gt;&lt;P&gt;retain test;&lt;/P&gt;&lt;P&gt;if first.ID &amp;amp; first.Numberofcars then test=0;&lt;/P&gt;&lt;P&gt;haveflag=0;&lt;/P&gt;&lt;P&gt;if Numberofcars&amp;gt;1 &amp;amp; test=0 then do;&lt;/P&gt;&lt;P&gt;haveflag=1;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see in haveflag (which is generated from the above code), it equals 1 for ALL occurrences of Numberofcars=3. I only want it to do this for the first one and any other unique values of Numberofcars that does not equal 1.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 03:03:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533465#M6184</guid>
      <dc:creator>Wafflecakes</dc:creator>
      <dc:date>2019-02-07T03:03:46Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying all first occurrences of a value in a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533470#M6185</link>
      <description>&lt;P&gt;I'm still unsure what you want?&amp;nbsp; Can you post "have" and "want" data steps, using data lines so we can just c&amp;amp;p and run it?&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 02:48:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533470#M6185</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-02-07T02:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying all first occurrences of a value in a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533474#M6186</link>
      <description>Hi Scott. I edited my original post. Does it make more sense? I don't know what the "want" data steps should look like...</description>
      <pubDate>Thu, 07 Feb 2019 03:04:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533474#M6186</guid>
      <dc:creator>Wafflecakes</dc:creator>
      <dc:date>2019-02-07T03:04:29Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying all first occurrences of a value in a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533479#M6187</link>
      <description>Add to your knowledge base:  sorting the data is not enough to create first. and last. variables in a DATA step.  You also must add a BY statement to the DATA step.&lt;BR /&gt;&lt;BR /&gt;This should work;&lt;BR /&gt;&lt;BR /&gt;Proc sort data=have;&lt;BR /&gt;By ID numberofcars;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;Data want;&lt;BR /&gt;Set have,&lt;BR /&gt;By ID numberofcars;&lt;BR /&gt;If first.numberofcars and &lt;BR /&gt;Numberofcars &amp;gt; 1 then flag=1;&lt;BR /&gt;Run;&lt;BR /&gt;</description>
      <pubDate>Thu, 07 Feb 2019 03:24:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533479#M6187</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-02-07T03:24:23Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying all first occurrences of a value in a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533481#M6188</link>
      <description>Ahhh sorry. I forgot to write that in the original post. Still, it works now! Thanks Astounding.</description>
      <pubDate>Thu, 07 Feb 2019 03:31:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533481#M6188</guid>
      <dc:creator>Wafflecakes</dc:creator>
      <dc:date>2019-02-07T03:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying all first occurrences of a value in a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533486#M6189</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;I'm still unsure what you want?&amp;nbsp; Can you post "have" and "want" data steps, using data lines so we can just c&amp;amp;p and run it?&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;You've got your answer, but to explain my post...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I cut-and-paste this into SAS:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ID Numberofcars WantFlag HaveFlag 

1 1  . . 

1 1 . .

1 2 1 1

1 3 1 1

1 3  . 1

2 1 . .

2 2 1 1

2 2 . 1

2 3 1 1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I get an error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I cut-and-paste this into SAS, it works:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   length ID Numberofcars WantFlag HaveFlag 8;
   input ID Numberofcars WantFlag HaveFlag 8;
   datalines; 
1 1  . . 
1 1 . .
1 2 1 1
1 3 1 1
1 3  . 1
2 1 . .
2 2 1 1
2 2 . 1
2 3 1 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Same for "want" - use datalines to show me what you want as your desired output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Put your SAS code in a SAS block - click the "running man" icon in the Rich Text editor.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Make it easy for me to help you.&amp;nbsp; It's a slow day otherwise I wouldn't have bothered...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm glad Astounding figured out what you wanted.&amp;nbsp; He's smarter than me &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 03:44:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533486#M6189</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-02-07T03:44:35Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying all first occurrences of a value in a variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533934#M6246</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You've always provided good and thoughtful answers.&amp;nbsp; It's a pleasure to see you on the board, as time allows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 14:40:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Identifying-all-first-occurrences-of-a-value-in-a-variable/m-p/533934#M6246</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-02-08T14:40:05Z</dc:date>
    </item>
  </channel>
</rss>

