BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jacksonan123
Lapis Lazuli | Level 10

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; 
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.);

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

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.

jacksonan123
Lapis Lazuli | Level 10
Please see the reply I received from Astounding (Esteemed Advisor) which I
will post and answered my question with code that is operates inside the
Macro.
Astounding
PROC Star

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.);

jacksonan123
Lapis Lazuli | Level 10

Your code worked perfectly.

 

I had a follow-up question.  Is there a reason that my original code using substr didn't work?

Astounding
PROC Star

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1020 views
  • 0 likes
  • 3 in conversation