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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.