SAS assign results from macro function to array within data step - macros

This code takes two macros and assigns them to arrays inside a data step, then loops through each variable defined in ln_vars, creating a new variable which is the natural log of the variable, appending _ln to the name
%let ln_vars = var1 var2;
%let ln_names = %add_string(&ln_vars, _ln);
data transform;
set analysis;
array ln &ln_vars;
array ln_n &ln_names;
*call execute ('%add_string(%str(&ln_vars), _ln)');
do over ln;
ln_n = log(ln);
end;
run;
maybe there's a better idiom in sas code (I hope).
I want to be able to just pass a single macro (the ln_vars macro) and call the %add_string() function from inside the data step.
The commented 'call execute' returns the correct string, but when I try to
1588 array ln_n call execute ('%add_string(%str(&ln_vars), _ln)');
ERROR: Attempt to initialize variable call in numeric array ln_n with character constant
'%add_string(%str(&ln_vars), _ln)'.

It would help if you gave us the definition of the %add_string macro. In any case, it looks like you need &ln_names to be a list of variables matching the list in &ln_vars except each variable has the suffix '_ln'.
If I'm correct, you don't really need the %add_string macro and could do this instead:
%let ln_vars = var1 var2;
%let ln_names = %sysfunc(tranwrd(&ln_vars,%str( ),%str(_ln )))_ln;
%put LN_VARS: &ln_vars;
%put LN_NAMES:&ln_names;
data transform;
set analysis;
array ln &ln_vars;
array ln_n &ln_names;
do over ln;
ln_n = log(ln);
end;
run;
Note that the two %put statements are not really necessary - they are just placed there to inspect the values of the two macro variables
As per your comment, you could have a macro:
%macro lnvars(vars=,suffix=_ln);
%let newvars=%sysfunc(tranwrd(&vars,%str( ),%str(&suffix )))&suffix;
array ln &vars.;
array ln_n &newvars.;
do over ln;
ln_n = log(ln);
end;
%mend;
and then call the macro from your data step as follows:
data transform;
set analysis;
%lnvars(vars=var1 var2);
run;
(Note, I've not tested the code, but you should get the general idea)

Related

Macro numeric values comparing

I am trying to compare two numberic value in a Macro.
But I keep getting the following message:
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 0.2
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 0.05
ERROR: The %BY value of the %DO I loop is invalid.
ERROR: The macro FAIL will stop executing.
My code is the following:
%macro fail;
%do i=0 %to 0.2 %by 0.05;
data failcrs;
set fail;
if f_p>=input(&i, 8.) then output;
run;
%end;
%mend failcrs;
f_p is a numeric variable.
What is wrong with my code? Please help.
Thank you so much!
Conditional tests in macro code (%if, %until, %while, etc) use %eval() macro function that only does integer arithmetic. This includes the increments and tests done in a %do-%to-%by loop.
To use floating point arithmetic you need to use the %sysvalf() macro function.
You could code your own increments to the loop counter.
%let i=0;
%do %while( %sysevalf( &I <= 0.2 ) );
...
%let i=%sysevalf(&i + 0.05);
%end;
Or make the loop counter an integer and use another macro variable to hold the fraction.
%do j=0 %to 20 %by 5 ;
%let i=%sysevalf(&j/100);
...
%end;
You have a couple of issues.
Macro loops work better with integers, but an easy workaround is a %DO %UNTIL loop instead.
Name on %MEND is different than on %MACRO
Invalid values for %DO %I loop.
Non-unique data set name, which means the output overwrites itself.
*fake data to work with;
data fail;
do f_p=0 to 0.2 by 0.01;
output;
end;
run;
%macro fail;
%let i=0;
%do %until(&i = 0.2); /*2*/
data failcrs_%sysevalf(&i*100); /*3*/
set fail;
if f_p>=&i then output;
run;
%let i = %sysevalf(&i + 0.05);
%end;
%mend fail; /*3*/
*test macro;
%fail;
The numbers in the comments align with the issues identified.
Try using best32. But why do you want to loop, when your dataset is overwritten for each loop. Please check log for each of step below. As at #Reeza in comments explains below you even do not an input statement
Options mprint;
/* do this*/
%macro fail;
%let i =15;
data failcrs;
set sashelp.class;
if age lt input(&i, best32.) then output;
run;
%mend fail;
%fail
/* dataset overwritten every time to finally pick up 15 as value check in the log*/
%macro fail1;
%do i = 1 %to 15;
data failcrs1;
set sashelp.class;
if age lt input(i, best32.) then output;
run;
%end;
%mend fail1;
%fail1
%macro fail;
%let i=0;
%do %until(&i = 0.2);
data failcrs;
set crse_grade_dist_fail;
if f_p>=&i then output;
run;
proc sql;
create table count_failclass as
select strm, count(class_nbr) as numfclass_%sysevalf(&i*100)
from failcrs
group by strm;
quit;
proc sql;
create table failfaculty as
select strm, instructor_id, instructor, count(class_nbr) as numfclass
from failcrs
group by strm, instructor_id, instructor;
quit;
proc sql;
create table count_failfaculty as
select strm, count(instructor) as numffaculty_%sysevalf(&i*100)
from failfaculty
group by strm;
quit;
data count_class_faculty;
set count_class_faculty;
set count_failclass;
set count_failfaculty;
run;
%let i = %sysevalf(&i + 0.05);
%end;
%mend fail;
Good thing is my data doesn't have f_p=0, all of them is greater than zero. Because I only want to count failed courses.
Documentation is written to be read, a simple search for "SAS 9.4 Macro Do" should explain it all -- start, stop and by are integers -- integers in the sense that whatever macro source expression in their place evaluates implicitly or explicitly to an integer at need time.
The macro you coded is a little strange. It will generate multiple data steps that all overwrite the same dataset. You might want to concentrate on not writing macro code first, and move to it when the need to have repetitive boilerplate code submitted. Writing good macro code means you have to think in terms of "will this generate appropriate source code and what side effect will these macro statements have in their resolution scope"
%DO, Iterative Statement
Syntax
%DO macro-variable=start %TO stop <%BY increment>;
  text and macro language statements
%END;
Required Arguments
macro-variable
names a macro variable or a text expression that generates a macro
variable name. Its value functions as an index that determines the
number of times the %DO loop iterates. If the macro variable specified
as the index does not exist, the macro processor creates it in the
local symbol table.
You can change the value of the
index variable during processing. For example, using conditional
processing to set the value of the index variable beyond the stop
value when a certain condition is met ends processing of the loop.
startstop
specify integers or macro expressions that generate integers
to control the number of times the portion of the macro between the
iterative %DO and %END statements is processed.
The first time the
%DO group iterates, macro-variable is equal to start. As processing
continues, the value of macro-variable changes by the value of
increment until the value of macro-variable is outside the range of
integers included by start and stop.
increment
specifies an integer
(other than 0) or a macro expression that generates an integer to be
added to the value of the index variable in each iteration of the
loop. By default, increment is 1. Increment is evaluated before the
first iteration of the loop. Therefore, you cannot change it as the
loop iterates.

rename the variables in an array with names from another array

I am trying to rename the variables based on one array elements in the folloing way,
%let var= class name gender;
data want;
set have;
%global noof;
array point(*)$ %str(&var) ;
a=dim(point);
call symputx('noof',a);
array newvar(&noof);
do i=1 to &noof;
newvar(i)=translate(point(i),',','.');
end;
drop &var;
do i=1 to &noof;
rename newvar(i)=vname(point(i));
end;
run;
I want to rename the new variables to the first array elemets.
LOG:
rename newvar(i)=vname(point(i));
-
22
76
ERROR 22-322: Syntax error, expecting one of the following: -, :, =.
ERROR 76-322: Syntax error, statement will be ignored.
Unfortunately, the value on the RHS of the RENAME statement must be a literal. The statement is evaluated at compile time, not run time.
Try this:
%let var= class name gender;
%macro translate(datain,dataout,vars);
%local n i var;
%let n=%sysfunc(countw(&vars));
data &dataout(rename=(
%do i=1 %to &n;
%let var = %scan(&vars,&i);
newvar&i = &var
%end;
));
set &datain;
array invars(&n) $ &vars ;
array newvar(&n) $;
do i=1 to &n;
newvar(i)=translate(invars(i),',','.');
end;
drop &vars i;
run;
%mend;
data test;
class = "1,2,3";
name= "Dom,Pazzula";
gender="M";
run;
%translate(test,out,&var);
You can run into issues if the length of these character variables are too large. The new variables might be truncated. You will have to modify this to add a length statement.

recode and add prefix to sas variables

Lets's say I have a bunch of variables named the same way and I'd like to recode them and add a prefix to each (the variables are all numeric).
In Stata I would do something like (let's say the variables start with eq)
foreach var of varlist eq* {
recode var (1/4=1) (else=0), pre(r_)
}
How can I do this in SAS? I'd like to use the %DO macros, but I'm not familiar with them (I want to avoid SQL). I'd appreciate if you could include comments explaining each step!
SAS syntax for this would be easier if your variables are named using numeric suffix. That is, if you had ten variables with names of eq1, eq2, .... , eq10, then you could just use variable lists to define both sets of variables.
There are a number of ways to translate your recode logic. If we assume you have clean variables then we can just use a boolean expression to generate a 0/1 result. So if 4 and 5 map to 1 and the rest map to 0 you could use x in (4,5) or x > 3 as the boolean expresson.
data want;
set have;
array old eq1-eq10 ;
array new r_eq1-r_eq10 ;
do i=1 to dim(old);
new(i) = old(i) in (4,5);
end;
run;
If you have missing values or other complications you might want to use IF/THEN logic or a SELECT statement or you could define a format you could use to convert the values.
If your list of names is more random then you might need to use some code generation, such as macro code, to generate the new variable names.
Here is one method that use the eq: variable list syntax in SAS that is similar to the syntax of your variable selection before. Use PROC TRANSPOSE on an empty (obs=0) version of your source dataset to get a dataset with the variable names that match your name pattern.
proc transpose data=have(obs=0) out=names;
var eq: ;
run;
Then generate two macro variables with the list of old and new names.
proc sql noprint ;
select _name_
, cats('r_',_name_)
into :old_list separated by ' '
, :new_list separated by ' '
from names
;
quit;
You can then use the two macro variables in your ARRAY statements.
array old &old_list ;
array new &new_list ;
You can do this with rename and a dash indicating which variables you want to rename. Note the following only renames the col variables, and not the other one:
data have;
col1=1;
col2=2;
col3=3;
col5=5;
other=99;
col12=12;
run;
%macro recoder(dsn = , varname = , prefix = );
/*select all variables that include the string "varname"*/
/*(you can change this if you want to be more specific on the conditions that need to be met to be renamed)*/
proc sql noprint;
select distinct name into: varnames
separated by " "
from dictionary.columns where memname = upcase("&dsn.") and index(name, "&varname.") > 0;
quit;
data want;
set have;
/*loop through that list of variables to recode*/
%do i = 1 %to %sysfunc(countw(&varnames.));
%let this_varname = %scan(&varnames., &i.);
/*create a new variable with desired prefix based on value of old variable*/
if &this_varname. in (1 2 3) then &prefix.&this_varname. = 0;
else if &this_varname. in (4 5) then &prefix.&this_varname. = 1;
%end;
run;
%mend recoder;
%recoder(dsn = have, varname = col, prefix = r_);
PROC TRANSPOSE will give you good flexibility with regards to the way your variables are named.
proc transpose data=have(obs=0) out=vars;
var col1-numeric-col12;
copy col1;
run;
proc transpose data=vars out=revars(drop=_:) prefix=RE_;
id _name_;
run;
data recode;
set have;
if 0 then set revars;
array c[*] col1-numeric-col12;
array r[*] re_:;
call missing(of r[*]);
do _n_ = 1 to dim(c);
if c[_n_] in(1 2 3) then r[_n_] = 0;
else if c[_n_] in(4 5) then r[_n_] = 1;
else r[_n_] = c[_n_];
end;
run;
proc print;
run;
It would be nearly trivial to write a macro to parse almost that exact syntax.
I wouldn't necessarily use this - I like both the transpose and the array methods better, both are more 'SASsy' (think 'pythonic' but for SAS) - but this is more or less exactly what you're doing above.
First set up a dataset:
data class;
set sashelp.class;
age_ly = age-1;
age_ny = age+1;
run;
Then the macro:
%macro do_count(data=, out=, prefix=, condition=, recode=, else=, var_start=);
%local dsid varcount varname rc; *declare local for safety;
%let dsid = %sysfunc(open(&data.,i)); *open the dataset;
%let varcount = %sysfunc(attrn(&dsid,nvars)); *get the count of variables to access;
data &out.; *now start the main data step;
set &data.; *set the original data set;
%do i = 1 %to &varcount; *iterate over the variables;
%let varname= %sysfunc(varname(&dsid.,&i.)); *determine the variable name;
%if %upcase(%substr(&varname.,1,%length(&var_start.))) = %upcase(&var_start.) %then %do; *if it matches your pattern then recode it;
&prefix.&varname. = ifn(&varname. &condition., &recode., &else.); *this uses IFN - only recodes numerics. More complicated code would work if this could be character.;
%end;
%end;
%let rc = %sysfunc(close(&dsid)); *clean up after yourself;
run;
%mend do_count;
%do_count(data=class, out=class_r, var_start=age, condition= > 14, recode=1, else=0, prefix=p_);
The expression (1/4=1) means values {1,2,3,4} should be recoded into
1.
Perhaps you do not need to make new variables at all? If have variables with values 1,2,3,4,5 and you want to treat them as if they have only two groups you could do it with a format.
First define your grouping using a format.
proc format ;
value newgrp 1-4='Group 1' 5='Group 2' ;
run;
Then you can just use a FORMAT statement in your analysis step to have SAS treat your five level variable as it if had only two levels.
proc freq ;
tables eq: ;
format eq: NEWGRP. ;
run;

Get the ith word in a macro variable list

%let TableList = TableA TableH TableB TableG;
Words in &TableList are separated by ' '.
How can I retrieve certain word to do the following?
I do not know the number of words in the tablelist and would like to get the nth word from the list.
Given i = 4,
data &&table&i.; /* &&table&i. will resolve to TableG */
set have;
[..];
run;
I would have done the same %sysfunc(scan) trick as #mjsqu and as to answer your remaining question - of getting the last word because you don't know the number of words in the list, the easiest way I can think of is using array like below
%let all=word1 word2 word3 word4 word5;
%macro test;
data _NULL_;
array x[*] &all.;
Num=dim(x);
call symput("Num_of_words",num);
run;
%mend;
%test;
Now you know the total number of words so can find out the last word as well.
The short answer is to use the %scan function:
%put %scan(&tablelist,4,%str( ));
The third argument specifies that %scan should count only spaces as delimiters. Otherwise, it will also treat all of the following characters as delimiters by default:
. < ( + & ! $ * ) ; ^ - / , % |
Given the list you have, you can use a %do loop to add the macro variables to a list:
/* initialise a counter macro variable */
%let k = 1;
/* iterate through tablelist until a value is not found */
%do %until (%scan(&tablelist,&k,%str( )) = );
%let table&k = %scan(&tablelist,&k,%str( ));
%let k = &k + 1;
%end;
%let i = 4;
%put &&table&i;
N.B. this code only works inside a macro definition (that is a block of code delimited by %macro and %mend statements.
If you're doing this for the purpose of selecting on the fly one word from the list, you should just make a macro, not try to set up macro variables. Too much extra work to do all that business to make the various macro variables versus a one-line macro.
%let tableList=TableA TableB TableC TableD;
%macro selectTable(k=);
%scan(&tablelist,&k)
%mend selectTable;
data %selectTable(k=4);
set sashelp.class;
run;

SAS: put format in macro

I am trying to create a new variable by assigning a format to an existing variable. I'm doing this from within a macro. I'm getting the following error: ": Expecting a format name." Any thoughts on how to resolve? Thanks!
/* macro to loop thru a list of vars and execute a code block on each. This is working fine. */
%macro iterlist
(
code =
,list =
)
;
%*** ASSIGN EACH ITEM IN THE LIST TO AN INDEXED MACRO VARIABLE &&ITEM&I ;
%let i = 1;
%do %while (%cmpres(%scan(&list., &i.)) ne );
%let item&i. = %cmpres(%scan(&list., &i.));
%let i = %eval((&i. + 1);
%end;
%*** STORE THE COUNT OF THE NUMBER OF ITEMS IN A MACRO VARIABLE: &CNTITEM;
%let cntitem = %eval((&i. - 1);
%*** EXPRESS CODE, REPLACING TOKENS WITH ELEMENTS OF THE LIST, IN SEQUENCE;
%do i = 1 %to &cntitem.;
%let codeprp = %qsysfunc(tranwrd(&code.,?,%nrstr(&&item&i..)));
%unquote(&codeprp.)
%end;
%mend iterlist;
/* set the list of variables to iterate thru */
%let mylist = v1 v2 v3 v4;
/* create a contents table to look up format info to assign in macro below*/
proc contents data=a.recode1 noprint out=contents;
run;
/* macro to create freq and chisq tables for each var */
%macro runfreqs (variabl = );
proc freq data=a.recode1 noprint ;
tables &variabl.*improved /out=&variabl._1 chisq;
output out=&variabl.chisq n pchi ;
run;
/* do some more stuff with the freq tables, then grab format for variable from contents */
data _null_;
set contents;
if name="&variabl." then CALL SYMPUT("classformat", format);
run;
data &variabl._3;
length classvalue $ 30 ;
set &variabl._2; ;
/* output a new var using the macro variable for format that we pulled from contents above. Here's where the error occurs. */
classvalue=put(class, %quote(&classformat.));
run;
%mend runfreqs;
* run the macro, iterating thru var list and creating freq tables;
%ITERLIST(list = &mylist., code = %nrstr(%runfreqs(variabl = ?);));
Just guessing, the line
classvalue=put(class, %quote(&classformat.));
should be
classvalue=put(class, &classformat..);
Two points because one is "eaten" by macro processor to mark end of macro variable name, the second one is needed to complete format name.
I believe you won't need %quote() in your case - format name cannot contain strings quoted by %quote().
EDIT: Again not tried, just based on the code I see you also need to change CALL SYMPUT("classformat", format);
to CALL SYMPUTX("classformat", format);
CALL SYMPUTX() is advanced version of CALL SYMPUT(), it removes trailing blanks in macro variable value while the original version keeps blanks. Effectively this will be same as your solution, just simpler.
So the problem is indeed with extra blanks between format name and the period.
No idea why this works and vasja's idea wouldn't, but the problem was clearly with the period on the end of the format name (or perhaps some extra white space?). I changed the data step to add the period before the SYMPUT call:
data _null_;
set contents;
myformat=catt(format,'.');
if name="&variabl." then CALL SYMPUT("classformat", myformat);
run;