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;
@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 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')
Oh I see.
Let put it in a macro and call it later.
Thanks.
HHC
@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.
Thanks for the note.
It helps a lot.
HHC
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.