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

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
Barite | Level 11

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
Barite | Level 11

Thanks for the note.

It helps a lot.

HHC

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 617 views
  • 0 likes
  • 2 in conversation