I need to know how to get the last chars of some Strings












2















I got this chars
DDSPRJ11
DDSPRJ12
DDSPRJ12
DDRJCT



in the case of the first 3 i want the last 4 chars e the case of the last i want the last 3 chars, how can i get them using substr and get them in the correct order eg: RJ11.










share|improve this question


















  • 1





    Is the condition of how many suffix characters you want dependent on the prefix characters (DDSP vs DDR) ? What should be done for rogue values such as DDSPRJ1729 or DDREXTREME ? Is the focus more concerned with removing the prefix or detecting and extracting a suffix ?

    – Richard
    Nov 20 '18 at 12:52
















2















I got this chars
DDSPRJ11
DDSPRJ12
DDSPRJ12
DDRJCT



in the case of the first 3 i want the last 4 chars e the case of the last i want the last 3 chars, how can i get them using substr and get them in the correct order eg: RJ11.










share|improve this question


















  • 1





    Is the condition of how many suffix characters you want dependent on the prefix characters (DDSP vs DDR) ? What should be done for rogue values such as DDSPRJ1729 or DDREXTREME ? Is the focus more concerned with removing the prefix or detecting and extracting a suffix ?

    – Richard
    Nov 20 '18 at 12:52














2












2








2








I got this chars
DDSPRJ11
DDSPRJ12
DDSPRJ12
DDRJCT



in the case of the first 3 i want the last 4 chars e the case of the last i want the last 3 chars, how can i get them using substr and get them in the correct order eg: RJ11.










share|improve this question














I got this chars
DDSPRJ11
DDSPRJ12
DDSPRJ12
DDRJCT



in the case of the first 3 i want the last 4 chars e the case of the last i want the last 3 chars, how can i get them using substr and get them in the correct order eg: RJ11.







sas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 9:22









manso.andremanso.andre

111




111








  • 1





    Is the condition of how many suffix characters you want dependent on the prefix characters (DDSP vs DDR) ? What should be done for rogue values such as DDSPRJ1729 or DDREXTREME ? Is the focus more concerned with removing the prefix or detecting and extracting a suffix ?

    – Richard
    Nov 20 '18 at 12:52














  • 1





    Is the condition of how many suffix characters you want dependent on the prefix characters (DDSP vs DDR) ? What should be done for rogue values such as DDSPRJ1729 or DDREXTREME ? Is the focus more concerned with removing the prefix or detecting and extracting a suffix ?

    – Richard
    Nov 20 '18 at 12:52








1




1





Is the condition of how many suffix characters you want dependent on the prefix characters (DDSP vs DDR) ? What should be done for rogue values such as DDSPRJ1729 or DDREXTREME ? Is the focus more concerned with removing the prefix or detecting and extracting a suffix ?

– Richard
Nov 20 '18 at 12:52





Is the condition of how many suffix characters you want dependent on the prefix characters (DDSP vs DDR) ? What should be done for rogue values such as DDSPRJ1729 or DDREXTREME ? Is the focus more concerned with removing the prefix or detecting and extracting a suffix ?

– Richard
Nov 20 '18 at 12:52












3 Answers
3






active

oldest

votes


















1














You can do this with regular expression matching using prxchange:



data have;
infile datalines;
input mystr $ @@;
datalines;
DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT
;
run;

data want;
set have;
suffix = prxchange('s/(DDSP|DDR)(.*)/$2/', 1, mystr);
run;





