SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Create one variable from multiple arrays

Reply
Occasional Contributor
Posts: 14

Create one variable from multiple arrays

[ Edited ]

Question: How do I create one character variable from output in mulitple arrays? I'd like to take 4 arrays to create a variable called "first name", and 4 other arrays to create a variable called "last name".

 

Backgorund: I'm working with a very large social network analysis data set (5000+ variables). I've separated the data set into 5 smaller components for easier analysis. In the study, subjects are asked to name their peers in a name generator. Subjects are able to name up to 12 individuals in four categories: categories A (variables q1a-q1l), B (variables q2a-q2l), C (variables q3a-q3l) and D (q4a-q4l), resulting in up to 48 named peers. The data is structured so that there is one row per subject interviewed; however, I want to create a data set with one row per peer named.  So if subject 01 named 5 peers, I'll need 5 rows that capture the information pertaining to those peers. 

 

In previous data sets, I successful created the first and last name of the peers by using the following code:

 

 ------------------------

array names_recode(*) q1a q1b q1c q1d q1e q1f q1g q1h q1i q1j q1k q1l q2a q2b q2c q2d q2e q2f q2g q2h q2i q2j q2k q2l q3a q3b q3c q3d q3e q3f q3g q3h q3i q3j q3k q3l
q4a q4b q4c q4d q4e q4f q4g q4h q4i q4j q4k q4l;

 

do i=1 to 48;

names= names_recode[i];

 

*The following code takes alters names and separates them into first and last. The if-ten statements are for alters that have 3 names and need further specification;
Alter_First= SCAN(names_recode[i], 1, " ");
Alter_Last= SCAN(names_recode[i], 2, " ");

 

output; end;

 

----------------------------

Issue: All other arrays in the larger data set have 48 variables; however, there are only 12 variables describing the nature of the relationship between the subjects and their peers. I was told I'd have to restructure these questions in a separate data set, and merge that information into a larger data set by a common variable (first and last name). How can I create one first name variable from the arrays name1 name2 name3 name4? 

 

Below is the code that I have:

 

----------------------------------

*Importing dataset;
data have;
set ina.sna_combined_170717 (encoding='asciiany');
if SNA_ID= "212100023" then SNA_ID= "212200024"; *correcting an incorrect ID;

