def export_data(processed_data, all_drugs_sorted, export_path, cost_usd):
    """
    Formats and writes all entries to export file.

    Args:
        processed_data (dictionary): contains all analyzed data. Primary key
            is drug name (string), and primary value is tuple containing
            number of prescribers (integer, index 0) and total cost (float,
            index 1).
        all_drugs_sorted (list of strings): contains all drug names in
            sequential list sorted by drug cost and alphanumeric name.
        export_path (string): path to output file.

    Returns:
        None.
    """
    # Safely opens and closes file for writing
    with open(export_path, 'w') as target_file:
        # Creates header for output file
        target_file.write("drug_name,num_prescriber,total_cost\n")
        # Iterates over whole drug name list
        for drug in all_drugs_sorted:
            # Sets number of prescribers
            num_prescriber = "{}".format(processed_data[drug][0])
            # If True, sets cost display in dollars only
            if cost_usd:
                # Sets total drug cost in dollars
                total_cost = "{:.0f}".format(processed_data[drug][1])            
            # Cost is displayed in dollars and cents
            else:
                # Sets total drug cost in dollars and cents
                total_cost = "{:.2f}".format(processed_data[drug][1])
            # Creates final export string for given drug
            export_text = ",".join([drug,num_prescriber,total_cost])
            #If not the last drug, add line break
            if drug != all_drugs_sorted[-1]:
                # Adds line break to final export string
                export_text = "".join([export_text,"\n"])
            # Writes data entry to output file
            target_file.write(export_text)
    # Completes analyzed data export during file writing
    return None