BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
TX_STAR
Obsidian | Level 7

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!!!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

@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;
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
TX_STAR
Obsidian | Level 7
Thank you all. Both solutions work for me!!!
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
  • 3 replies
  • 1326 views
  • 2 likes
  • 3 in conversation