Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6005431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:23:46+00:00 2026-05-23T01:23:46+00:00

Suppose I allow user to write his own variable calculation macro using a common

  • 0

Suppose I allow user to write his own variable calculation macro using a common user interface:

%macro calculate(var_name, var_value);
%* Some user-defined calculation;
%mend calculate;

Then in a data step, I can calculate a new variable using the user-defined macro:

data dataset;
    set dataset;
    new_var = %calculate('variable1', variable1); * This doesn't work. It just shows my indication.
run;

Where variable1 is a variable in dataset. Here, I want to pass in the variable name and the actual value of the variable. After the calculation, put the value in new_var.

How can I achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:23:47+00:00Added an answer on May 23, 2026 at 1:23 am

    You can make this work, but you are probably writing the macro incorrectly. You have to remember that SAS macros are essentially text preprocessors: they input the code you have written and output code to feed to SAS [1].

    So here is a simple “addition” macro:

    %macro calculate (var_name, var_value);
      &var_name + &var_value;
    %mend;
    
    data one;
      input a@@;
      b = %calculate(a, 3);
      c = %calculate(a, a);
    cards;
    1 3 -2 4
    ;
    run;
    
    proc print data=one;
    run;
    

    The macro facility will replace the %calculate bits with the code generated by the macro, and SAS will actually see the following:

    %macro calculate (var_name, var_value);
      &var_name + &var_value;
    %mend;
    
    data one;
      input a@@;
      b = a + 3;
      c = a + a;
    cards;
    1 3 -2 4
    ;
    run;
    
    proc print data=one;
    run;
    

    You can see it for yourself on the log file with the options mprint statement

    Unfortunately, this will not cover all potential calculations. You might want to look up PROC FCMP for a way to generate custom functions/subroutines usable in the DATA step.

    [1] I know it is more complicated than that, but you can get far thinking like this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using jQuery UI sortable to allow the user to shuffle the list, but
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose a large composite application built on several foundation components packaged in their own
Suppose I have the following tables: User, Computer and Command. Users and computers may
I suppose it allows for moving changes from one branch to the next but
I'm working on a web-based contest which is supposed to allow anonymous users to
Suppose your git history looks like this: 1 2 3 4 5 1–5 are
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose we have a table A: itemid mark 1 5 2 3 and table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.