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

I have a variable that contains a string of 9 numbers as values (e.g. 0000000100, 1110000100, etc.). These 9 numbers represent individual values (1 or 0). They were generated by a survey using radio buttons. If there is a 1 in the first position, it indicates a "Yes" to question number 1. If there is a 0 in the second position, it indicates a "No" to question number 2. 

 

I need to create 9 new variables from each of the values in the string. 

 

Var1 will get a 1 if the value in position one is 1. It will get a 0 if the value in position one is 0.

Var2 will get a 1 if the value in position 2 is 1. Etc.

 

How can I select specific values in a string like this?

 

I apologize if my description is unclear. I am a relative novice.

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star
data want;
  set have;
  array answers (*) $ 1 A1-A9;
  do i = 1 to 9;
    answers(i) = substr(answer_string, i, 1);
  end;
run; 

View solution in original post

6 REPLIES 6
SASKiwi
PROC Star
data want;
  answer_string = '101010101';
  array answers (*) $ 1 A1-A9;
  do i = 1 to 9;
    answers(i) = substr(answer_string, i, 1);
  end;
run; 
_maldini_
Barite | Level 11

Thanks @SASKiwi. Can I ask you a few clarifying questions?

 

  1. <answer_string = '101010101';>  Does this define the name of the string? And the value? The actual value of the string is different for each observation. Subject 1 might have 101010101 while subject 2 might have 000010101. Do I need to account for that in this line?
  2. What does the 1 in the array represent (After the $ and before the elements, A1-A9)?
SASKiwi
PROC Star
  1. Yes. I assigned it just one value so you could understand the solution more easily. Of course it changes for each observation. Do you already have a dataset with this string in it? If you do then just read it in with a SET statement.
  2. The 1 is the length of the variables A1 to A9 - one character. 
_maldini_
Barite | Level 11

<I assigned it just one value so you could understand the solution more easily.>

 

Got it. I guess I'm still not clear what to put here if the values are different from observation to observation. 

 

Yes, I do have the data set and was planning on reading it in w/ a SET statement. 

SASKiwi
PROC Star
data want;
  set have;
  array answers (*) $ 1 A1-A9;
  do i = 1 to 9;
    answers(i) = substr(answer_string, i, 1);
  end;
run; 
_maldini_
Barite | Level 11

I am using this array again for a similar purpose, and need some additional help.

 

<The SUBSTR function call has too many arguments.>

 

This time, the string is a maximum of 6 characters. Each value is 2 characters. In other words, a value of 010101 has 3 distinct values (01, 01, 01).

 

Ultimately, I want to create 3 new variables for each string:

IF VAR1=01 THEN NEW_VAR1=1; ELSE NEW_VAR1=0; 

IF VAR2=01 THEN NEW_VAR2=1; ELSE NEW_VAR2=0; 

Etc.

 

This code works for one variable:

DATA want;
	SET have;
	
	ARRAY answers (*) $ 2 VAR1-VAR3;
	DO i = 1 to 3;
	answers(i) = substr(_400203, i, 2);
	END;
	
	DROP 
	_400203
	i;
RUN; 

But this code does not work for multiple variables (In this example there are only 2 variables (_400203 and_400204), but I want to use it w/ 44 variables.

 

<The SUBSTR function call has too many arguments.>

 

DATA want;
SET have

ARRAY array_name (*) $ 2 VAR1-VAR6;
DO i = 1 to 6;
answers(i) = substr(_400203-_400204, i, 2);
END;

DROP 
_400203
_400204
i;
RUN;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 6 replies
  • 2378 views
  • 8 likes
  • 2 in conversation