BookmarkSubscribeRSS Feed
weweaw
Obsidian | Level 7

I know there has to be a super simple way to do this.

I need to create an indicator variable for a large range of contentious and non continuous numbers.  In my past I've only had a max of 20 numbers so I just wrote each one in.  Lets say for example my numbers are 142-244, 333, and 401-420.  How would I do this?  I ultimately want to create an indicator variable to say if the previous numbers are there put 1 and if any other number put 0. I've been trying to figure this out for a day and by now i could have typed out the numbers....

Side note, they were originally assigned at characters and I changed it to numbers to try to figure this out.

Thank you for your help,

Jen

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

You want do loops:

data want;

     do I=142 to 244;

          output;

     end;

     do I=333,334,335;

          output;

     end;

run;

weweaw
Obsidian | Level 7

For my number range, I ultimately need to exclude those numbers for certain tables.  Also there is not always numbers in between the numbers I want.  Like 200 and 205 is missing but I need 142-245.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Perhaps post some test data of what you have and what you want.  If it is as I think then;

data want;

     do I=142 to 244;

          if thenumber ne I then do;

               /* do your code */

          end;

     end;

run;

Astounding
PROC Star

If I have the correct picture, you have a variable that takes on numeric values and you need to examine this variable to determine whether the current observation should be included or excluded.  Does that sound right?

If that's the case, a format is probably the best bet.  You can hard-code a format, such as:

proc format;

  value inex  142-144, 333, 401-420 = 'Include' other='Exclude';

run;

Then in a procedure you can use the format, such as:

where put(some_variable, inex.)='Include';

Formats let you handle trickier ranges, such as excluding 200 and 205 specifically.  And if your list of values is long but is contained in a data set, formats let you use the data set to construct the format instead of having to type out all the values.  Formats can be permanently saved, so they don't have to re-created each time.

So the first question is still, is this the actual problem you are trying to solve?

RaviKommuri
Fluorite | Level 6

From the subject and your explanation I just understand that

     You have a list of continuos and non-continuos number from which you want to set some ranges of continuous values.

Final desired output is just starting range and ending range... this is what i understand, correct me if i am wrong.

And here is my code.....

data have;                                                         

Input mynum;                                                       

Datalines ;                                                        

142                                                                

143                                                                

144                                                                

145                                                                

333                                                                

401                                                                

402                                                                

403                                                                

404                                                                

;                                                                  

                                                                   

data want;                                                         

set have;                                                         

init = mynum;                                                     

if _N_ =1 then start = mynum;                                     

if mynum - prevnum ne 1 then start = init;                        

prevnum = mynum;                                                  

retain start init prevnum;                                        

                                                                   

proc sort data=want; by start; run;                                

data want(rename=(start=rangest prevnum=rangelt) drop=mynum init);

set want;                                                        

by start;                                                        

if last.start;                                                   

Output:

Obs    rangest    rangelt

                         

1       142        145  

2       333        333  

3       401        404 

If this is not your desired output, Give us some example data and your desired output!!!!

Reeza
Super User

Use the colon operator to specify a range of values and then comma's to separate the different ranges and values.

data subset;

    set sashelp.class;

    if age in (12:14, 16);

run;

if number in (142:244, 333, 401:420) then flag=1; else flag=0;

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 7615 views
  • 3 likes
  • 5 in conversation