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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 661 views
  • 2 likes
  • 3 in conversation