Hello programmers,
I believe i am closer to what i want to implement but i may be missing something and i need help.
I want to string out my dataset where the testcodes (tc) include not only my heart variables (heart1-heart6) but BMI as well.
My heart variables are coded as dichotomous (1/0) while BMI is continuous.
I am trying to modify my array and proc format by adding BMI to the end of both my array and format but i have not been successful.
data me;
input id sex race agecat health bmi bmigroup heart1 heart2 heart3 heart4 heart5 heart6 death tc Tn $ value;
datalines;
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 1 Angina 0
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 2 Heartburn 1
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 3 Sleepiness 0
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 4 Exercise 0
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 5 Palpitation 1
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 6 Any 1
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 1 Angina 0
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 2 Heartburn 0
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 3 Sleepiness 0
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 4 Exercise 1
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 5 Palpitation 0
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 6 Any 1
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 1 Angina 0
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 2 Heartburn 1
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 3 Sleepiness 0
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 4 Exercise 0
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 5 Palpitation 0
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 6 Any 1
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 1 Angina 0
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 2 Heartburn 0
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 3 Sleepiness 1
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 4 Exercise 1
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 5 Palpitation 0
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 6 Any 1
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 1 Angina 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 2 Heartburn 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 3 Sleepiness 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 4 Exercise 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 5 Palpitation 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 6 Any 0
; run;
proc print; run;
/*format and stringing out*/
Proc format;
value testcodefm
1= 'Angina'
2= 'Heartburn'
3= 'Sleepiness'
4= 'Exercise'
5= 'Palpitation'
6= 'Any'
7= 'BMI'
;
data You; set me;
array him heart1-heart6 BMI;
do over him;
tc=_I_;
tn= put(tc,testcodefm.);
value=him;
output; end;
proc print; run;
If you use tabs in datalines, you need to tell SAS so:
data me;
infile datalines dlm='09'x dsd;
input id sex race agecat health bmi bmigroup heart1 heart2 heart3 heart4 heart5 heart6 death tc Tn $ value;
datalines;
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 1 Angina 0
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 2 Heartburn 1
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 3 Sleepiness 0
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 4 Exercise 0
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 5 Palpitation 1
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 6 Any 1
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 1 Angina 0
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 2 Heartburn 0
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 3 Sleepiness 0
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 4 Exercise 1
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 5 Palpitation 0
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 6 Any 1
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 1 Angina 0
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 2 Heartburn 1
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 3 Sleepiness 0
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 4 Exercise 0
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 5 Palpitation 0
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 6 Any 1
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 1 Angina 0
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 2 Heartburn 0
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 3 Sleepiness 1
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 4 Exercise 1
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 5 Palpitation 0
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 6 Any 1
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 1 Angina 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 2 Heartburn 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 3 Sleepiness 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 4 Exercise 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 5 Palpitation 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 6 Any 0
;
A little visual formatting goes a long way in making code more readable:
/*format and stringing out*/
Proc format;
value testcodefm
1= 'Angina'
2= 'Heartburn'
3= 'Sleepiness'
4= 'Exercise'
5= 'Palpitation'
6= 'Any'
7= 'BMI'
;
run;
data You;
set me;
array him heart1-heart6 BMI;
do over him;
tc = _I_;
tn = put(tc,testcodefm.);
value = him;
output;
end;
keep
run;
proc print data=you;
run;
As you can see form this (partial) result of the above, BMI is the last entry in a group:
Beob. id sex race agecat health bmi bmigroup heart1 heart2 heart3 heart4 heart5 heart6 death tc Tn value 1 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 1 Angina 0.0000 2 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 2 Heartbur 1.0000 3 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 3 Sleepine 0.0000 4 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 4 Exercise 0.0000 5 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 5 Palpitat 1.0000 6 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 6 Any 1.0000 7 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 7 BMI 22.4940
If this is not the result you want, please post an example for that, based on your input data.
PS you did not define tn with a sufficient length, so the values are truncated.
I don't understand this. What does your desired result look like given this data?
If you use tabs in datalines, you need to tell SAS so:
data me;
infile datalines dlm='09'x dsd;
input id sex race agecat health bmi bmigroup heart1 heart2 heart3 heart4 heart5 heart6 death tc Tn $ value;
datalines;
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 1 Angina 0
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 2 Heartburn 1
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 3 Sleepiness 0
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 4 Exercise 0
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 5 Palpitation 1
1 0 0 5 4 22.494 2 0 1 0 0 1 1 1 6 Any 1
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 1 Angina 0
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 2 Heartburn 0
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 3 Sleepiness 0
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 4 Exercise 1
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 5 Palpitation 0
2 1 0 5 2 20.1801 1 0 0 0 1 0 1 0 6 Any 1
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 1 Angina 0
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 2 Heartburn 1
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 3 Sleepiness 0
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 4 Exercise 0
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 5 Palpitation 0
3 1 0 4 2 26.3606 4 0 1 0 0 0 1 0 6 Any 1
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 1 Angina 0
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 2 Heartburn 0
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 3 Sleepiness 1
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 4 Exercise 1
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 5 Palpitation 0
4 0 0 4 3 25.373 3 0 0 1 1 0 1 0 6 Any 1
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 1 Angina 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 2 Heartburn 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 3 Sleepiness 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 4 Exercise 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 5 Palpitation 0
6 0 0 4 1 24.8608 3 0 0 0 0 0 0 0 6 Any 0
;
A little visual formatting goes a long way in making code more readable:
/*format and stringing out*/
Proc format;
value testcodefm
1= 'Angina'
2= 'Heartburn'
3= 'Sleepiness'
4= 'Exercise'
5= 'Palpitation'
6= 'Any'
7= 'BMI'
;
run;
data You;
set me;
array him heart1-heart6 BMI;
do over him;
tc = _I_;
tn = put(tc,testcodefm.);
value = him;
output;
end;
keep
run;
proc print data=you;
run;
As you can see form this (partial) result of the above, BMI is the last entry in a group:
Beob. id sex race agecat health bmi bmigroup heart1 heart2 heart3 heart4 heart5 heart6 death tc Tn value 1 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 1 Angina 0.0000 2 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 2 Heartbur 1.0000 3 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 3 Sleepine 0.0000 4 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 4 Exercise 0.0000 5 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 5 Palpitat 1.0000 6 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 6 Any 1.0000 7 1 0 0 5 4 22.4940 2 0 1 0 0 1 1 1 7 BMI 22.4940
If this is not the result you want, please post an example for that, based on your input data.
PS you did not define tn with a sufficient length, so the values are truncated.
Thanks. Your insight was helpful.
Please how can i write an output statement to output the BMI as a testcode with a value.
@ChuksManuel wrote:
Thanks. Your insight was helpful.
Please how can i write an output statement to output the BMI as a testcode with a value.
But you already have that. See my previous post.
If this is not what you want, then please (I repeat myself here) post the expected output.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.