Using first.variable and last.variables with accumulator variables










-1















I am trying to understand what this program does.



data a;
set b (keep=c d);
by c;
if first.c then e=0;
e+d;
if last.c;
run;


What confuses me is the following.



1), When there is not THEN statements, what does and IF statement do?



2), When there is no value evalueated, what does an IF statement do ? (why would it not say something like if first.c =1 ?)



3), My understanding is that e is an accumulator variable starting from 0, and d is added by iteration. But for some reason e represents the total for all values of c, not d even though we are adding d.



I am thinking that the first two questions will answer my question 3, but I would really appreciate your help.










share|improve this question

















  • 1





    Have you looked at the documentation for the IF statement? Please demonstrate that you have done some basic research before posting questions.

    – user667489
    Nov 13 '18 at 10:10











  • I understand that IF-THEN/ELSE statements can conditionally execute codes. I also know that FIRST.variable is equal to 1 when the first observation of that variable is found and 0 otherwise. It is just confusing that when there are no THEN or ELSE statements that follows. It's like saying, I evaluated if the last variable is 1 or 0, so what?

    – hyg17
    Nov 14 '18 at 3:13











  • Please add an example step that makes input dataset B, and show the output you get from your step and describe how it differs from the output you want. Once you have the example you can play with your code, trying things like commenting out the if last.c; statement and changing if first.c then e=0 to if first.c=1 then e=0, to see if/how they change the output. Also try adding put (c d e)(=); after e+d;

    – Quentin
    Nov 14 '18 at 14:10















-1















I am trying to understand what this program does.



data a;
set b (keep=c d);
by c;
if first.c then e=0;
e+d;
if last.c;
run;


What confuses me is the following.



1), When there is not THEN statements, what does and IF statement do?



2), When there is no value evalueated, what does an IF statement do ? (why would it not say something like if first.c =1 ?)



3), My understanding is that e is an accumulator variable starting from 0, and d is added by iteration. But for some reason e represents the total for all values of c, not d even though we are adding d.



I am thinking that the first two questions will answer my question 3, but I would really appreciate your help.










share|improve this question

















  • 1





    Have you looked at the documentation for the IF statement? Please demonstrate that you have done some basic research before posting questions.

    – user667489
    Nov 13 '18 at 10:10











  • I understand that IF-THEN/ELSE statements can conditionally execute codes. I also know that FIRST.variable is equal to 1 when the first observation of that variable is found and 0 otherwise. It is just confusing that when there are no THEN or ELSE statements that follows. It's like saying, I evaluated if the last variable is 1 or 0, so what?

    – hyg17
    Nov 14 '18 at 3:13











  • Please add an example step that makes input dataset B, and show the output you get from your step and describe how it differs from the output you want. Once you have the example you can play with your code, trying things like commenting out the if last.c; statement and changing if first.c then e=0 to if first.c=1 then e=0, to see if/how they change the output. Also try adding put (c d e)(=); after e+d;

    – Quentin
    Nov 14 '18 at 14:10













-1












-1








-1








I am trying to understand what this program does.



data a;
set b (keep=c d);
by c;
if first.c then e=0;
e+d;
if last.c;
run;


What confuses me is the following.



1), When there is not THEN statements, what does and IF statement do?



2), When there is no value evalueated, what does an IF statement do ? (why would it not say something like if first.c =1 ?)



3), My understanding is that e is an accumulator variable starting from 0, and d is added by iteration. But for some reason e represents the total for all values of c, not d even though we are adding d.



I am thinking that the first two questions will answer my question 3, but I would really appreciate your help.










share|improve this question














I am trying to understand what this program does.



data a;
set b (keep=c d);
by c;
if first.c then e=0;
e+d;
if last.c;
run;


What confuses me is the following.



1), When there is not THEN statements, what does and IF statement do?



2), When there is no value evalueated, what does an IF statement do ? (why would it not say something like if first.c =1 ?)



3), My understanding is that e is an accumulator variable starting from 0, and d is added by iteration. But for some reason e represents the total for all values of c, not d even though we are adding d.



I am thinking that the first two questions will answer my question 3, but I would really appreciate your help.







sas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 7:57









hyg17hyg17

1639




