I have the following data set which is being processed within a macro:
id time ipred occ sid
subj1 0 0
subj1 0.5 0
subj1 1 0
subj1 2 0
subj1 3 0
subj1 4 0
subj1 5 0
I would like to:
1.take a substring of the variable subj1 to have a result of 1. How can this be done when the substring is a value within the id column?
2.I tried to convert id from a character to numeric value using sid=input(id,8.) but a s you can see it is converted but the column entries are missing. What am I doing wrong to cause the column entries to be missing?
I have included my code which is part of a Macro.
/*16)**************TEST CONCENTRATIONS************************/
data simt&i._&j;
retain _name_ cln lagn ka1n ka2n vfn fn ;
set nall_final&i._&j;
if Treat in ('ATES') ;
if _n_=2;
do time=0, 0.5,1,2,3,4,5,6,7,8,9,10,12,14,16,18,20,24;
/*DOSE IS IN NG AND CP IN NG/ML*/
dose=40000;
cln=input (cl,8.);
rename cln=cl;
lagn= input (lag,8.);
rename lagn= lag;
ka1n=input (ka1,8.);
rename ka1n=ka1;
ka2n=input (ka2,8.);
rename ka2n=ka2;
vfn=input (vf,8.);
rename vfn=vf;
fn=input (logit,8.);
rename fn=f;
id=input(_name_,8.);
rename _name_=id;
drop id cl lag ka1 ka2 vf logit;
substr(subj1&i,1,4);
sid=input (id,8.);
output ;
end;
run;
As you have seen, the INPUT function can't read "subj" as numeric. If you want to get just the digits, use COMPRESS:
id2 = compress(id, , 'kd');
KD means keep only the digits.
That value can be converted to numeric:
id3 = input(id2, 8.);
And you could combine these into one statement:
id3 = input(compress(id, , 'kd'), 8.);
PLEASE do always post example data in a working data step with datalines; right now it's very hard to make sense of your data:
id time ipred occ sid
subj1 0 0
subj1 0.5 0
subj1 1 0
subj1 2 0
subj1 3 0
subj1 4 0
subj1 5 0
as you seem to have 5 variables, but the last two of them are always missing?
This statement
substr(subj1&i,1,4);
will throw a syntax error. substr() must either be a part of an expression (right side of an assignment, or in a condition), or be used on the left side of an assignment, but you don't have one here. Since your variable is already called subj1, the resolution of macro variable &i (which I take to be a counter of a %do loop) will result in a variable you do not have on your dataset.
Start out with a piece of working Base SAS code before you wrap it into a macro definition.
As you have seen, the INPUT function can't read "subj" as numeric. If you want to get just the digits, use COMPRESS:
id2 = compress(id, , 'kd');
KD means keep only the digits.
That value can be converted to numeric:
id3 = input(id2, 8.);
And you could combine these into one statement:
id3 = input(compress(id, , 'kd'), 8.);
Your code worked perfectly.
I had a follow-up question. Is there a reason that my original code using substr didn't work?
Presumably, you are talking about this:
substr(subj1&i, 1, 4)
Macro language resolves first, and SAS sees the result only. For example, when &i is 1, SAS sees:
substr(subj11, 1, 4)
This causes SAS to look for a variable named subj11 (which I presume doesn't exist).
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.