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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 723 views
  • 0 likes
  • 2 in conversation