return and assign global value from macro function in data setp












0















Not quite sure the best way to do the following - I want to call a macro function from within a DATA step, passing as an argument the value from an existing column, and assign the value generated by the macro function to a new column. I'm open to any approach - PROC SQL, CALL EXECUTE, etc.



%macro simple(x=);
%global value;
%let value = %sysfunc(intnx(month, %sysfunc(today()), &x, same));
%put &value;
%mend simple;
%simple(x=1);

data have;
do x = 1 to 15;
output;
end;
run;

data want;
data have;
newdate = %simple(x);
run;









share|improve this question




















  • 1





    Can you use PROC FCMP instead? DOSUBL may return in same session, but CALL EXECUTE is after the data step ends, so won't do what you want.

    – Reeza
    Nov 20 '18 at 19:35











  • Hi again! If you can provide an example that would help - I will also look into PROC FCMP in the meantime

    – CPak
    Nov 20 '18 at 19:36













  • Is your example above representative? Is that function actually what you're trying to do?

    – Reeza
    Nov 20 '18 at 19:41











  • It's simpler for sure but I think it captures my problem. I can try to update my post if the solution seems to not address my real world case.

    – CPak
    Nov 20 '18 at 19:47






  • 1





    Don't use data where you mean set. Probably useful to add a format to your new variable so that it prints the dates in a human readable format.

    – Tom
    Nov 20 '18 at 20:35


















0















Not quite sure the best way to do the following - I want to call a macro function from within a DATA step, passing as an argument the value from an existing column, and assign the value generated by the macro function to a new column. I'm open to any approach - PROC SQL, CALL EXECUTE, etc.



%macro simple(x=);
%global value;
%let value = %sysfunc(intnx(month, %sysfunc(today()), &x, same));
%put &value;
%mend simple;
%simple(x=1);

data have;
do x = 1 to 15;
output;
end;
run;

data want;
data have;
newdate = %simple(x);
run;









share|improve this question




















  • 1





    Can you use PROC FCMP instead? DOSUBL may return in same session, but CALL EXECUTE is after the data step ends, so won't do what you want.

    – Reeza
    Nov 20 '18 at 19:35











  • Hi again! If you can provide an example that would help - I will also look into PROC FCMP in the meantime

    – CPak
    Nov 20 '18 at 19:36













  • Is your example above representative? Is that function actually what you're trying to do?

    – Reeza
    Nov 20 '18 at 19:41











  • It's simpler for sure but I think it captures my problem. I can try to update my post if the solution seems to not address my real world case.

    – CPak
    Nov 20 '18 at 19:47






  • 1





    Don't use data where you mean set. Probably useful to add a format to your new variable so that it prints the dates in a human readable format.

    – Tom
    Nov 20 '18 at 20:35
















0












0








0








Not quite sure the best way to do the following - I want to call a macro function from within a DATA step, passing as an argument the value from an existing column, and assign the value generated by the macro function to a new column. I'm open to any approach - PROC SQL, CALL EXECUTE, etc.



%macro simple(x=);
%global value;
%let value = %sysfunc(intnx(month, %sysfunc(today()), &x, same));
%put &value;
%mend simple;
%simple(x=1);

data have;
do x = 1 to 15;
output;
end;
run;

data want;
data have;
newdate = %simple(x);
run;









share|improve this question
















Not quite sure the best way to do the following - I want to call a macro function from within a DATA step, passing as an argument the value from an existing column, and assign the value generated by the macro function to a new column. I'm open to any approach - PROC SQL, CALL EXECUTE, etc.



%macro simple(x=);
%global value;
%let value = %sysfunc(intnx(month, %sysfunc(today()), &x, same));
%put &value;
%mend simple;
%simple(x=1);

data have;
do x = 1 to 15;
output;
end;
run;

data want;
data have;
newdate = %simple(x);
run;






sas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 20:37







CPak

















asked Nov 20 '18 at 19:26









CPakCPak

9,4941726




9,4941726








  • 1





    Can you use PROC FCMP instead? DOSUBL may return in same session, but CALL EXECUTE is after the data step ends, so won't do what you want.

    – Reeza
    Nov 20 '18 at 19:35











  • Hi again! If you can provide an example that would help - I will also look into PROC FCMP in the meantime

    – CPak
    Nov 20 '18 at 19:36













  • Is your example above representative? Is that function actually what you're trying to do?

    – Reeza
    Nov 20 '18 at 19:41











  • It's simpler for sure but I think it captures my problem. I can try to update my post if the solution seems to not address my real world case.

    – CPak
    Nov 20 '18 at 19:47






  • 1





    Don't use data where you mean set. Probably useful to add a format to your new variable so that it prints the dates in a human readable format.

    – Tom
    Nov 20 '18 at 20:35
















  • 1





    Can you use PROC FCMP instead? DOSUBL may return in same session, but CALL EXECUTE is after the data step ends, so won't do what you want.

    – Reeza
    Nov 20 '18 at 19:35











  • Hi again! If you can provide an example that would help - I will also look into PROC FCMP in the meantime

    – CPak
    Nov 20 '18 at 19:36













  • Is your example above representative? Is that function actually what you're trying to do?

    – Reeza
    Nov 20 '18 at 19:41











  • It's simpler for sure but I think it captures my problem. I can try to update my post if the solution seems to not address my real world case.

    – CPak
    Nov 20 '18 at 19:47






  • 1





    Don't use data where you mean set. Probably useful to add a format to your new variable so that it prints the dates in a human readable format.

    – Tom
    Nov 20 '18 at 20:35










1




1





Can you use PROC FCMP instead? DOSUBL may return in same session, but CALL EXECUTE is after the data step ends, so won't do what you want.

– Reeza
Nov 20 '18 at 19:35





Can you use PROC FCMP instead? DOSUBL may return in same session, but CALL EXECUTE is after the data step ends, so won't do what you want.

– Reeza
Nov 20 '18 at 19:35













Hi again! If you can provide an example that would help - I will also look into PROC FCMP in the meantime

– CPak
Nov 20 '18 at 19:36







Hi again! If you can provide an example that would help - I will also look into PROC FCMP in the meantime

– CPak
Nov 20 '18 at 19:36















Is your example above representative? Is that function actually what you're trying to do?

– Reeza
Nov 20 '18 at 19:41





Is your example above representative? Is that function actually what you're trying to do?

– Reeza
Nov 20 '18 at 19:41













It's simpler for sure but I think it captures my problem. I can try to update my post if the solution seems to not address my real world case.

– CPak
Nov 20 '18 at 19:47





It's simpler for sure but I think it captures my problem. I can try to update my post if the solution seems to not address my real world case.

– CPak
Nov 20 '18 at 19:47




1




1





Don't use data where you mean set. Probably useful to add a format to your new variable so that it prints the dates in a human readable format.

– Tom
Nov 20 '18 at 20:35







Don't use data where you mean set. Probably useful to add a format to your new variable so that it prints the dates in a human readable format.

– Tom
Nov 20 '18 at 20:35














2 Answers
2






active

oldest

votes


















1














Notice the changes to %SIMPLE and where it is called. RESOLVE works for this case but

%COMPLEX :-) will not. Also this is better done without calling a macro it can all be done in data step. I hope you have a better use for this than job security.



%macro simple(x=);
%sysfunc(intnx(month, %sysfunc(today()), &x, same))
%mend simple;

%put NOTE: %simple(x=1);

data have;
do x = 1 to 15;
output;
end;
run;

data want;
set have;
newdate = input(resolve(cats('%simple(x=',x,')')),F16.);
format newdate date9.;
run;
proc print;
run;





share|improve this answer
























  • Do you mind also showing how this could be done in DATA step? I can't seem to make that work either - I can show what I tried in my post if necessary.

    – CPak
    Nov 20 '18 at 20:11











  • input(resolve(…)) is a coding pattern I wasn't aware of - I'm sure I'll abuse it needlessly

    – CPak
    Nov 20 '18 at 20:14











  • To call intnx() in a data step instead of in macro code just don't wrap it in %sysfunc().

    – Tom
    Nov 20 '18 at 20:26











  • Hi @Tom - I've edited my post with sample code (at bottom) - it's not giving me output I expect...

    – CPak
    Nov 20 '18 at 20:31











  • Fix the syntax error in your posted data step.

    – Tom
    Nov 20 '18 at 20:37



















3














If you want to use the macro in the middle of a statement like:



newdate = %simple(x);


then the macro can only emit part of a statement. That is no semi-colons.



In this case if you define the macro to generate the call to INTNX() then you could use it that way in an assignment statement.



%macro simple(value);
intnx('month',%sysfunc(today()),&value,'same')
%mend simple;


So if today() is 20NOV2018 then you are effectively running this valid SAS statement in your data step.



newdate = intnx('month',21508,x,'same') ;


Notice how the name of the variable X is what is passed to macro and what is used in the generated piece of SAS code.



Of course your macro is now useless if you wanted to try to use it outside of a SAS statement.






share|improve this answer
























  • That's cool! In real life, I'm using a multi line macro function so having both options is great

    – CPak
    Nov 20 '18 at 20:52











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%2f53400167%2freturn-and-assign-global-value-from-macro-function-in-data-setp%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














Notice the changes to %SIMPLE and where it is called. RESOLVE works for this case but

%COMPLEX :-) will not. Also this is better done without calling a macro it can all be done in data step. I hope you have a better use for this than job security.



%macro simple(x=);
%sysfunc(intnx(month, %sysfunc(today()), &x, same))
%mend simple;

%put NOTE: %simple(x=1);

data have;
do x = 1 to 15;
output;
end;
run;

data want;
set have;
newdate = input(resolve(cats('%simple(x=',x,')')),F16.);
format newdate date9.;
run;
proc print;
run;





share|improve this answer
























  • Do you mind also showing how this could be done in DATA step? I can't seem to make that work either - I can show what I tried in my post if necessary.

    – CPak
    Nov 20 '18 at 20:11











  • input(resolve(…)) is a coding pattern I wasn't aware of - I'm sure I'll abuse it needlessly

    – CPak
    Nov 20 '18 at 20:14











  • To call intnx() in a data step instead of in macro code just don't wrap it in %sysfunc().

    – Tom
    Nov 20 '18 at 20:26











  • Hi @Tom - I've edited my post with sample code (at bottom) - it's not giving me output I expect...

    – CPak
    Nov 20 '18 at 20:31











  • Fix the syntax error in your posted data step.

    – Tom
    Nov 20 '18 at 20:37
















1














Notice the changes to %SIMPLE and where it is called. RESOLVE works for this case but

%COMPLEX :-) will not. Also this is better done without calling a macro it can all be done in data step. I hope you have a better use for this than job security.



%macro simple(x=);
%sysfunc(intnx(month, %sysfunc(today()), &x, same))
%mend simple;

%put NOTE: %simple(x=1);

data have;
do x = 1 to 15;
output;
end;
run;

data want;
set have;
newdate = input(resolve(cats('%simple(x=',x,')')),F16.);
format newdate date9.;
run;
proc print;
run;





share|improve this answer
























  • Do you mind also showing how this could be done in DATA step? I can't seem to make that work either - I can show what I tried in my post if necessary.

    – CPak
    Nov 20 '18 at 20:11











  • input(resolve(…)) is a coding pattern I wasn't aware of - I'm sure I'll abuse it needlessly

    – CPak
    Nov 20 '18 at 20:14











  • To call intnx() in a data step instead of in macro code just don't wrap it in %sysfunc().

    – Tom
    Nov 20 '18 at 20:26











  • Hi @Tom - I've edited my post with sample code (at bottom) - it's not giving me output I expect...

    – CPak
    Nov 20 '18 at 20:31











  • Fix the syntax error in your posted data step.

    – Tom
    Nov 20 '18 at 20:37














1












1








1







Notice the changes to %SIMPLE and where it is called. RESOLVE works for this case but

%COMPLEX :-) will not. Also this is better done without calling a macro it can all be done in data step. I hope you have a better use for this than job security.



%macro simple(x=);
%sysfunc(intnx(month, %sysfunc(today()), &x, same))
%mend simple;

%put NOTE: %simple(x=1);

data have;
do x = 1 to 15;
output;
end;
run;

data want;
set have;
newdate = input(resolve(cats('%simple(x=',x,')')),F16.);
format newdate date9.;
run;
proc print;
run;





share|improve this answer













Notice the changes to %SIMPLE and where it is called. RESOLVE works for this case but

%COMPLEX :-) will not. Also this is better done without calling a macro it can all be done in data step. I hope you have a better use for this than job security.



%macro simple(x=);
%sysfunc(intnx(month, %sysfunc(today()), &x, same))
%mend simple;

%put NOTE: %simple(x=1);

data have;
do x = 1 to 15;
output;
end;
run;

data want;
set have;
newdate = input(resolve(cats('%simple(x=',x,')')),F16.);
format newdate date9.;
run;
proc print;
run;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 20:04









data _null_data _null_

5,472610




5,472610













  • Do you mind also showing how this could be done in DATA step? I can't seem to make that work either - I can show what I tried in my post if necessary.

    – CPak
    Nov 20 '18 at 20:11











  • input(resolve(…)) is a coding pattern I wasn't aware of - I'm sure I'll abuse it needlessly

    – CPak
    Nov 20 '18 at 20:14











  • To call intnx() in a data step instead of in macro code just don't wrap it in %sysfunc().

    – Tom
    Nov 20 '18 at 20:26











  • Hi @Tom - I've edited my post with sample code (at bottom) - it's not giving me output I expect...

    – CPak
    Nov 20 '18 at 20:31











  • Fix the syntax error in your posted data step.

    – Tom
    Nov 20 '18 at 20:37



















  • Do you mind also showing how this could be done in DATA step? I can't seem to make that work either - I can show what I tried in my post if necessary.

    – CPak
    Nov 20 '18 at 20:11











  • input(resolve(…)) is a coding pattern I wasn't aware of - I'm sure I'll abuse it needlessly

    – CPak
    Nov 20 '18 at 20:14











  • To call intnx() in a data step instead of in macro code just don't wrap it in %sysfunc().

    – Tom
    Nov 20 '18 at 20:26











  • Hi @Tom - I've edited my post with sample code (at bottom) - it's not giving me output I expect...

    – CPak
    Nov 20 '18 at 20:31











  • Fix the syntax error in your posted data step.

    – Tom
    Nov 20 '18 at 20:37

















Do you mind also showing how this could be done in DATA step? I can't seem to make that work either - I can show what I tried in my post if necessary.

– CPak
Nov 20 '18 at 20:11





Do you mind also showing how this could be done in DATA step? I can't seem to make that work either - I can show what I tried in my post if necessary.

– CPak
Nov 20 '18 at 20:11













input(resolve(…)) is a coding pattern I wasn't aware of - I'm sure I'll abuse it needlessly

– CPak
Nov 20 '18 at 20:14





input(resolve(…)) is a coding pattern I wasn't aware of - I'm sure I'll abuse it needlessly

– CPak
Nov 20 '18 at 20:14













To call intnx() in a data step instead of in macro code just don't wrap it in %sysfunc().

– Tom
Nov 20 '18 at 20:26





To call intnx() in a data step instead of in macro code just don't wrap it in %sysfunc().

– Tom
Nov 20 '18 at 20:26













Hi @Tom - I've edited my post with sample code (at bottom) - it's not giving me output I expect...

– CPak
Nov 20 '18 at 20:31





Hi @Tom - I've edited my post with sample code (at bottom) - it's not giving me output I expect...

– CPak
Nov 20 '18 at 20:31













Fix the syntax error in your posted data step.

– Tom
Nov 20 '18 at 20:37





Fix the syntax error in your posted data step.

– Tom
Nov 20 '18 at 20:37













3














If you want to use the macro in the middle of a statement like:



newdate = %simple(x);


then the macro can only emit part of a statement. That is no semi-colons.



In this case if you define the macro to generate the call to INTNX() then you could use it that way in an assignment statement.



%macro simple(value);
intnx('month',%sysfunc(today()),&value,'same')
%mend simple;


So if today() is 20NOV2018 then you are effectively running this valid SAS statement in your data step.



newdate = intnx('month',21508,x,'same') ;


Notice how the name of the variable X is what is passed to macro and what is used in the generated piece of SAS code.



Of course your macro is now useless if you wanted to try to use it outside of a SAS statement.






share|improve this answer
























  • That's cool! In real life, I'm using a multi line macro function so having both options is great

    – CPak
    Nov 20 '18 at 20:52
















3














If you want to use the macro in the middle of a statement like:



newdate = %simple(x);


then the macro can only emit part of a statement. That is no semi-colons.



In this case if you define the macro to generate the call to INTNX() then you could use it that way in an assignment statement.



%macro simple(value);
intnx('month',%sysfunc(today()),&value,'same')
%mend simple;


So if today() is 20NOV2018 then you are effectively running this valid SAS statement in your data step.



newdate = intnx('month',21508,x,'same') ;


Notice how the name of the variable X is what is passed to macro and what is used in the generated piece of SAS code.



Of course your macro is now useless if you wanted to try to use it outside of a SAS statement.






share|improve this answer
























  • That's cool! In real life, I'm using a multi line macro function so having both options is great

    – CPak
    Nov 20 '18 at 20:52














3












3








3







If you want to use the macro in the middle of a statement like:



newdate = %simple(x);


then the macro can only emit part of a statement. That is no semi-colons.



In this case if you define the macro to generate the call to INTNX() then you could use it that way in an assignment statement.



%macro simple(value);
intnx('month',%sysfunc(today()),&value,'same')
%mend simple;


So if today() is 20NOV2018 then you are effectively running this valid SAS statement in your data step.



newdate = intnx('month',21508,x,'same') ;


Notice how the name of the variable X is what is passed to macro and what is used in the generated piece of SAS code.



Of course your macro is now useless if you wanted to try to use it outside of a SAS statement.






share|improve this answer













If you want to use the macro in the middle of a statement like:



newdate = %simple(x);


then the macro can only emit part of a statement. That is no semi-colons.



In this case if you define the macro to generate the call to INTNX() then you could use it that way in an assignment statement.



%macro simple(value);
intnx('month',%sysfunc(today()),&value,'same')
%mend simple;


So if today() is 20NOV2018 then you are effectively running this valid SAS statement in your data step.



newdate = intnx('month',21508,x,'same') ;


Notice how the name of the variable X is what is passed to macro and what is used in the generated piece of SAS code.



Of course your macro is now useless if you wanted to try to use it outside of a SAS statement.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 20:47









TomTom

23.7k2718




23.7k2718













  • That's cool! In real life, I'm using a multi line macro function so having both options is great

    – CPak
    Nov 20 '18 at 20:52



















  • That's cool! In real life, I'm using a multi line macro function so having both options is great

    – CPak
    Nov 20 '18 at 20:52

















That's cool! In real life, I'm using a multi line macro function so having both options is great

– CPak
Nov 20 '18 at 20:52





That's cool! In real life, I'm using a multi line macro function so having both options is great

– CPak
Nov 20 '18 at 20:52


















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%2f53400167%2freturn-and-assign-global-value-from-macro-function-in-data-setp%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

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?