## Create variable in do loop in data step?

Solved
Super Contributor
Posts: 383

# Create variable in do loop in data step?

Is it possible to create a variable 'risk' in data step for the range from 0.1 thru 2 by 0.1 increment where i=20.
However, code below runs non-stop.
Any suggestion?

``````data temp;
input change;
datalines;
change01
change02
change03
change04
change05
change06
change07
change08
change09
change10
change11
change12
change13
change14
change15
change16
change17
change18
change19
change20
;

data temp1; set temp;
do risk = 0.1 to 2 by 0.1;
end;
run;``````

Accepted Solutions
Solution
‎02-10-2018 10:18 PM
Super User
Posts: 2,061

## Re: Create variable in do loop in data step?

do you mean this?

``````data temp;
input change \$10.;
datalines;
change01
change02
change03
change04
change05
change06
change07
change08
change09
change10
change11
change12
change13
change14
change15
change16
change17
change18
change19
change20
;

data want;
do risk=0.1 to 2 by 0.1 until(last);
set temp end=last;
output;
end;
run;``````

All Replies
Solution
‎02-10-2018 10:18 PM
Super User
Posts: 2,061

## Re: Create variable in do loop in data step?

do you mean this?

``````data temp;
input change \$10.;
datalines;
change01
change02
change03
change04
change05
change06
change07
change08
change09
change10
change11
change12
change13
change14
change15
change16
change17
change18
change19
change20
;

data want;
do risk=0.1 to 2 by 0.1 until(last);
set temp end=last;
output;
end;
run;``````
Super Contributor
Posts: 383

## Re: Create variable in do loop in data step?

exactly, why do statement comes in between data and set? how to master 'do' statement systematically? from the beginning
Super User
Posts: 2,061

## Re: Create variable in do loop in data step?

[ Edited ]

I learned most of SAS by reading the book "Learning SAS by example" by author ron cody and a book by Art carpenter. If you could get those two books, read page by page, you will be coding here and helping others. HTH

PS Those books have all those you need. Trust me

Super Contributor
Posts: 383

## Re: Create variable in do loop in data step?

wow, what's the title for book by Art Carpenter? I googled, one popped up for macro
Super User
Posts: 2,061

## Re: Create variable in do loop in data step?

[ Edited ]

by @ArtC.....This book will make anybody a master

Super Contributor
Posts: 383

## Re: Create variable in do loop in data step?

wow, i shall own this piece. Thanks a lot!
Super Contributor
Posts: 383

Super User
Posts: 2,061

## Re: Create variable in do loop in data step?

Ok, Follow this idea if you wish, coz that worked for me.

1. SAS documentation is great for quick reference like a user manual

2. Ron cody is the best book to start. I regard him guru of all beginners

2. Art carpenter is great when you have done with ron cody's

Super User
Posts: 24,003

## Re: Create variable in do loop in data step?

A different way, this doesn't require you to know that you're going up to 20 ahead of time it just increments it until the end.

No RETAIN statement is required because of the implicit RETAIN.

``````data want;
set temp;
risk + 0.1;
run;``````

Super Contributor
Posts: 383

wow!
Super User
Posts: 8,277

## Re: Create variable in do loop in data step?

Cruise wrote:
exactly, why do statement comes in between data and set? how to master 'do' statement systematically? from the beginning

Like for most real problems there is no one "right" answer, but you do need to think about what the code you have written is doing.

So for your original program you tried this:

``````data temp1;
set temp;
do risk = 0.1 to 2 by 0.1;
end;
run;``````

You really don't need to know SAS to understand why that won't work.  Your DO loop has nothing in it so all it is doing is setting a constant value to the RISK variable.  (you do need to understand SAS to know that it set RISK to 2.1, plus any minor differences cause by adding 21 floating point representations of 0.1 together)

So if you want each input observation to be repeated 20 times for each value of RISK then you just need to add an OUTPUT statement in the loop.

If instead you want to values for risk on the first observation to start at 0.1 and increase by 1 for each observation after that then you can retain the value of RISK and increment it. The SUM statement is an easy way to do that.  Usually you will see it used to add a counter variable, but you can increment by anything.

``````data temp1;
set temp;
risk + 0.1;
run;``````

If you put the SET statement inside of a DO loop then it will execute multiple times for each iteration of the DATA step.  But this also means that the implied OUTPUT that is done at the end of a normal DATA step would only happen after the DO loop ends.  So if you do not add an OUTPUT statement in the loop then only every 20th observation would be written.

Now if you just want the RISK value to increment by 0.1 you do not need to put an upper bound on the DO loop.  The data step (like most data steps) will stop when it reads past the end of the input dataset.

``````data temp1;
do risk = 0.1 by 0.1;
set temp;
output;
end;
run;``````

☑ This topic is solved.