share|improve this answer































    0














    @user667489 is perfect answer if it you have can read all of values separately. if it is in same variable as shown below you can use the same code given by @user667489. and add can add can function. prxnext, can also be used to achieve the same. both examples are shown below



    data have;
    val= "DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT";
    run;

    /* using prxchange with scan*/
    data want;
    set have;
    suffix = prxchange('s/(DDSP|DDR)//', -1, val);
    do i = 1 to countw(suffix,' ');
    newstr= scan(suffix, i);
    output;
    end;
    drop suffix val;
    run;

    /* using prxposn*/

    data want;
    length val1 re $200.;
    set have;
    start = 1;
    stop = length(val);

    re = prxparse('/(DDSP|DDR)/');
    set have;
    call prxnext(re, start, stop, trim(val), position, length);
    do while (position > 0);
    val1 = substr(val, position+length, length);
    call prxnext(re, start, stop, trim(val), position, length);
    output;
    end;
    drop re start stop position length val;
    run;





    share|improve this answer

































      0














      Here is how you can do it in a simple python.
      I assumed that, you want last 4 char of every word except last.



      string_1 = 'DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT'



      list_string = string_1.split()



      new_list =



      for i in range(len(list_string)):



      if i == len(list_string) - 1:



      new_list.append(list_string[i][-3:])


      else:



      new_list.append(list_string[i][-4:])


      print(new_list)



      output:
      ['RJ11', 'RJ12', 'RJ12', 'JCT']






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389789%2fi-need-to-know-how-to-get-the-last-chars-of-some-strings%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        You can do this with regular expression matching using prxchange:



        data have;
        infile datalines;
        input mystr $ @@;
        datalines;
        DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT
        ;
        run;

        data want;
        set have;
        suffix = prxchange('s/(DDSP|DDR)(.*)/$2/', 1, mystr);
        run;





        share|improve this answer




























          1














          You can do this with regular expression matching using prxchange:



          data have;
          infile datalines;
          input mystr $ @@;
          datalines;
          DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT
          ;
          run;

          data want;
          set have;
          suffix = prxchange('s/(DDSP|DDR)(.*)/$2/', 1, mystr);
          run;





          share|improve this answer


























            1












            1








            1







            You can do this with regular expression matching using prxchange:



            data have;
            infile datalines;
            input mystr $ @@;
            datalines;
            DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT
            ;
            run;

            data want;
            set have;
            suffix = prxchange('s/(DDSP|DDR)(.*)/$2/', 1, mystr);
            run;





            share|improve this answer













            You can do this with regular expression matching using prxchange:



            data have;
            infile datalines;
            input mystr $ @@;
            datalines;
            DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT
            ;
            run;

            data want;
            set have;
            suffix = prxchange('s/(DDSP|DDR)(.*)/$2/', 1, mystr);
            run;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 9:50









            user667489user667489

            7,97721227




            7,97721227

























                0














                @user667489 is perfect answer if it you have can read all of values separately. if it is in same variable as shown below you can use the same code given by @user667489. and add can add can function. prxnext, can also be used to achieve the same. both examples are shown below



                data have;
                val= "DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT";
                run;

                /* using prxchange with scan*/
                data want;
                set have;
                suffix = prxchange('s/(DDSP|DDR)//', -1, val);
                do i = 1 to countw(suffix,' ');
                newstr= scan(suffix, i);
                output;
                end;
                drop suffix val;
                run;

                /* using prxposn*/

                data want;
                length val1 re $200.;
                set have;
                start = 1;
                stop = length(val);

                re = prxparse('/(DDSP|DDR)/');
                set have;
                call prxnext(re, start, stop, trim(val), position, length);
                do while (position > 0);
                val1 = substr(val, position+length, length);
                call prxnext(re, start, stop, trim(val), position, length);
                output;
                end;
                drop re start stop position length val;
                run;





                share|improve this answer






























                  0














                  @user667489 is perfect answer if it you have can read all of values separately. if it is in same variable as shown below you can use the same code given by @user667489. and add can add can function. prxnext, can also be used to achieve the same. both examples are shown below



                  data have;
                  val= "DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT";
                  run;

                  /* using prxchange with scan*/
                  data want;
                  set have;
                  suffix = prxchange('s/(DDSP|DDR)//', -1, val);
                  do i = 1 to countw(suffix,' ');
                  newstr= scan(suffix, i);
                  output;
                  end;
                  drop suffix val;
                  run;

                  /* using prxposn*/

                  data want;
                  length val1 re $200.;
                  set have;
                  start = 1;
                  stop = length(val);

                  re = prxparse('/(DDSP|DDR)/');
                  set have;
                  call prxnext(re, start, stop, trim(val), position, length);
                  do while (position > 0);
                  val1 = substr(val, position+length, length);
                  call prxnext(re, start, stop, trim(val), position, length);
                  output;
                  end;
                  drop re start stop position length val;
                  run;





                  share|improve this answer




























                    0












                    0








                    0







                    @user667489 is perfect answer if it you have can read all of values separately. if it is in same variable as shown below you can use the same code given by @user667489. and add can add can function. prxnext, can also be used to achieve the same. both examples are shown below



                    data have;
                    val= "DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT";
                    run;

                    /* using prxchange with scan*/
                    data want;
                    set have;
                    suffix = prxchange('s/(DDSP|DDR)//', -1, val);
                    do i = 1 to countw(suffix,' ');
                    newstr= scan(suffix, i);
                    output;
                    end;
                    drop suffix val;
                    run;

                    /* using prxposn*/

                    data want;
                    length val1 re $200.;
                    set have;
                    start = 1;
                    stop = length(val);

                    re = prxparse('/(DDSP|DDR)/');
                    set have;
                    call prxnext(re, start, stop, trim(val), position, length);
                    do while (position > 0);
                    val1 = substr(val, position+length, length);
                    call prxnext(re, start, stop, trim(val), position, length);
                    output;
                    end;
                    drop re start stop position length val;
                    run;





                    share|improve this answer















                    @user667489 is perfect answer if it you have can read all of values separately. if it is in same variable as shown below you can use the same code given by @user667489. and add can add can function. prxnext, can also be used to achieve the same. both examples are shown below



                    data have;
                    val= "DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT";
                    run;

                    /* using prxchange with scan*/
                    data want;
                    set have;
                    suffix = prxchange('s/(DDSP|DDR)//', -1, val);
                    do i = 1 to countw(suffix,' ');
                    newstr= scan(suffix, i);
                    output;
                    end;
                    drop suffix val;
                    run;

                    /* using prxposn*/

                    data want;
                    length val1 re $200.;
                    set have;
                    start = 1;
                    stop = length(val);

                    re = prxparse('/(DDSP|DDR)/');
                    set have;
                    call prxnext(re, start, stop, trim(val), position, length);
                    do while (position > 0);
                    val1 = substr(val, position+length, length);
                    call prxnext(re, start, stop, trim(val), position, length);
                    output;
                    end;
                    drop re start stop position length val;
                    run;






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 20 '18 at 14:57

























                    answered Nov 20 '18 at 14:47









                    Kiran Kiran

                    2,8553919




                    2,8553919























                        0














                        Here is how you can do it in a simple python.
                        I assumed that, you want last 4 char of every word except last.



                        string_1 = 'DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT'



                        list_string = string_1.split()



                        new_list =



                        for i in range(len(list_string)):



                        if i == len(list_string) - 1:



                        new_list.append(list_string[i][-3:])


                        else:



                        new_list.append(list_string[i][-4:])


                        print(new_list)



                        output:
                        ['RJ11', 'RJ12', 'RJ12', 'JCT']






                        share|improve this answer




























                          0














                          Here is how you can do it in a simple python.
                          I assumed that, you want last 4 char of every word except last.



                          string_1 = 'DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT'



                          list_string = string_1.split()



                          new_list =



                          for i in range(len(list_string)):



                          if i == len(list_string) - 1:



                          new_list.append(list_string[i][-3:])


                          else:



                          new_list.append(list_string[i][-4:])


                          print(new_list)



                          output:
                          ['RJ11', 'RJ12', 'RJ12', 'JCT']






                          share|improve this answer


























                            0












                            0








                            0







                            Here is how you can do it in a simple python.
                            I assumed that, you want last 4 char of every word except last.



                            string_1 = 'DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT'



                            list_string = string_1.split()



                            new_list =



                            for i in range(len(list_string)):



                            if i == len(list_string) - 1:



                            new_list.append(list_string[i][-3:])


                            else:



                            new_list.append(list_string[i][-4:])


                            print(new_list)



                            output:
                            ['RJ11', 'RJ12', 'RJ12', 'JCT']






                            share|improve this answer













                            Here is how you can do it in a simple python.
                            I assumed that, you want last 4 char of every word except last.



                            string_1 = 'DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT'



                            list_string = string_1.split()



                            new_list =



                            for i in range(len(list_string)):



                            if i == len(list_string) - 1:



                            new_list.append(list_string[i][-3:])


                            else:



                            new_list.append(list_string[i][-4:])


                            print(new_list)



                            output:
                            ['RJ11', 'RJ12', 'RJ12', 'JCT']







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 20 '18 at 16:34









                            Gajanan WadekarGajanan Wadekar

                            113




                            113






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389789%2fi-need-to-know-how-to-get-the-last-chars-of-some-strings%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Guess what letter conforming each word

                                Port of Spain

                                Run scheduled task as local user group (not BUILTIN)