keep visit SNA_ID q1a q1b q1c q1d q1e q1f q1g q1h q1i q1j q1k q1l q2a q2b q2c q2d q2e q2f q2g q2h q2i q2j q2k q2l q3a q3b q3c q3d q3e q3f q3g q3h q3i q3j q3k q3l
q4a q4b q4c q4d q4e q4f q4g q4h q4i q4j q4k q4l
/*nrs01- I feel comfortable talking to this person about sensitive topics*/
nrs01_1_1_1 nrs01_1_2_1 nrs01_1_3_1 nrs01_1_4_1 nrs01_1_5_1 nrs01_1_6_1 nrs01_1_7_1 nrs01_1_8_1 nrs01_1_9_1 nrs01_1_10_1 nrs01_1_11_1 nrs01_1_12_1
/*nrs01- I trust that this person will not gossip*/
nrs01_1_1_2 nrs01_1_2_2 nrs01_1_3_2 nrs01_1_4_2 nrs01_1_5_2 nrs01_1_6_2 nrs01_1_7_2 nrs01_1_8_2 nrs01_1_9_2 nrs01_1_10_2 nrs01_1_11_2 nrs01_1_12_2
/*nrs01- I trust that this person will tell me the truth (be honest)*/
nrs01_1_1_3 nrs01_1_2_3 nrs01_1_3_3 nrs01_1_4_3 nrs01_1_5_3 nrs01_1_6_3 nrs01_1_7_3 nrs01_1_8_3 nrs01_1_9_3 nrs01_1_10_3 nrs01_1_11_3 nrs01_1_12_3
/*nrs01- This person is a medical or healthcare professional*/
nrs01_1_1_4 nrs01_1_2_4 nrs01_1_3_4 nrs01_1_4_4 nrs01_1_5_4 nrs01_1_6_4 nrs01_1_7_4 nrs01_1_8_4 nrs01_1_9_4 nrs01_1_10_4 nrs01_1_11_4 nrs01_1_12_4
/*nrs01- This person is my significant other or spouse (former or current)*/
nrs01_1_1_5 nrs01_1_2_5 nrs01_1_3_5 nrs01_1_4_5 nrs01_1_5_5 nrs01_1_6_5 nrs01_1_7_5 nrs01_1_8_5 nrs01_1_9_5 nrs01_1_10_5 nrs01_1_11_5 nrs01_1_12_5
/*nrs01- This person and I have been through or are going through a similar experience*/
nrs01_1_1_6 nrs01_1_2_6 nrs01_1_3_6 nrs01_1_4_6 nrs01_1_5_6 nrs01_1_6_6 nrs01_1_7_6 nrs01_1_8_6 nrs01_1_9_6 nrs01_1_10_6 nrs01_1_11_6 nrs01_1_12_6
/*nrs01- This person is family*/
nrs01_1_1_7 nrs01_1_2_7 nrs01_1_3_7 nrs01_1_4_7 nrs01_1_5_7 nrs01_1_6_7 nrs01_1_7_7 nrs01_1_8_7 nrs01_1_9_7 nrs01_1_10_7 nrs01_1_11_7 nrs01_1_12_7
/*nrs01- This person values my advice and looks towards me for guidance*/
nrs01_1_1_8 nrs01_1_2_8 nrs01_1_3_8 nrs01_1_4_8 nrs01_1_5_8 nrs01_1_6_8 nrs01_1_7_8 nrs01_1_8_8 nrs01_1_9_8 nrs01_1_10_8 nrs01_1_11_8 nrs01_1_12_8
/*nrs01- Other, specify*/
nrs01_2_1_1_TEXT nrs01_2_2_1_TEXT nrs01_2_3_1_TEXT nrs01_2_4_1_TEXT nrs01_2_5_1_TEXT nrs01_2_6_1_TEXT nrs01_2_7_1_TEXT nrs01_2_8_1_TEXT nrs01_2_9_1_TEXT
nrs01_2_10_1_TEXT nrs01_2_11_1_TEXT nrs01_2_12_1_TEXT
/*nrs02- This person and I live together*/
nrs02_1_1_1 nrs02_1_2_1 nrs02_1_3_1 nrs02_1_4_1 nrs02_1_5_1 nrs02_1_6_1 nrs02_1_7_1 nrs02_1_8_1 nrs02_1_9_1 nrs02_1_10_1 nrs02_1_11_1 nrs02_1_12_1
/*nrs02- I enjoy socializing with this person over meals */
nrs02_1_1_2 nrs02_1_2_2 nrs02_1_3_2 nrs02_1_4_2 nrs02_1_5_2 nrs02_1_6_2 nrs02_1_7_2 nrs02_1_8_2 nrs02_1_9_2 nrs02_1_10_2 nrs02_1_11_2 nrs02_1_12_2
/*nrs02- This person often cooks for me*/
nrs02_1_1_3 nrs02_1_2_3 nrs02_1_3_3 nrs02_1_4_3 nrs02_1_5_3 nrs02_1_6_3 nrs02_1_7_3 nrs02_1_8_3 nrs02_1_9_3 nrs02_1_10_3 nrs02_1_11_3 nrs02_1_12_3
/*nrs02- I often cook for this person*/
nrs02_1_1_4 nrs02_1_2_4 nrs02_1_3_4 nrs02_1_4_4 nrs02_1_5_4 nrs02_1_6_4 nrs02_1_7_4 nrs02_1_8_4 nrs02_1_9_4 nrs02_1_10_4 nrs02_1_11_4 nrs02_1_12_4
/*nrs02- This person and I eat similar food, so we often eat together*/
nrs02_1_1_5 nrs02_1_2_5 nrs02_1_3_5 nrs02_1_4_5 nrs02_1_5_5 nrs02_1_6_5 nrs02_1_7_5 nrs02_1_8_5 nrs02_1_9_5 nrs02_1_10_5 nrs02_1_11_5 nrs02_1_12_5
/*nrs02- I am this person's caregiver*/
nrs02_1_1_6 nrs02_1_2_6 nrs02_1_3_6 nrs02_1_4_6 nrs02_1_5_6 nrs02_1_6_6 nrs02_1_7_6 nrs02_1_8_6 nrs02_1_9_6 nrs02_1_10_6 nrs02_1_11_6 nrs02_1_12_6
/*nrs02- Other, specify*/
nrs02_2_1_1_TEXT nrs02_2_2_1_TEXT nrs02_2_3_1_TEXT nrs02_2_4_1_TEXT nrs02_2_5_1_TEXT nrs02_2_6_1_TEXT nrs02_2_7_1_TEXT nrs02_2_8_1_TEXT nrs02_2_9_1_TEXT
nrs02_2_10_1_TEXT nrs02_2_11_1_TEXT nrs02_2_12_1_TEXT
/*nrs03- This person buys food for me*/
nrs03_1_1_1 nrs03_1_2_1 nrs03_1_3_1 nrs03_1_4_1 nrs03_1_5_1 nrs03_1_6_1 nrs03_1_7_1 nrs03_1_8_1 nrs03_1_9_1 nrs03_1_10_1 nrs03_1_11_1 nrs03_1_12_1
/*nrs03- I buy this person food*/
nrs03_1_1_2 nrs03_1_2_2 nrs03_1_3_2 nrs03_1_4_2 nrs03_1_5_2 nrs03_1_6_2 nrs03_1_7_2 nrs03_1_8_2 nrs03_1_9_2 nrs03_1_10_2 nrs03_1_11_2 nrs03_1_12_2
/*nrs03- This person and I live together*/
nrs03_1_1_3 nrs03_1_2_3 nrs03_1_3_3 nrs03_1_4_3 nrs03_1_5_3 nrs03_1_6_3 nrs03_1_7_3 nrs03_1_8_3 nrs03_1_9_3 nrs03_1_10_3 nrs03_1_11_3 nrs03_1_12_3
/*nrs03- I buy healthier food when I shop with this person*/
nrs03_1_1_5 nrs03_1_2_5 nrs03_1_3_5 nrs03_1_4_5 nrs03_1_5_5 nrs03_1_6_5 nrs03_1_7_5 nrs03_1_8_5 nrs03_1_9_5 nrs03_1_10_5 nrs03_1_11_5 nrs03_1_12_5
/*nrs03- I have a car*/
nrs03_1_1_4 nrs03_1_2_4 nrs03_1_3_4 nrs03_1_4_4 nrs03_1_5_4 nrs03_1_6_4 nrs03_1_7_4 nrs03_1_8_4 nrs03_1_9_4 nrs03_1_10_4 nrs03_1_11_4 nrs03_1_12_4
/*nrs03- I have a car*/
nrs03_1_1_5 nrs03_1_2_5 nrs03_1_3_5 nrs03_1_4_5 nrs03_1_5_5 nrs03_1_6_5 nrs03_1_7_5 nrs03_1_8_5 nrs03_1_9_5 nrs03_1_10_5 nrs03_1_11_5 nrs03_1_12_5
/*nrs03- This person has a car */
nrs03_1_1_6 nrs03_1_2_6 nrs03_1_3_6 nrs03_1_4_6 nrs03_1_5_6 nrs03_1_6_6 nrs03_1_7_6 nrs03_1_8_6 nrs03_1_9_6 nrs03_1_10_6 nrs03_1_11_6 nrs03_1_12_6
/*nrs03- Other, specify*/
nrs03_2_1_1_TEXT nrs03_2_2_1_TEXT nrs03_2_3_1_TEXT nrs03_2_4_1_TEXT nrs03_2_5_1_TEXT nrs03_2_6_1_TEXT nrs03_2_7_1_TEXT nrs03_2_8_1_TEXT nrs03_2_9_1_TEXT
nrs03_2_10_1_TEXT nrs03_2_11_1_TEXT nrs03_2_12_1_TEXT
/*nrs04- This person and I are friends*/
nrs04_1_1_1 nrs04_1_2_1 nrs04_1_3_1 nrs04_1_4_1 nrs04_1_5_1 nrs04_1_6_1 nrs04_1_7_1 nrs04_1_8_1 nrs04_1_9_1 nrs04_1_10_1 nrs04_1_11_1 nrs04_1_12_1
/*nrs04- This person and I are family members*/
nrs04_1_1_2 nrs04_1_2_2 nrs04_1_3_2 nrs04_1_4_2 nrs04_1_5_2 nrs04_1_6_2 nrs04_1_7_2 nrs04_1_8_2 nrs04_1_9_2 nrs04_1_10_2 nrs04_1_11_2 nrs04_1_12_2
/*nrs04- This person and I are romantic partners*/
nrs04_1_1_3 nrs04_1_2_3 nrs04_1_3_3 nrs04_1_4_3 nrs04_1_5_3 nrs04_1_6_3 nrs04_1_7_3 nrs04_1_8_3 nrs04_1_9_3 nrs04_1_10_3 nrs04_1_11_3 nrs04_1_12_3
/*nrs04- This person often babysits my child(ren)*/
nrs04_1_1_4 nrs04_1_2_4 nrs04_1_3_4 nrs04_1_4_4 nrs04_1_5_4 nrs04_1_6_4 nrs04_1_7_4 nrs04_1_8_4 nrs04_1_9_4 nrs04_1_10_4 nrs04_1_11_4 nrs04_1_12_4
/*nrs04- I often babysit this person's child(ren)*/
nrs04_1_1_5 nrs04_1_2_5 nrs04_1_3_5 nrs04_1_4_5 nrs04_1_5_5 nrs04_1_6_5 nrs04_1_7_5 nrs04_1_8_5 nrs04_1_9_5 nrs04_1_10_5 nrs04_1_11_5 nrs04_1_12_5
/*nrs04- This person provides me with transporation*/
nrs04_1_1_6 nrs04_1_2_6 nrs04_1_3_6 nrs04_1_4_6 nrs04_1_5_6 nrs04_1_6_6 nrs04_1_7_6 nrs04_1_8_6 nrs04_1_9_6 nrs04_1_10_6 nrs04_1_11_6 nrs04_1_12_6
/*nrs04- I often provide transporation for this person*/
nrs04_1_1_7 nrs04_1_2_7 nrs04_1_3_7 nrs04_1_4_7 nrs04_1_5_7 nrs04_1_6_7 nrs04_1_7_7 nrs04_1_8_7 nrs04_1_9_7 nrs04_1_10_7 nrs04_1_11_7 nrs04_1_12_7
/*nrs04- This person provides me with emotional support or encouragement*/
nrs04_1_1_8 nrs04_1_2_8 nrs04_1_3_8 nrs04_1_4_8 nrs04_1_5_8 nrs04_1_6_8 nrs04_1_7_8 nrs04_1_8_8 nrs04_1_9_8 nrs04_1_10_8 nrs04_1_11_8 nrs04_1_12_8
/*nrs04- This person provvides me with financial support*/
nrs04_1_1_9 nrs04_1_2_9 nrs04_1_3_9 nrs04_1_4_9 nrs04_1_5_9 nrs04_1_6_9 nrs04_1_7_9 nrs04_1_8_9 nrs04_1_9_9 nrs04_1_10_9 nrs04_1_11_9 nrs04_1_12_9
/*nrs04- This person depends on me for help (emotional, financial, or otherwise)*/
nrs04_1_1_10 nrs04_1_2_10 nrs04_1_3_10 nrs04_1_4_10 nrs04_1_5_10 nrs04_1_6_10 nrs04_1_7_10 nrs04_1_8_10 nrs04_1_9_10 nrs04_1_10_10 nrs04_1_11_10 nrs04_1_12_10
/*nrs04- Other, specify*/
nrs04_2_1_1_TEXT nrs04_2_2_1_TEXT nrs04_2_3_1_TEXT nrs04_2_4_1_TEXT nrs04_2_5_1_TEXT nrs04_2_6_1_TEXT nrs04_2_7_1_TEXT nrs04_2_8_1_TEXT nrs04_2_9_1_TEXT nrs04_2_10_1_TEXT
nrs04_2_11_1_TEXT nrs04_2_12_1_TEXT;
run;

