07-29-2014 01:01 AM
data pat;
do gp=1 to 2;
if gp=1 then gp="Live";
if gp=2 then gp="Death";
do np=1 to 8;
input ch @;
output;
end;
end;
cards;
1 2 6 2 3 8 5 1 7
2 5 5 3 2 7 5 6 3
;
run;
proc print;
run;
I want all gps that equal to 1 to be seen as Live and gps that are equal to 2 to be seen as Death on the printed table. How do I go about it, my try( line in red) didn't work.
07-29-2014 06:25 AM
You can use character variables in the do loop. Note the extra space in "Live ".
data pat;
do gp="Live ", "Death";
do np=1 to 8;
input ch @;
output;
end;
end;
cards;
1 2 6 2 3 8 5 1 7
2 5 5 3 2 7 5 6 3
;
run;
proc print;
run;
Result
The SAS System
Obs | gp | np | ch |
1 | Live | 1 | 1 |
2 | Live | 2 | 2 |
3 | Live | 3 | 6 |
4 | Live | 4 | 2 |
5 | Live | 5 | 3 |
6 | Live | 6 | 8 |
7 | Live | 7 | 5 |
8 | Live | 8 | 1 |
9 | Death | 1 | 7 |
10 | Death | 2 | 2 |
11 | Death | 3 | 5 |
12 | Death | 4 | 5 |
13 | Death | 5 | 3 |
14 | Death | 6 | 2 |
15 | Death | 7 | 7 |
16 | Death | 8 | 5 |
Richard
Message was edited by: Richard Carson - removed duplicate code
07-29-2014 04:21 AM
There is no red in your screenshot. What is it your trying to achieve, please post example output. From the above your code is a bit mixed up, gp is not explicitly defined, so the do loop creates it as numeric, then you try to assign character to it. Your if statements do not match up, which reads oddly, it should be an if / else if combo. The below runs, though not sure its what you want out:
data pat;
attrib gpc format=$20.;
do gp=1 to 2;
if gp=1 then gpc="Live";
if gp=2 then gpc="Death";
do np=1 to 8;
input ch @;
output;
end;
end;
cards;
1 2 6 2 3 8 5 1 7
2 5 5 3 2 7 5 6 3
;
run;
07-29-2014 06:25 AM
You can use character variables in the do loop. Note the extra space in "Live ".
data pat;
do gp="Live ", "Death";
do np=1 to 8;
input ch @;
output;
end;
end;
cards;
1 2 6 2 3 8 5 1 7
2 5 5 3 2 7 5 6 3
;
run;
proc print;
run;
Result
The SAS System
Obs | gp | np | ch |
1 | Live | 1 | 1 |
2 | Live | 2 | 2 |
3 | Live | 3 | 6 |
4 | Live | 4 | 2 |
5 | Live | 5 | 3 |
6 | Live | 6 | 8 |
7 | Live | 7 | 5 |
8 | Live | 8 | 1 |
9 | Death | 1 | 7 |
10 | Death | 2 | 2 |
11 | Death | 3 | 5 |
12 | Death | 4 | 5 |
13 | Death | 5 | 3 |
14 | Death | 6 | 2 |
15 | Death | 7 | 7 |
16 | Death | 8 | 5 |
Richard
Message was edited by: Richard Carson - removed duplicate code
07-29-2014 07:01 AM
You could also put a length statement before the do loop to avoid having to pad out to the width you want.
07-29-2014 07:24 AM
Just make a format.
proc format;
value gp 1='Life' 2='Death';
run;
Need further help from the community? Please ask a new question.