Selection of variables using x1-x5, but with strings

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Selection of variables using x1-x5, but with strings

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;

 


Accepted Solutions
Solution
‎12-01-2017 06:38 AM
Super User
Super User
Posts: 8,634

Re: Selection of variables using x1-x5, but with strings

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


All Replies
Solution
‎12-01-2017 06:38 AM
Super User
Super User
Posts: 8,634

Re: Selection of variables using x1-x5, but with strings

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?

Contributor
Posts: 24

Re: Selection of variables using x1-x5, but with strings

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.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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