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

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

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

11 REPLIES 11
novinosrin
Tourmaline | Level 20

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;
Cruise
Ammonite | Level 13
exactly, why do statement comes in between data and set? how to master 'do' statement systematically? from the beginning
novinosrin
Tourmaline | Level 20

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

Cruise
Ammonite | Level 13
wow, what's the title for book by Art Carpenter? I googled, one popped up for macro
novinosrin
Tourmaline | Level 20

Cover art

 

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

Cruise
Ammonite | Level 13
wow, i shall own this piece. Thanks a lot!
Cruise
Ammonite | Level 13
Last question please. Which one would you recommend me to start with?
novinosrin
Tourmaline | Level 20

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

Reeza
Super User

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;

 

Cruise
Ammonite | Level 13
wow!
Tom
Super User Tom
Super User

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

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 11 replies
  • 4809 views
  • 10 likes
  • 4 in conversation