<?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: Create a new Variable after a particular occurrence in the existing variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777775#M247510</link>
    <description>&lt;P&gt;My expected results are:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;CITY             CITY1
u3jZdcVN	CHICAGO
0nnmdOz	        CHICAGO
7MxtrQjC	ELGIN
wCKbhZM         CHICAGO&lt;/PRE&gt;</description>
    <pubDate>Mon, 01 Nov 2021 20:52:04 GMT</pubDate>
    <dc:creator>prad001</dc:creator>
    <dc:date>2021-11-01T20:52:04Z</dc:date>
    <item>
      <title>Create a new Variable after a particular occurrence in the existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777771#M247506</link>
      <description>&lt;P&gt;From the below example:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to create a new variable called city1 with all the observations after the value "##$$%%**--".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA NEW;
INPUT city $;
cards;
u3jZdcVN
0nnmdOz
7MxtrQjCXR
wCKbhZM
##$$%%**--
city_dec
CHICAGO
CHICAGO
ELGIN
CHICAGO
CHICAGO
;
RUN;&lt;/PRE&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;Prad&lt;/P&gt;</description>
      <pubDate>Mon, 01 Nov 2021 20:38:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777771#M247506</guid>
      <dc:creator>prad001</dc:creator>
      <dc:date>2021-11-01T20:38:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Variable after a particular occurrence in the existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777774#M247509</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/237405"&gt;@prad001&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;From the below example:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to create a new variable called city1 with all the observations after the value "##$$%%**--".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA NEW;
INPUT city $;
cards;
u3jZdcVN
0nnmdOz
7MxtrQjCXR
wCKbhZM
##$$%%**--
city_dec
CHICAGO
CHICAGO
ELGIN
CHICAGO
CHICAGO
;
RUN;&lt;/PRE&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;Prad&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Can you show us what you expect final result to be? Does "all the observations" mean all the cities in one variable for one record? All records? Records after that value? Only the same city?&lt;/P&gt;</description>
      <pubDate>Mon, 01 Nov 2021 20:47:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777774#M247509</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-11-01T20:47:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Variable after a particular occurrence in the existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777775#M247510</link>
      <description>&lt;P&gt;My expected results are:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;CITY             CITY1
u3jZdcVN	CHICAGO
0nnmdOz	        CHICAGO
7MxtrQjC	ELGIN
wCKbhZM         CHICAGO&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Nov 2021 20:52:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777775#M247510</guid>
      <dc:creator>prad001</dc:creator>
      <dc:date>2021-11-01T20:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Variable after a particular occurrence in the existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777793#M247527</link>
      <description>&lt;P&gt;Some details:&lt;/P&gt;
&lt;P&gt;How is the code supposed to know that "city_dec" does not go into the output? Are there any other values that do not get into the output?&lt;/P&gt;
&lt;P&gt;There are 5 observations in your source data after city_dec, why are there only 4 in the output? What is the rule that excludes that instance?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have to assume that your actual data is bit more complex. That means clear rules for inclusion/exclusion are required.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Nov 2021 22:11:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777793#M247527</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-11-01T22:11:13Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Variable after a particular occurrence in the existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777794#M247528</link>
      <description>&lt;P&gt;Hi Ballardw,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am sorry.&lt;/P&gt;
&lt;P&gt;Let me give you the better example. with what is the situation and what I want...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA NEW;
INPUT city $;
cards;
city_enc
u3jZdcVN
0nnmdOz
7MxtrQjCXR
wCKbhZM
##$$%%**--
city_dec
CHICAGO
CHICAGO
ELGIN
CHICAGO
;
RUN;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want the results to be..&lt;/P&gt;
&lt;P&gt;Create two new variables city1 and city2 based out of above city.&lt;/P&gt;
&lt;PRE&gt;city1           city2
u3jZdcVN	CHICAGO
0nnmdOz	        CHICAGO
7MxtrQjC	ELGIN
wCKbhZM         CHICAGO&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Nov 2021 22:25:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777794#M247528</guid>
      <dc:creator>prad001</dc:creator>
      <dc:date>2021-11-01T22:25:14Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Variable after a particular occurrence in the existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777811#M247540</link>
      <description>&lt;P&gt;You still haven't stated the task in a way that fully corresponds to your example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the example it appears that&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;You have two series of records that should be merged.&amp;nbsp; One series (variable CITY1) begins on the second line of data, and the second series (CITY2) begins on the second line of data following the dataline containing&amp;nbsp;&lt;BR /&gt;##$$%%**--&lt;/LI&gt;
&lt;LI&gt;You now have also introduced two new lines to be ignored, city_enc and city_dec, presumably for encoded and non-encoded city values.&amp;nbsp; But you apparently don't want those lines, and you don't even use those lines to guide your variable naming.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;In addition your sample DATA step does not correctly store the DATA NEW values, because the length of CITY defaults to $8.&amp;nbsp; You need to set it to $10 in order to test for the magic line with city="##$$%%**--".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide working code.&amp;nbsp; Help us help you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are a couple ways to do this.&amp;nbsp; (1) from NEW, make a second data set (V2 below) containing only the values for CITY2 (i.e. valid city names afte the magic line).&amp;nbsp; Then merge it with the corresponding values prior to the magic line:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA NEW;
INPUT city $10.;
cards;
city_enc
u3jZdcVN
0nnmdOz
7MxtrQjCXR
wCKbhZM
##$$%%**--
city_dec
CHICAGO
CHICAGO
ELGIN
CHICAGO
RUN;

data v2 (drop=_:) /view=v2;
  set new ;
  retain _after_magic_line;
  if lag(city)='##$$%%**--' then _after_magic_line='YES';
  if _after_magic_line='YES';
run;

data want;
  set new (rename=(city=city1));
  set v2  (rename=(city=city2));
  if _n_&amp;gt;=2;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This uses two DATA steps, the first to create the subset of city2 values, the second to "merge" with the city1 values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I use two SET statements in the DATA WANT step instead of a MERGE statement, because using the two SETs will stop the data step at the end of the shorter sequence produced by a SET (when the CITY2 values are exhausted).&amp;nbsp; &amp;nbsp;Using MERGE would have continued until the longer SET is exhausted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also do this in a single DATA step by using two SET statements with one series offset from the other:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2 (drop=_:);
  set new (rename=(city=city2));
  retain _after_magic_line;
  if lag(city2)='##$$%%**--' then _after_magic_line='YES';
  if _after_magic_line='YES' then set new (rename=(city=city1)); **start at the top**;
  if lag(_after_magic_line)='YES';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Nov 2021 03:06:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777811#M247540</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-11-02T03:06:47Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new Variable after a particular occurrence in the existing variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777927#M247591</link>
      <description>Works perfect.. Thank you mkeintz.</description>
      <pubDate>Tue, 02 Nov 2021 14:25:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-new-Variable-after-a-particular-occurrence-in-the/m-p/777927#M247591</guid>
      <dc:creator>prad001</dc:creator>
      <dc:date>2021-11-02T14:25:23Z</dc:date>
    </item>
  </channel>
</rss>

