BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Rhodochrosite | Level 12

Hi,

There is a piece of my code that need manual change from time to time.

Thus, I want to put this piece on top and then call it in the middle of my full code.

Can you please show me how to do it?

Thank you so much,

HHCFX 

 

In the sample below, I put that 3 line on top and it supposed to be execute at the end

data final; set want1; if id=2;run;

 

 

*my 3 lines that need manual changes;
data final; set want1;
if id=2;run;



*Main code----------------------;

data have;
input ID var;
datalines;
1 2
2 3
3 4
;run;

proc means data=have noprint;
by id;
var var;
output out=want1
sum=sum_var; run;

*Call the 3 line code here;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@hhchenfx wrote:

Hi,

There is a piece of my code that need manual change from time to time.

Thus, I want to put this piece on top and then call it in the middle of my full code.

Can you please show me how to do it?

Thank you so much,

HHCFX 

 

In the sample below, I put that 3 line on top and it supposed to be execute at the end

data final; set want1; if id=2;run;

 

 

*my 3 lines that need manual changes;
data final; set want1;
if id=2;run;



*Main code----------------------;

data have;
input ID var;
datalines;
1 2
2 3
3 4
;run;

proc means data=have noprint;
by id;
var var;
output out=want1
sum=sum_var; run;

*Call the 3 line code here;

What do you need to change from time to time?

This is a likely candidate for a macro. Define the macro at the top of the code and then call with needed changes with your changes as parameters.

 

%macro helper(outset=work.final, inset=work.want1, idvar=id, id=2);
data &outset.;
   set &inset.;
   if &idvar. = &id.;
run;
%mend;




proc means data = sashelp.class;
   class sex;
   var height;
   output out=work.summary sum=sum_var;
run;

%helper(inset=work.summary, idvar=sex, id='M')


View solution in original post

4 REPLIES 4
ballardw
Super User

@hhchenfx wrote:

Hi,

There is a piece of my code that need manual change from time to time.

Thus, I want to put this piece on top and then call it in the middle of my full code.

Can you please show me how to do it?

Thank you so much,

HHCFX 

 

In the sample below, I put that 3 line on top and it supposed to be execute at the end

data final; set want1; if id=2;run;

 

 

*my 3 lines that need manual changes;
data final; set want1;
if id=2;run;



*Main code----------------------;

data have;
input ID var;
datalines;
1 2
2 3
3 4
;run;

proc means data=have noprint;
by id;
var var;
output out=want1
sum=sum_var; run;

*Call the 3 line code here;

What do you need to change from time to time?

This is a likely candidate for a macro. Define the macro at the top of the code and then call with needed changes with your changes as parameters.

 

%macro helper(outset=work.final, inset=work.want1, idvar=id, id=2);
data &outset.;
   set &inset.;
   if &idvar. = &id.;
run;
%mend;




proc means data = sashelp.class;
   class sex;
   var height;
   output out=work.summary sum=sum_var;
run;

%helper(inset=work.summary, idvar=sex, id='M')


hhchenfx
Rhodochrosite | Level 12

Oh I see. 

Let put it in a macro and call it later.

Thanks.

HHC

ballardw
Super User

@hhchenfx wrote:

Oh I see. 

Let put it in a macro and call it later.

Thanks.

HHC


That specific macro uses Keyword variable assigments. So if you like the default as assigned in the macro you do not need to pass a value for the parameter. For example I provided a default of ID for IDVAR but used in the example call a different variable because I created a different summary data set to demonstrate the flexibility. This one will only work with single values for the ID variable and I demonstrated how to use a character value. This will not work with multiple values though. You could pass a date value using id='01JAN2019'd.

 

 

hhchenfx
Rhodochrosite | Level 12

Thanks for the note.

It helps a lot.

HHC

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 4 replies
  • 1140 views
  • 0 likes
  • 2 in conversation