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

hi! Ive ran into a problem where i need to create a range of values, contained in strings. The data given is on the format

X-H-237   to X-H-240  (in reality its a lot more values, but for simplicity lets settle with 237-240).

Is there a function or similar that can handle this problem?

 

Below is a script showing what i want to do.

 

data Matrix(drop= i);
array A(*) A1-A7;
do i=1 to 7;
A(i)=rand('Uniform');
end;
format B $10.;

input B $;
datalines;
X-H-237
X-H-238
X-H-239
X-H-240
;
run;
data test;
/*State 1-5 Rk-states, 6=Delinquent 7=default */
set Matrix;
if B in "X-H-237"--"X-H-240" then put A1;
if B in "X-H-237"-"X-H-240" then put A2;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

There really is no need to code all in mixed case, its very hard to read.  Now, you can simply change you code to number and use that, thus:

data matrix(drop= i);
  array a(*) a1-a7;
  do i=1 to 7;
    a(i)=rand('uniform');
  end;
  format b $10.;
  input b $;
datalines;
X-H-237
X-H-238
X-H-239
X-H-240
;
run;

data test;
  set matrix;
  if 237 <= input(scan(b,3,"-"),best.) <= 240 then put a1;
run;

(you will note the use of the code window to preserve indenting).  With that you can do numeric ranges, although I do not know what the second datastep is meant to acheive?

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

There really is no need to code all in mixed case, its very hard to read.  Now, you can simply change you code to number and use that, thus:

data matrix(drop= i);
  array a(*) a1-a7;
  do i=1 to 7;
    a(i)=rand('uniform');
  end;
  format b $10.;
  input b $;
datalines;
X-H-237
X-H-238
X-H-239
X-H-240
;
run;

data test;
  set matrix;
  if 237 <= input(scan(b,3,"-"),best.) <= 240 then put a1;
run;

(you will note the use of the code window to preserve indenting).  With that you can do numeric ranges, although I do not know what the second datastep is meant to acheive?

Shawnty
Obsidian | Level 7
Hi RW9, Exactly what i was looking for. Thank you!

Second datastep was just created to illustrate the if statement. What happends in it was irrelevant for the question so i left it out.

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
  • 2 replies
  • 1228 views
  • 0 likes
  • 2 in conversation