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

I have data that contains a range of numbers with a variable called flag for each range. It looks like this:

 

range flag
'10040-10040' 0
'10060-10061' 1

 

I'd like to expand this to a dataset that looks like this. 

number flag
10040 0
10060 1
10061 1

 

It's easy to define the beginning and end of the range but not sure exactly how to write the right do loop for this. I'd appreciate any advice.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

First question, do your "range" values actually have quote marks as a SAS data value?

 

If the values actually contain the quote character:

data want;
   set have;
   if flag then do number = input(scan(range,1,"'-"),8.) to input(scan(range,2,"'-"),8.);
        output;
   end;
   else do;
      number=input (scan(range,1,"'-"),8.);
      output;
   end;
run;

If not

data want;
   set have;
   if flag then do number = input(scan(range,1,"-"),8.) to input(scan(range,2,"-"),8.);
        output;
   end;
   else do;
      number=input (scan(range,1,"-"),8.);
      output;
   end;
run;

Hard to see but there is a single quote inside the double quotes in the Scan function in the first version.

 

View solution in original post

8 REPLIES 8
ballardw
Super User

First question, do your "range" values actually have quote marks as a SAS data value?

 

If the values actually contain the quote character:

data want;
   set have;
   if flag then do number = input(scan(range,1,"'-"),8.) to input(scan(range,2,"'-"),8.);
        output;
   end;
   else do;
      number=input (scan(range,1,"'-"),8.);
      output;
   end;
run;

If not

data want;
   set have;
   if flag then do number = input(scan(range,1,"-"),8.) to input(scan(range,2,"-"),8.);
        output;
   end;
   else do;
      number=input (scan(range,1,"-"),8.);
      output;
   end;
run;

Hard to see but there is a single quote inside the double quotes in the Scan function in the first version.

 

chuakp
Obsidian | Level 7

The variable is a string in which the numbers are surrounded by quotes.  

sbxkoenk
SAS Super FREQ
data have;
LENGTH range $ 20;
input range	$ flag;
range_wo_quotes=compress(range,"'");
start_range=input(scan(range_wo_quotes,1,'-'),12.);
stop_range =input(scan(range_wo_quotes,2,'-'),12.);
cards;
'10040-10040'	0
'10060-10061'	1
;
run;
data want;
 set have;
  do number=start_range to stop_range;
  output;
 end;
run;
/* end of program */
chuakp
Obsidian | Level 7

Thanks. This code works and is very helpful.

Reeza
Super User
This is pretty close to this question here:
https://communities.sas.com/t5/SAS-Programming/I-need-to-turn-a-range-of-values-i-e-A0259-B3548-into...

The answer will also be very similar.
chuakp
Obsidian | Level 7

Thanks - this works, but could you help me understand the logic of this code - what does it mean for "if flag" to be true versus false?

 

if flag then do number = input(scan(range,1,"'-"),8.) to input(scan(range,2,"'-"),8.);
chuakp
Obsidian | Level 7

Never mind, I answered my own question.

ballardw
Super User

@chuakp wrote:

Never mind, I answered my own question.


You might play around with SAS numeric values and the results of logic functions.

In general SAS resolves any logic result to 1 for true and 0 for false:    If x=3 then  <do something>, for instance becomes If 1 then when x is indeed 3 or 0 otherwise. So when you have a 1/0 coded variable then IF Variable behaves that way as well.

 

Other numeric values will be treated as true. Exercise left to the interested reader.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 8 replies
  • 1832 views
  • 0 likes
  • 4 in conversation