data check;
set have;
array names1 (*)q1a q1b q1c q1d q1e q1f q1g q1h q1i q1j q1k q1l;

array nrs01_1_recode(*) nrs01_1_1_1 nrs01_1_2_1 nrs01_1_3_1 nrs01_1_4_1 nrs01_1_5_1 nrs01_1_6_1 nrs01_1_7_1 nrs01_1_8_1 nrs01_1_9_1 nrs01_1_10_1 nrs01_1_11_1 nrs01_1_12_1;

array nrs01_2_recode(*) nrs01_1_1_2 nrs01_1_2_2 nrs01_1_3_2 nrs01_1_4_2 nrs01_1_5_2 nrs01_1_6_2 nrs01_1_7_2 nrs01_1_8_2 nrs01_1_9_2 nrs01_1_10_2 nrs01_1_11_2 nrs01_1_12_2;

array nrs01_3_recode(*) nrs01_1_1_3 nrs01_1_2_3 nrs01_1_3_3 nrs01_1_4_3 nrs01_1_5_3 nrs01_1_6_3 nrs01_1_7_3 nrs01_1_8_3 nrs01_1_9_3 nrs01_1_10_3 nrs01_1_11_3 nrs01_1_12_3;

array nrs01_4_recode(*) nrs01_1_1_4 nrs01_1_2_4 nrs01_1_3_4 nrs01_1_4_4 nrs01_1_5_4 nrs01_1_6_4 nrs01_1_7_4 nrs01_1_8_4 nrs01_1_9_4 nrs01_1_10_4 nrs01_1_11_4 nrs01_1_12_4;