1639







  • 1





    Have you looked at the documentation for the IF statement? Please demonstrate that you have done some basic research before posting questions.

    – user667489
    Nov 13 '18 at 10:10











  • I understand that IF-THEN/ELSE statements can conditionally execute codes. I also know that FIRST.variable is equal to 1 when the first observation of that variable is found and 0 otherwise. It is just confusing that when there are no THEN or ELSE statements that follows. It's like saying, I evaluated if the last variable is 1 or 0, so what?

    – hyg17
    Nov 14 '18 at 3:13











  • Please add an example step that makes input dataset B, and show the output you get from your step and describe how it differs from the output you want. Once you have the example you can play with your code, trying things like commenting out the if last.c; statement and changing if first.c then e=0 to if first.c=1 then e=0, to see if/how they change the output. Also try adding put (c d e)(=); after e+d;

    – Quentin
    Nov 14 '18 at 14:10












  • 1





    Have you looked at the documentation for the IF statement? Please demonstrate that you have done some basic research before posting questions.

    – user667489
    Nov 13 '18 at 10:10











  • I understand that IF-THEN/ELSE statements can conditionally execute codes. I also know that FIRST.variable is equal to 1 when the first observation of that variable is found and 0 otherwise. It is just confusing that when there are no THEN or ELSE statements that follows. It's like saying, I evaluated if the last variable is 1 or 0, so what?

    – hyg17
    Nov 14 '18 at 3:13











  • Please add an example step that makes input dataset B, and show the output you get from your step and describe how it differs from the output you want. Once you have the example you can play with your code, trying things like commenting out the if last.c; statement and changing if first.c then e=0 to if first.c=1 then e=0, to see if/how they change the output. Also try adding put (c d e)(=); after e+d;

    – Quentin
    Nov 14 '18 at 14:10







1




1





Have you looked at the documentation for the IF statement? Please demonstrate that you have done some basic research before posting questions.

– user667489
Nov 13 '18 at 10:10





Have you looked at the documentation for the IF statement? Please demonstrate that you have done some basic research before posting questions.

– user667489
Nov 13 '18 at 10:10













I understand that IF-THEN/ELSE statements can conditionally execute codes. I also know that FIRST.variable is equal to 1 when the first observation of that variable is found and 0 otherwise. It is just confusing that when there are no THEN or ELSE statements that follows. It's like saying, I evaluated if the last variable is 1 or 0, so what?

– hyg17
Nov 14 '18 at 3:13





I understand that IF-THEN/ELSE statements can conditionally execute codes. I also know that FIRST.variable is equal to 1 when the first observation of that variable is found and 0 otherwise. It is just confusing that when there are no THEN or ELSE statements that follows. It's like saying, I evaluated if the last variable is 1 or 0, so what?

– hyg17
Nov 14 '18 at 3:13













Please add an example step that makes input dataset B, and show the output you get from your step and describe how it differs from the output you want. Once you have the example you can play with your code, trying things like commenting out the if last.c; statement and changing if first.c then e=0 to if first.c=1 then e=0, to see if/how they change the output. Also try adding put (c d e)(=); after e+d;

– Quentin
Nov 14 '18 at 14:10





Please add an example step that makes input dataset B, and show the output you get from your step and describe how it differs from the output you want. Once you have the example you can play with your code, trying things like commenting out the if last.c; statement and changing if first.c then e=0 to if first.c=1 then e=0, to see if/how they change the output. Also try adding put (c d e)(=); after e+d;

– Quentin
Nov 14 '18 at 14:10












2 Answers
2






active

oldest

votes


















1














In SAS there are two different if statements - if-then-else and subsetting if. They have similar syntax but they do different things. You have one of each in your code.



This is how it's explained in the documentation:




The subsetting IF statement is equivalent to this IF-THEN statement:



if not (expression) then delete;







share|improve this answer























  • Thank you, I am a beginner so a simple guidance was really all I wanted. That was very helpful.

    – hyg17
    Nov 14 '18 at 19:16


















1














Without a THEN clause the IF statement is a subsetting statement. Basically if the condition is not true the current iteration of the data step stops and control returns to the top to process the next observation.



SAS evaluates any value that is not zero or missing as true. The automatic FIRST. and LAST. variables generated when you use a BY statement will have values of either 1 (true) or 0 (false). The FIRST. variable will be true when on the first observation for the BY group and the LAST. variable will be true when on the last observation for the BY group.



