Hello,
I have a categorical variable RESP like this '
AB++++B+BD+A++BCDB++++B++A+B++A++BC++B+B+D |
I want to recode the 42 columns into 42 numerical variables, such as
if substr(RESP, 1, 1) = '+ ' then score1=1;
else if substr(RESP, 1, 1 )= '*' then score1=0;
else score1=0;
How to code this using array and do loop?
Thanks!!!
@TX_STAR wrote:
Hello,
I have a categorical variable RESP like this '
AB++++B+BD+A++BCDB++++B++A+B++A++BC++B+B+D
I want to recode the 42 columns into 42 numerical variables, such as
if substr(RESP, 1, 1) = '+ ' then score1=1;
else if substr(RESP, 1, 1 )= '*' then score1=0;
else score1=0;
How to code this using array and do loop?
Thanks!!!
I don't think macro variables would help with this at all.
data want;
set have;
array score [42] ;
do i=1 to dim(score);
score[i]= ('+'=char(resp,i));
end;
run;
@TX_STAR wrote:
Hello,
I have a categorical variable RESP like this '
AB++++B+BD+A++BCDB++++B++A+B++A++BC++B+B+D
I want to recode the 42 columns into 42 numerical variables, such as
if substr(RESP, 1, 1) = '+ ' then score1=1;
else if substr(RESP, 1, 1 )= '*' then score1=0;
else score1=0;
How to code this using array and do loop?
Thanks!!!
I don't think macro variables would help with this at all.
data want;
set have;
array score [42] ;
do i=1 to dim(score);
score[i]= ('+'=char(resp,i));
end;
run;
It seems to me that macro variables (or macros) are not needed here.
data want;
resp='AB++++B+BD+A++BCDB++++B++A+B++A++BC++B+B+D';
array score score1-score42;
do i=1 to length(Resp);
if substr(resp,i,1)='+' then score(i)=1;
else if substr(resp,i,1)='*' then score(i)=0;
else score(i)=0;
end;
run;
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.