array nrs01_5_recode(*) nrs01_1_1_5 nrs01_1_2_5 nrs01_1_3_5 nrs01_1_4_5 nrs01_1_5_5 nrs01_1_6_5 nrs01_1_7_5 nrs01_1_8_5 nrs01_1_9_5 nrs01_1_10_5 nrs01_1_11_5 nrs01_1_12_5;

array nrs01_6_recode(*) nrs01_1_1_6 nrs01_1_2_6 nrs01_1_3_6 nrs01_1_4_6 nrs01_1_5_6 nrs01_1_6_6 nrs01_1_7_6 nrs01_1_8_6 nrs01_1_9_6 nrs01_1_10_6 nrs01_1_11_6 nrs01_1_12_6;

array nrs01_7_recode(*) nrs01_1_1_7 nrs01_1_2_7 nrs01_1_3_7 nrs01_1_4_7 nrs01_1_5_7 nrs01_1_6_7 nrs01_1_7_7 nrs01_1_8_7 nrs01_1_9_7 nrs01_1_10_7 nrs01_1_11_7 nrs01_1_12_7;

array nrs01_8_recode(*) nrs01_1_1_8 nrs01_1_2_8 nrs01_1_3_8 nrs01_1_4_8 nrs01_1_5_8 nrs01_1_6_8 nrs01_1_7_8 nrs01_1_8_8 nrs01_1_9_8 nrs01_1_10_8 nrs01_1_11_8 nrs01_1_12_8;