Your data step is getting the sum of the values of D for each value of C.






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%2f53276299%2fusing-first-variable-and-last-variables-with-accumulator-variables%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    In SAS there are two different if statements - if-then-else and subsetting if. They have similar syntax but they do different things. You have one of each in your code.



    This is how it's explained in the documentation:




    The subsetting IF statement is equivalent to this IF-THEN statement:



    if not (expression) then delete;







    share|improve this answer























    • Thank you, I am a beginner so a simple guidance was really all I wanted. That was very helpful.

      – hyg17
      Nov 14 '18 at 19:16















    1














    In SAS there are two different if statements - if-then-else and subsetting if. They have similar syntax but they do different things. You have one of each in your code.



    This is how it's explained in the documentation:




    The subsetting IF statement is equivalent to this IF-THEN statement:



    if not (expression) then delete;







    share|improve this answer























    • Thank you, I am a beginner so a simple guidance was really all I wanted. That was very helpful.

      – hyg17
      Nov 14 '18 at 19:16













    1












    1








    1







    In SAS there are two different if statements - if-then-else and subsetting if. They have similar syntax but they do different things. You have one of each in your code.



    This is how it's explained in the documentation:




    The subsetting IF statement is equivalent to this IF-THEN statement:



    if not (expression) then delete;







    share|improve this answer













    In SAS there are two different if statements - if-then-else and subsetting if. They have similar syntax but they do different things. You have one of each in your code.



    This is how it's explained in the documentation:




    The subsetting IF statement is equivalent to this IF-THEN statement:



    if not (expression) then delete;








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 14 '18 at 10:08









    user667489user667489

    7,93221227




    7,93221227












    • Thank you, I am a beginner so a simple guidance was really all I wanted. That was very helpful.

      – hyg17
      Nov 14 '18 at 19:16

















    • Thank you, I am a beginner so a simple guidance was really all I wanted. That was very helpful.

      – hyg17
      Nov 14 '18 at 19:16
















    Thank you, I am a beginner so a simple guidance was really all I wanted. That was very helpful.

    – hyg17
    Nov 14 '18 at 19:16





    Thank you, I am a beginner so a simple guidance was really all I wanted. That was very helpful.

    – hyg17
    Nov 14 '18 at 19:16













    1














    Without a THEN clause the IF statement is a subsetting statement. Basically if the condition is not true the current iteration of the data step stops and control returns to the top to process the next observation.



    SAS evaluates any value that is not zero or missing as true. The automatic FIRST. and LAST. variables generated when you use a BY statement will have values of either 1 (true) or 0 (false). The FIRST. variable will be true when on the first observation for the BY group and the LAST. variable will be true when on the last observation for the BY group.



    Your data step is getting the sum of the values of D for each value of C.






    share|improve this answer



























      1














      Without a THEN clause the IF statement is a subsetting statement. Basically if the condition is not true the current iteration of the data step stops and control returns to the top to process the next observation.



      SAS evaluates any value that is not zero or missing as true. The automatic FIRST. and LAST. variables generated when you use a BY statement will have values of either 1 (true) or 0 (false). The FIRST. variable will be true when on the first observation for the BY group and the LAST. variable will be true when on the last observation for the BY group.



      Your data step is getting the sum of the values of D for each value of C.






      share|improve this answer

























        1












        1








        1







        Without a THEN clause the IF statement is a subsetting statement. Basically if the condition is not true the current iteration of the data step stops and control returns to the top to process the next observation.



        SAS evaluates any value that is not zero or missing as true. The automatic FIRST. and LAST. variables generated when you use a BY statement will have values of either 1 (true) or 0 (false). The FIRST. variable will be true when on the first observation for the BY group and the LAST. variable will be true when on the last observation for the BY group.



        Your data step is getting the sum of the values of D for each value of C.






        share|improve this answer













        Without a THEN clause the IF statement is a subsetting statement. Basically if the condition is not true the current iteration of the data step stops and control returns to the top to process the next observation.



        SAS evaluates any value that is not zero or missing as true. The automatic FIRST. and LAST. variables generated when you use a BY statement will have values of either 1 (true) or 0 (false). The FIRST. variable will be true when on the first observation for the BY group and the LAST. variable will be true when on the last observation for the BY group.



        Your data step is getting the sum of the values of D for each value of C.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 18:54









        TomTom

        23.2k2718




        23.2k2718



























            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%2f53276299%2fusing-first-variable-and-last-variables-with-accumulator-variables%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo