BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
akash1088
Obsidian | Level 7
Hi All,

I have stored Macro in data set I want run that macro .
Is there any way to run or compile that macro from SAS dataset.
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you have the source code for a macro definition in character variable(s) in a SAS dataset you have to get SAS to treat that as code.

 

Say you have a dataset named HAVE with a variable named LINE that has the code as multiple lines of text.

I think the easiest is to just write it to file and then %INCLUDE that file.

filename macdef temp;
data _null_;
  set have;
  file macdef;
  put line;
run;
%include macdef;

 

View solution in original post

12 REPLIES 12
PaigeMiller
Diamond | Level 26

Can you give us an example of what you are talking about?

 

Do you mean something like this?

 

%include "g:\myfolder\mymacro.sas";
data abc;
    %mymacro
run;

This works if %MYMACRO produces valid data step code.

--
Paige Miller
akash1088
Obsidian | Level 7

I have already stored a macro code in a table now I have run that code.

akash1088
Obsidian | Level 7

I have already stored a macro in dataset now I want to run.

akash1088
Obsidian | Level 7
Data m;
Length a $100;
a='%Macro Test;'; output;
a='data m; set sashelp.class; run;'; output;
a='%mend;'; output;
a='%Test'; output;
Run;

Let's suppose I have stored this macro in dataset now I have to call this macro.Is there any way to do that.

 

 

andreas_lds
Jade | Level 19

@akash1088 wrote:
Data m;
Length a $100;
a='%Macro Test;'; output;
a='data m; set sashelp.class; run;'; output;
a='%mend;'; output;
a='%Test'; output;
Run;

Let's suppose I have stored this macro in dataset now I have to call this macro.Is there any way to do that.

 

 


I have never ever seen such an interesting approach to macro programming and after thinking about it, i can only advise: stop this right now! Macros are text and storing them in text-files is the only pain-free way to deal with them.

data_null__
Jade | Level 19

@akash1088 wrote:
Data m;
Length a $100;
a='%Macro Test;'; output;
a='data m; set sashelp.class; run;'; output;
a='%mend;'; output;
a='%Test'; output;
Run;

Let's suppose I have stored this macro in dataset now I have to call this macro.Is there any way to do that.

 

 

You should take @andreas_lds advice.

 

It does seem to work using CALL EXECUTE and %NRSTR

 

75   %sysmacdelete test;
76   options mprint=1;
77   Data m;
78      Length a $100;
79      a='%Macro Test;'; output;
80      a='data m; set sashelp.class; put _all_; run;'; output;
81      a='%mend;'; output;
82      a='%Test'; output;
83      Run;

NOTE: The data set WORK.M has 4 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


84   proc print;
85      run;

NOTE: There were 4 observations read from the data set WORK.M.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


86   data _null_;
87      set m;
88      call execute(cats('%nrstr(',a,')'));
89      run;

NOTE: There were 4 observations read from the data set WORK.M.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


NOTE: CALL EXECUTE generated line.
1   + %Macro Test;
2   + data m; set sashelp.class; put _all_; run;
3   + %mend;
4   + %Test
MPRINT(TEST):   data m;
MPRINT(TEST):   set sashelp.class;
MPRINT(TEST):   put _all_;
MPRINT(TEST):   run;

Name=Alfred Sex=M Age=14 Height=69 Weight=112.5 _ERROR_=0 _N_=1
Name=Alice Sex=F Age=13 Height=56.5 Weight=84 _ERROR_=0 _N_=2
Name=Barbara Sex=F Age=13 Height=65.3 Weight=98 _ERROR_=0 _N_=3
Name=Carol Sex=F Age=14 Height=62.8 Weight=102.5 _ERROR_=0 _N_=4
Name=Henry Sex=M Age=14 Height=63.5 Weight=102.5 _ERROR_=0 _N_=5
Name=James Sex=M Age=12 Height=57.3 Weight=83 _ERROR_=0 _N_=6
Name=Jane Sex=F Age=12 Height=59.8 Weight=84.5 _ERROR_=0 _N_=7
Name=Janet Sex=F Age=15 Height=62.5 Weight=112.5 _ERROR_=0 _N_=8
Name=Jeffrey Sex=M Age=13 Height=62.5 Weight=84 _ERROR_=0 _N_=9
Name=John Sex=M Age=12 Height=59 Weight=99.5 _ERROR_=0 _N_=10
Name=Joyce Sex=F Age=11 Height=51.3 Weight=50.5 _ERROR_=0 _N_=11
Name=Judy Sex=F Age=14 Height=64.3 Weight=90 _ERROR_=0 _N_=12
Name=Louise Sex=F Age=12 Height=56.3 Weight=77 _ERROR_=0 _N_=13
Name=Mary Sex=F Age=15 Height=66.5 Weight=112 _ERROR_=0 _N_=14
Name=Philip Sex=M Age=16 Height=72 Weight=150 _ERROR_=0 _N_=15
Name=Robert Sex=M Age=12 Height=64.8 Weight=128 _ERROR_=0 _N_=16
Name=Ronald Sex=M Age=15 Height=67 Weight=133 _ERROR_=0 _N_=17
Name=Thomas Sex=M Age=11 Height=57.5 Weight=85 _ERROR_=0 _N_=18
Name=William Sex=M Age=15 Height=66.5 Weight=112 _ERROR_=0 _N_=19
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.M has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
akash1088
Obsidian | Level 7
Actually I have many SAS code as a . SAS file but issues is that if I can't make changes in SAS code everytime so I am planning to store this Macro in permanent dataset and execute them .

Is there any way to do that guys ?
Kurt_Bremser
Super User

@akash1088 wrote:
Actually I have many SAS code as a . SAS file but issues is that if I can't make changes in SAS code everytime so I am planning to store this Macro in permanent dataset and execute them .

Is there any way to do that guys ?

So you just want to bypass proper code maintenance processes?

Tom
Super User Tom
Super User

If you have the source code for a macro definition in character variable(s) in a SAS dataset you have to get SAS to treat that as code.

 

Say you have a dataset named HAVE with a variable named LINE that has the code as multiple lines of text.

I think the easiest is to just write it to file and then %INCLUDE that file.

filename macdef temp;
data _null_;
  set have;
  file macdef;
  put line;
run;
%include macdef;

 

akash1088
Obsidian | Level 7

Thanks @Tom 

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 3826 views
  • 1 like
  • 6 in conversation