array nrs01_9_recode(*) nrs01_2_1_1_TEXT nrs01_2_2_1_TEXT nrs01_2_3_1_TEXT nrs01_2_4_1_TEXT nrs01_2_5_1_TEXT nrs01_2_6_1_TEXT nrs01_2_7_1_TEXT nrs01_2_8_1_TEXT nrs01_2_9_1_TEXT
nrs01_2_10_1_TEXT nrs01_2_11_1_TEXT nrs01_2_12_1_TEXT;


array names2 (*) q2a q2b q2c q2d q2e q2f q2g q2h q2i q2j q2k q2l;

array nrs02_1_recode(*) nrs02_1_1_1 nrs02_1_2_1 nrs02_1_3_1 nrs02_1_4_1 nrs02_1_5_1 nrs02_1_6_1 nrs02_1_7_1 nrs02_1_8_1 nrs02_1_9_1 nrs02_1_10_1 nrs02_1_11_1 nrs02_1_12_1;

array nrs02_2_recode(*) nrs02_1_1_2 nrs02_1_2_2 nrs02_1_3_2 nrs02_1_4_2 nrs02_1_5_2 nrs02_1_6_2 nrs02_1_7_2 nrs02_1_8_2 nrs02_1_9_2 nrs02_1_10_2 nrs02_1_11_2 nrs02_1_12_2;

array nrs02_3_recode(*) nrs02_1_1_3 nrs02_1_2_3 nrs02_1_3_3 nrs02_1_4_3 nrs02_1_5_3 nrs02_1_6_3 nrs02_1_7_3 nrs02_1_8_3 nrs02_1_9_3 nrs02_1_10_3 nrs02_1_11_3 nrs02_1_12_3;

array nrs02_4_recode(*) nrs02_1_1_4 nrs02_1_2_4 nrs02_1_3_4 nrs02_1_4_4 nrs02_1_5_4 nrs02_1_6_4 nrs02_1_7_4 nrs02_1_8_4 nrs02_1_9_4 nrs02_1_10_4 nrs02_1_11_4 nrs02_1_12_4;

array nrs02_5_recode(*) nrs02_1_1_5 nrs02_1_2_5 nrs02_1_3_5 nrs02_1_4_5 nrs02_1_5_5 nrs02_1_6_5 nrs02_1_7_5 nrs02_1_8_5 nrs02_1_9_5 nrs02_1_10_5 nrs02_1_11_5 nrs02_1_12_5;

array nrs02_6_recode(*) nrs02_1_1_6 nrs02_1_2_6 nrs02_1_3_6 nrs02_1_4_6 nrs02_1_5_6 nrs02_1_6_6 nrs02_1_7_6 nrs02_1_8_6 nrs02_1_9_6 nrs02_1_10_6 nrs02_1_11_6 nrs02_1_12_6;

array nrs02_7_recode(*) nrs02_2_1_1_TEXT nrs02_2_2_1_TEXT nrs02_2_3_1_TEXT nrs02_2_4_1_TEXT nrs02_2_5_1_TEXT nrs02_2_6_1_TEXT nrs02_2_7_1_TEXT nrs02_2_8_1_TEXT nrs02_2_9_1_TEXT
nrs02_2_10_1_TEXT nrs02_2_11_1_TEXT nrs02_2_12_1_TEXT;

array names3 (*) q3a q3b q3c q3d q3e q3f q3g q3h q3i q3j q3k q3l;

array nrs03_1_recode(*) nrs03_1_1_1 nrs03_1_2_1 nrs03_1_3_1 nrs03_1_4_1 nrs03_1_5_1 nrs03_1_6_1 nrs03_1_7_1 nrs03_1_8_1 nrs03_1_9_1 nrs03_1_10_1 nrs03_1_11_1 nrs03_1_12_1;

array nrs03_2_recode(*) nrs03_1_1_2 nrs03_1_2_2 nrs03_1_3_2 nrs03_1_4_2 nrs03_1_5_2 nrs03_1_6_2 nrs03_1_7_2 nrs03_1_8_2 nrs03_1_9_2 nrs03_1_10_2 nrs03_1_11_2 nrs03_1_12_2;

array nrs03_3_recode(*) nrs03_1_1_3 nrs03_1_2_3 nrs03_1_3_3 nrs03_1_4_3 nrs03_1_5_3 nrs03_1_6_3 nrs03_1_7_3 nrs03_1_8_3 nrs03_1_9_3 nrs03_1_10_3 nrs03_1_11_3 nrs03_1_12_3;

array nrs03_4_recode(*) nrs03_1_1_4 nrs03_1_2_4 nrs03_1_3_4 nrs03_1_4_4 nrs03_1_5_4 nrs03_1_6_4 nrs03_1_7_4 nrs03_1_8_4 nrs03_1_9_4 nrs03_1_10_4 nrs03_1_11_4 nrs03_1_12_4;

array nrs03_5_recode(*) nrs03_1_1_5 nrs03_1_2_5 nrs03_1_3_5 nrs03_1_4_5 nrs03_1_5_5 nrs03_1_6_5 nrs03_1_7_5 nrs03_1_8_5 nrs03_1_9_5 nrs03_1_10_5 nrs03_1_11_5 nrs03_1_12_5;

array nrs03_6_recode(*) nrs03_1_1_6 nrs03_1_2_6 nrs03_1_3_6 nrs03_1_4_6 nrs03_1_5_6 nrs03_1_6_6 nrs03_1_7_6 nrs03_1_8_6 nrs03_1_9_6 nrs03_1_10_6 nrs03_1_11_6 nrs03_1_12_6;

array nrs03_7_recode(*) nrs03_2_1_1_TEXT nrs03_2_2_1_TEXT nrs03_2_3_1_TEXT nrs03_2_4_1_TEXT nrs03_2_5_1_TEXT nrs03_2_6_1_TEXT nrs03_2_7_1_TEXT nrs03_2_8_1_TEXT nrs03_2_9_1_TEXT
nrs03_2_10_1_TEXT nrs03_2_11_1_TEXT nrs03_2_12_1_TEXT;

array names4 (*) q4a q4b q4c q4d q4e q4f q4g q4h q4i q4j q4k q4l;

array nrs04_1_recode(*) nrs04_1_1_1 nrs04_1_2_1 nrs04_1_3_1 nrs04_1_4_1 nrs04_1_5_1 nrs04_1_6_1 nrs04_1_7_1 nrs04_1_8_1 nrs04_1_9_1 nrs04_1_10_1 nrs04_1_11_1 nrs04_1_12_1 nrs04_1_13_1;

array nrs04_2_recode(*) nrs04_1_1_2 nrs04_1_2_2 nrs04_1_3_2 nrs04_1_4_2 nrs04_1_5_2 nrs04_1_6_2 nrs04_1_7_2 nrs04_1_8_2 nrs04_1_9_2 nrs04_1_10_2 nrs04_1_11_2 nrs04_1_12_2 nrs04_1_13_2;

array nrs04_3_recode(*) nrs04_1_1_3 nrs04_1_2_3 nrs04_1_3_3 nrs04_1_4_3 nrs04_1_5_3 nrs04_1_6_3 nrs04_1_7_3 nrs04_1_8_3 nrs04_1_9_3 nrs04_1_10_3 nrs04_1_11_3 nrs04_1_12_3 nrs04_1_13_3;

array nrs04_4_recode(*) nrs04_1_1_4 nrs04_1_2_4 nrs04_1_3_4 nrs04_1_4_4 nrs04_1_5_4 nrs04_1_6_4 nrs04_1_7_4 nrs04_1_8_4 nrs04_1_9_4 nrs04_1_10_4 nrs04_1_11_4 nrs04_1_12_4 nrs04_1_13_4;

array nrs04_5_recode(*) nrs04_1_1_5 nrs04_1_2_5 nrs04_1_3_5 nrs04_1_4_5 nrs04_1_5_5 nrs04_1_6_5 nrs04_1_7_5 nrs04_1_8_5 nrs04_1_9_5 nrs04_1_10_5 nrs04_1_11_5 nrs04_1_12_5 nrs04_1_13_5;

array nrs04_6_recode(*) nrs04_1_1_6 nrs04_1_2_6 nrs04_1_3_6 nrs04_1_4_6 nrs04_1_5_6 nrs04_1_6_6 nrs04_1_7_6 nrs04_1_8_6 nrs04_1_9_6 nrs04_1_10_6 nrs04_1_11_6 nrs04_1_12_6 nrs04_1_13_6;

array nrs04_7_recode(*) nrs04_1_1_7 nrs04_1_2_7 nrs04_1_3_7 nrs04_1_4_7 nrs04_1_5_7 nrs04_1_6_7 nrs04_1_7_7 nrs04_1_8_7 nrs04_1_9_7 nrs04_1_10_7 nrs04_1_11_7 nrs04_1_12_7 nrs04_1_13_7;


