I'm trying to find the max of four variables, Value_A Value_B Value_C Value_D, within a macro. I thought I could do %sysfunc(max(value_&i.)) but that isn't working. My full code is:
%let i = (A B C D);
%macro maxvalue;
data want;
set have;
%do j = 1 %to %sysfunc(countw(&list.));
%let i = %scan(&list.,&j.);
value_&i.= Sale_&i. - int_&i.
Max_Value = %sysfunc(max(value_&i.));
%end;
run;
%mend maxvalue;
%maxvalue;
I should specify that I only want the max of the four variables for each observation. Thanks for your help!
Aside from the typo - %let i=(A B C D); should be %let list=(A B C D) - you're a) overcomplicating it, and b) confusing macro syntax with datastep syntax. Whilst you could do this using a macro, there is no need.
Given the variables in question are all prefixed in a similar manner (although it would be even better if they were numerically-suffixed, e.g. Value1, Value2), it's far easier to use arrays and the appropriate functions :
data want ;
set have ;
array sale{*} Sale_A Sale_B Sale_C Sale_D ;
array int{*} Int_A Int_B Int_C Int_D ;
array value{*} Value_A Value_B Value_C Value_D ;
/* Iterate over array */
do i = 1 to dim(sale) ;
value{i} = sum(sale{i},-int{i}) ;
end ;
max_value = max(of value{*}) ;
run ;
As aforementioned, you're over-complicating this, but you can achieve what you're trying to do using macro logic by including another for loop within your max_value assignment. This method involves you taking the max of your four variables and a missing value, which should produce the desired result:
%let list = A B C D;
%macro maxvalue;
data want;
set have;
%do j = 1 %to %sysfunc(countw(&list.));
%let i = %scan(&list.,&j.);
value_&i.= Sale_&i. - int_&i.
%end;
max_value = max(
%do x = 1 %to %sysfunc(countw(&list.));
%let y = %scan(&list.,&x.);
value_&y.,
%end; .
);
run;
%mend maxvalue;
%maxvalue;
Why not just rename your variables to SALE_1 to SALE_4? Then you can reference them with a simple variable list SALE_1-SALE_4.
If you are going to use non-numeric suffixes on lists of similarly named variables then perhaps what you really need is a simple function style macro to generate the lists of variable names based on a base name and list of suffix values.
%macro generate_names(base,list);
&base%sysfunc(tranwrd(%sysfunc(compbl(&list)),%str( ),%str( &base)))
%mend generate_names;
Then it is easier to generate variable lists to use for ARRAY statements
%let suffixes=A B C D;
array sale %generate_names(Sale_,&suffixes);
array int %generate_names(Int_,&suffixes);
array value %generate_names(Value_,&suffixes);
and other statements.
max_value = max(of %generate_names(Value_,&suffixes)) ;
Related
I'm SAS user.
I want to assign year columns using date values.
for example, here is my code, below.
I want to make Y_2010, Y_2011, Y_2012 , Y_2013, Y_2014 in work.total data set.
but there is only Y_2014 as a result.
How can I change the code as I can get right result which I intended first?
options mcompilenote = all;
%let a = Y_ ;
%macro B(YMIN, YMAX) ;
%do i = &YMIN %to &YMAX ;
DATA TOTAL ;
SET SASUSER.EMPDATA ;
IF YEAR(HIREDATE) = &i THEN &a&i = 1 ;
ELSE &a&i = 0 ;
RUN;
%end;
%mend;
%B (2010, 2014) ;
Because you are repeatedly re-creating the output dataset only the final version is available. To fix the macro move the %DO loop inside the DATA step so that you are generating all of the variables in a single data step.
%macro B(YMIN, YMAX) ;
DATA TOTAL ;
SET SASUSER.EMPDATA ;
%do i = &YMIN %to &YMAX ;
IF YEAR(HIREDATE) = &i THEN &a&i = 1 ;
ELSE &a&i = 0 ;
%end;
RUN;
%mend;
But there is no need to a macro for this. Just use normal SAS statements. For example you could use an ARRAY statement to define the variables and then loop over the array and set the values. Note that the result of a boolean expression in SAS is 0 when false and 1 when true so you can eliminate the IF/THEN/ELSE statement and just use an assignment statement.
DATA TOTAL ;
SET SASUSER.EMPDATA ;
array &a &a&ymin - &a&ymax;
do i=&ymin to &ymax ;
&a[i-&ymin+1] = (year(hiredata)=i);
end;
drop i;
RUN;
I have created a SAS macro, macro A, that takes in a variable name and returns transformed versions of that name i.e. if you run %A(asdf) you get out asdf_log asdf_exp asdf_10. I want to write another macro, macro B, that takes the output from the first macro and appends it together into a new macro variable.
%macro B(varList, outputName);
%let &outputName =
%A(var1);
%A(var2);
;
%mend
Is almost what I want to do, except that it obviously doesn't compile.
I am also not sure if this is possible in SAS.
As a further complication, the input to macro B is a list of variable that I want to run macro A for and append into one long list of variable names.
Why? Because I have a macro that runs on a list of variables and I want to run it on a transformed variable list.
Example:
I have %let varList = x y; and I want as an output x_log x_exp x_10 y_log y_exp y_10. To do this I want two macros one, macro A, that returns the transformed variables names:
%macro A(var);
&var._log
&var._exp
&var._10
%mend
I can't get the second macro (B as written above) to work properly.
So if the inner macro is just returning characters, that is it doesn't actually generate any non macro statements, then why not make the outer one work the same way?
%macro inner(x);
&x._log &x._exp &x._10
%mend;
%macro outer(list);
%local i;
%do i=1 %to %sysfunc(countw(&list));
%inner(%scan(&list,&i))
%end;
%mend outer;
%let want=%outer(X y Z);
This is not too hard. You need to loop over the values in varList, appending results to outputName. You also need to declare outputName as GLOBAL so it will be accessible outside %B
%macro B(varList, outputName);
%global &outputName;
%let &outputName = ;
%local i n var;
%let n = %sysfunc(countw(&varList));
%do i=1 %to &n;
%let var = %scan(&varList,&i);
%let &outputName = &outputName %A(&var);
%end;
%mend;
I have the following variables: A_Bldg B_Bldg C_Bldg D_Bldg. I want to multiply them by INTSF and store the result in a new variable, Sale_i. For example, A_Bldg * INTSF = Sale_A, B_Bldg * INTSF = Sale_B, and so on.
My code is:
%macro loopit(mylist);
%let n=%sysfunc(countw(&mylist));
%do J = 1 %to &n;
%let i = %scan(&mylist,&J);
data test;
set data;
sale_&i. = &i._Bldg * INTSF;
run;
%end;
%mend;
%let list = A B C D;
%loopit(&list);
This only produces Sale_D, which is the last letter in the list. How do I get Sales A-C to appear? The first four lines of code are so I can loop through the text A-D. I thought about doing it with arrays, but didn't know how to choose the variables based on the A-D indicators. Thanks for your help!
You're currently looping through your list and recreating the test dataset every time, so it only appears to have sale_d because you're only viewing the last iteration.
You can clean up your loop by scanning through your list in one data step to solve your problem:
%let list = A B C D;
%macro loopit;
data test;
set data;
%do i = 1 %to %sysfunc(countw(&list.));
%let this_letter = %scan(&list., &i.);
sale_&this_letter. = &this_letter._Bldg * INTSF;
%end;
run;
%mend loopit;
%loopit;
Your %DO loop is in the wrong place. But really you do not need to use macro code to do something that the native SAS code can already do.
data want;
set have ;
array in A_Bldg B_Bldg C_Bldg D_Bldg ;
array out sale_1-sale4 ;
do i=1 to dim(in);
out(i)=intsf*in(i);
end;
run;
I would like to create variables containing lagged values of a given variable for a large number of lags. How could I do this? I try the following:
data out;
set in;
do i = 1 to 50;
%let j = i;
lag_&j = Lag&j.(x);
end;
run;
How can I get the loop variable i into the macro variable j or how to use it directly to create the appropriately named variable and for the Lag function?
Chris J answers the question, but here i'll provide my preferred way of doing this.
%macro lagvar(var=,num=);
%do _iter = 1 %to &num.;
lag_&_iter. = lag&_iter.(&var.);
%end;
%mend lagvar;
data out;
set in;
%lagvar(var=x,num=50); *semicolon optional here;
run;
This is a more modular usage of the macro loop (and more readable, assuming you use intelligent names - the above is okay, you could do even more with the name if you wanted to be very clear, and of course add comments).
You're mixing macro & datastep syntax incorrectly...
You need a macro-loop (%DO instead of do) to generate the datastep code (i.e. lag1-lag50), and macro-loops need to be within a macro.
%MACRO LAGLOOP ;
data out ;
set in ;
%DO J = 1 %TO 50 ;
lag_&J = lag&J(x) ;
%END ;
run ;
%MEND ;
%LAGLOOP ;
I created two macro variables in my sas code using the %let option.
data sasdata1.dataone;
set sasdata1.dataone ;
%let week=1;
%let sum=0;
do i=1 to 53;
%let sum= _W&week._NRX + &sum.;
week=&week+1;
end;
drop i;
week=&week;
sum=&sum.;
run;
the value of the week variable remains 1 even after the loop has executed. Which is the correct way to change the value of Macro variables?
If your week variables are all next to each other in the dataset, you may want to consider a macro-less approach:
data sasdata1.dataone;
set sasdata1.dataone;
sum = sum(of _W1_NRX--_W53_NRX); *double dash means that the columns are next to each other with _W1_NRX as first and _W53_NRX as last;
run;
If your week variables end with the week number, they do not even need to be next to each other:
data sasdata1.dataone;
set sasdata1.dataone;
sum = sum(of _W1-_W53); *single dash implies that the suffix is numerically increasing;
run;
Clean and easy.
Here's a quick example showing how to do this in the context of a macro. Run this and you can see in the log what's happening.
* 'week' is in scope within the macro only
and iterates because of %do loop (no need
to explicitly increment it ;
%macro test1;
%do week=1 %to 10;
%put &week;
%end;
%mend;
%test1
* sum has global scope ;
%let sum=0;
* 'week' acts as per test1 above, but we
explicitly manipulate 'sum' within each iteration ;
%macro test2;
%do week=1 %to 10;
%let sum=%eval(&sum+&week);
%put in macro: week is &week, sum is now ∑
%end;
%mend;
%put before macro executes, sum is ∑
%test2
%put after macro executes, sum is now ∑