Change do loop ranges to strings sas

Accepted Solution Solved
Reply
N/A
Posts: 1
Accepted Solution

Change do loop ranges to strings sas

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.


Accepted Solutions
Solution
‎07-29-2014 06:25 AM
Super Contributor
Posts: 644

Re: Change do loop ranges to strings sas

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

View solution in original post


All Replies
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Change do loop ranges to strings sas

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;

Solution
‎07-29-2014 06:25 AM
Super Contributor
Posts: 644

Re: Change do loop ranges to strings sas

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

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Change do loop ranges to strings sas

You could also put a length statement before the do loop to avoid having to pad out to the width you want.

Respected Advisor
Posts: 3,775

Re: Change do loop ranges to strings sas

Just make a format.

proc format;

  value gp 1='Life' 2='Death';

  run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 224 views
  • 3 likes
  • 4 in conversation