array nrs04_8_recode(*) nrs04_1_1_8 nrs04_1_2_8 nrs04_1_3_8 nrs04_1_4_8 nrs04_1_5_8 nrs04_1_6_8 nrs04_1_7_8 nrs04_1_8_8 nrs04_1_9_8 nrs04_1_10_8 nrs04_1_11_8 nrs04_1_12_8 nrs04_1_13_8;

array nrs04_9_recode(*) nrs04_1_1_9 nrs04_1_2_9 nrs04_1_3_9 nrs04_1_4_9 nrs04_1_5_9 nrs04_1_6_9 nrs04_1_7_9 nrs04_1_8_9 nrs04_1_9_9 nrs04_1_10_9 nrs04_1_11_9 nrs04_1_12_9 nrs04_1_13_9;

array nrs04_10_recode(*) nrs04_1_1_10 nrs04_1_2_10 nrs04_1_3_10 nrs04_1_4_10 nrs04_1_5_10 nrs04_1_6_10 nrs04_1_7_10 nrs04_1_8_10 nrs04_1_9_10 nrs04_1_10_10 nrs04_1_11_10 nrs04_1_12_10 nrs04_1_13_10;

array nrs04_11_recode(*) nrs04_2_1_1_TEXT nrs04_2_2_1_TEXT nrs04_2_3_1_TEXT nrs04_2_4_1_TEXT nrs04_2_5_1_TEXT nrs04_2_6_1_TEXT nrs04_2_7_1_TEXT nrs04_2_8_1_TEXT nrs04_2_9_1_TEXT nrs04_2_10_1_TEXT
nrs04_2_11_1_TEXT nrs04_2_12_1_TEXT;
do i=1 to 12;
nrs01_1= nrs01_1_recode[i];
nrs01_2= nrs01_2_recode[i];
nrs01_3= nrs01_3_recode[i];
nrs01_4= nrs01_4_recode[i];
nrs01_5= nrs01_5_recode[i];
nrs01_6= nrs01_6_recode[i];
nrs01_7= nrs01_7_recode[i];
nrs01_8= nrs01_8_recode[i];
nrs01_9= nrs01_9_recode[i];
names_1= names1[i];
sum_nrs_01= sum ( nrs01_1, nrs01_2, nrs01_3, nrs01_4, nrs01_5, nrs01_6, nrs01_7, nrs01_8);


nrs02_1= nrs02_1_recode[i];
nrs02_2= nrs02_2_recode[i];
nrs02_3= nrs02_3_recode[i];
nrs02_4= nrs02_4_recode[i];
nrs02_5= nrs02_5_recode[i];
nrs02_6= nrs02_6_recode[i];
nrs02_7= nrs02_7_recode[i];
names_2= names2[i];

sum_nrs_02= sum ( nrs02_1, nrs02_2, nrs02_3, nrs02_4, nrs02_5, nrs02_6);

nrs03_1= nrs03_1_recode[i];
nrs03_2= nrs03_2_recode[i];
nrs03_3= nrs03_3_recode[i];
nrs03_4= nrs03_4_recode[i];
nrs03_5= nrs03_5_recode[i];
nrs03_6= nrs03_6_recode[i];
nrs03_7= nrs03_7_recode[i];
names_3= names3[i];

sum_nrs_03= sum ( nrs03_1, nrs03_2, nrs03_3, nrs03_4, nrs03_5, nrs03_6);

nrs04_1= nrs04_1_recode[i];
nrs04_2= nrs04_2_recode[i];
nrs04_3= nrs04_3_recode[i];
nrs04_4= nrs04_4_recode[i];
nrs04_5= nrs04_5_recode[i];
nrs04_6= nrs04_6_recode[i];
nrs04_7= nrs04_7_recode[i];
nrs04_8= nrs04_8_recode[i];
nrs04_9= nrs04_9_recode[i];
nrs04_10= nrs04_10_recode[i];
nrs04_11= nrs04_11_recode[i];
names_4= names4[i];

sum_nrs_04= sum ( nrs04_1, nrs04_2, nrs04_3, nrs04_4, nrs04_5, nrs04_6, nrs04_7, nrs04_8, nrs04_9, nrs04_10); 

 

First1= SCAN(name1 [i], 1, " ");

Last1= SCAN (names1[i], 2 " "); 

First2= SCAN(names2[i], 1, " ");
Last2= SCAN(names2[i], 2, " ");
First3= SCAN(names3[i], 1, " ");
Last3= SCAN(names3[i], 2, " ");
First4= SCAN(names4[i], 1, " ");
Last4= SCAN(names4[i], 2, " ");

 

Rename first1= alter_first first2= alter_first first3= alter_first first4= alter_first last1= alter_last last2= alter_last last3= alter_last last4= alter_last;

output; end;

Super Contributor
Posts: 259

Re: Create one variable from multiple arrays

