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.
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.
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.
The variable is a string in which the numbers are surrounded by quotes.
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 */
Thanks. This code works and is very helpful.
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.);
Never mind, I answered my own question.
@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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.