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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

I don't understand this. What does your desired result look like given this data?

Kurt_Bremser
Super User

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.

ChuksManuel
Pyrite | Level 9

Thanks. Your insight was helpful.

 

Please how can i write an output statement to output the BMI as a testcode with a value.

 

 

Kurt_Bremser
Super User

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

SAS Innovate 2025: Register Now

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!

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
  • 4 replies
  • 670 views
  • 1 like
  • 3 in conversation