I recommend stepping back to the point before you created that 5k-vars dataset. Post the structure of the datasets before creating that monster - sample data is highly appreciated - i am sure that the whole problem will be easier to solve with smaller datasets.

 

Occasional Contributor
Posts: 14

Re: Create one variable from multiple arrays

The information is HIPAA protected and so I'm not sure if I can provide a sample of the data. I was able to resolve the issue by creating 4 smaller data sets and merging those into the larger restructured one by a unique ID.

I'd like some advice for the future on creating one variable from several variables that don't overlap.


The following process doesn't work, and so I'd like to know if alternatives exist!

Rename first1= alter_first first2= alter_first first3= alter_first first4= alter_first last1= alter_last last2= alter_last last3= alter_last last4= alter_last;
PROC Star
Posts: 1,085

Re: Create one variable from multiple arrays

I advise you to never post actual data. Just take the structure of your datasets, and populate with some synthetic data where the keys and fields match where necessary. A few records is all that's usually needed.

Super User
Posts: 10,458

Re: Create one variable from multiple arrays


Dbynoe wrote:

The following process doesn't work, and so I'd like to know if alternatives exist!

Rename first1= alter_first first2= alter_first first3= alter_first first4= alter_first last1= alter_last last2= alter_last last3= alter_last last4= alter_last;

Fails because rename requires that only one variable can be renamed to a specific name so . So you are getting a warning similar to:

WARNING: Variable first2 cannot be renamed to alter_first because alter_first already exists.

And onlyt the first1 gets renamed.

Since you are starting with 4 variables (two times) and reducing to one you really need to provide some example of what you have and what you want as this single step makes no sense. If you have a rule on how to build alter_first given 4 other varaibles then that works: apply the rule(s) and drop the first1 first2 etc.

 

 

Of course since you have a keep statement that does not include alter_first or alter_last or any of the first/last variable the whole assignment is kind of moot.

Occasional Contributor
Posts: 14

Re: Create one variable from multiple arrays

I've used the rename variable several times without prior references. The new variable doesn't need to be intialized prior. In fact, the first1= alter_first and first2= alter_last work fine, the reamining variables in the rename function don't. I understand that the rename function is not appropriate, which is why I'm asking for alternatives. 

 

I've resolved the issue by creating smaller subsets and then merging them to the larger data set by a unique identifier; however, I'd like to know a more efficient way for future reference. 

 

Below is an example of the data set:

 

ID visit nrs01_1 nrs01_2 nrs01_3 nrs03_1 nrs03_2 nrs04_1 nrs04_2 q1a q3b q4a

1    0       1           .                1          1            .               1           1        Joe H  Samantha B Jodi R

1    1       .           1                1          .            1               1            .        Joe H  Samantha B Jodi R

 

 

What I want it to look like:

 

alter_first alter_last ID visit nrs01_1 nrs01_2 nrs01_3 nrs03_2 nrs04_1 nrs04_2

Joe            H            1     0      1             .         1       .         .           .             .

Samatha    B            1     0     .               .          .                    .       .              .

Jodi            R           1     0     .                .          .        .          .        1             1

 

Joe            H            1     1      .            1          1       .         .           .             .

Samatha    B            1     1     .               .          .                1       .              .

Jodi            R           1     1    .                .          .        .          .        1            .

 

Thanks!

Respected Advisor
Posts: 4,640

Re: Create one variable from multiple arrays

A first step to fix the data structure a bit (it is still very fragile, in my opinion) makes things easier:

 

data have;
input ID visit nrs01_1 nrs01_2 nrs01_3 nrs03_1 nrs03_2 nrs04_1 nrs04_2 (q1a q3b q4a) (&:$20.);
datalines;
1    0       1           .                1          1            .               1           1        Joe H  Samantha B  Jodi R
1    1       .           1                1          .            1               1            .        Joe H  Samantha B  Jodi R
;

data step1;
set have;
array nrs_ nrs:;
array q_ q:;
length code_name $32;
nrs_Id = 1;
do q_Id = 1 to dim(q_);
    real_name = q_{q_Id};
    do until(scan(code_name,1,"_") ne scan(vname(nrs_{nrs_Id}),1,"_"));
        code_name = vname(nrs_{nrs_Id});
        value = nrs_{nrs_Id};
        output;
        nrs_Id = nrs_Id + 1;
        if nrs_Id > dim(nrs_) then leave;
        end;
    end;
keep id visit real_name code_name value;
run;

proc sort data=step1; by id visit real_name code_name; run;

proc transpose data=step1 out=step2(drop=_name_);
by id visit real_name;
var value;
id code_name;
run;

and so on...

 

PG
Ask a Question
Discussion stats
  • 6 replies
  • 185 views
  • 0 likes
  • 5 in conversation