Reusing logic to set string variables I would like to set some variables for every entry within a section ('courses'), and then use those variables in loops that are filtering by entry types and categories.

Some simplified markup:

&lt;h2&gt;Required Courses&lt;/h2&gt;
&lt;ul&gt;
  {% for entry in craft.entries.section('courses').type('safsCourse') %}
    &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ courseName }}&lt;/a&gt;&lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;

&lt;h2&gt;Elective Courses&lt;/h2&gt;

    &lt;h3&gt;At least one agricultural science course&lt;/h3&gt;
    &lt;ul&gt;
      {% for entry in craft.entries.relatedTo(65) %}
        &lt;li&gt;&lt;a href="{{ msuLink }}"&gt;{{ courseName }}&lt;/a&gt;&lt;/li&gt;
      {% endfor %}
    &lt;/ul&gt;

    &lt;h3&gt;At least one social science course (3cr)&lt;/h3&gt;
    &lt;ul&gt;
      {% for entry in craft.entries.relatedTo(66) %}
        &lt;li&gt;a href="{{ msuLink }}"&gt;{{ courseName }}&lt;/a&gt;&lt;/li&gt;
      {% endfor %}
    &lt;/ul&gt;


As you can see, I'm using the variables msulink and courseName. The code works as intended if I include the following within each {% for entry... %} block:

{% set courseName = entry.deptCode ~ ' ' ~ entry.courseNumber ~ ': ' ~ entry.title %}
{% set msuLink = 'https://reg.msu.edu/Courses/Request.aspx?SubjectCode=' ~
    entry.deptCode ~ '&amp;CourseNumber=' ~ entry.courseNumber ~ '#Results' %}


But I don't want to set those variables three identical times. 

How can I set those variables once and use them when I'm pulling various subsets of my Course section entries?
I would use macros. A Twig macro is basically a function, that can take arguments and render HTML.

Macros can be defined inline (i.e. in the same template where it's used) or in a separate file (better if you're writing a generic macro that'll possibly be used in several different templates).

Here's how your template could look, using macros:

{% macro courseName(entry) %}
    {{ entry.deptCode ~ ' ' ~ entry.courseNumber ~ ': ' ~ entry.title }}
{% endmacro %}

{% macro msuLink(entry) %}
    {{ 'https://reg.msu.edu/Courses/Request.aspx?SubjectCode=' ~
entry.deptCode ~ '&amp;CourseNumber=' ~ entry.courseNumber ~ '#Results' }}
{% endmacro %}

{% import _self as helpers %}

&lt;h2&gt;Required Courses&lt;/h2&gt;
&lt;ul&gt;
    {% for entry in craft.entries.section('courses').type('safsCourse') %}
        &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ helpers.courseName(entry) }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;

&lt;h2&gt;Elective Courses&lt;/h2&gt;

&lt;h3&gt;At least one agricultural science course&lt;/h3&gt;
&lt;ul&gt;
    {% for entry in craft.entries.relatedTo(65) %}
        &lt;li&gt;&lt;a href="{{ helpers.msuLink(entry) }}"&gt;{{ helpers.courseName(entry) }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;

...


If you're wondering about the {% import _self as helpers %} directive below the actual macros, the {% import %} tag is neccessary to actually expose your macros to your template (even if the macros are declared in the same file), and the "helpers" part is just a contextual variable that could be named anything you want (as semantic as possible is always a good thing).