I have a variable which has caharacter values and i want to extract values which ends with *.
var
5.6 H *
4.3
2.6
9.6 L *
How can i do that?
where index(var, 'H *') or index(var, 'L *');
The initial focus on the * led in the direction of using INDEXC. Switch to INDEX.
Try this....
data WANT;
set HAVE;
where indexc(var_char,"*");
run;
Hope this helps.
Thanks a lot can i include multiple conditions in it like
where indexc(var_char,"H*" "L*");
so that i can extract values which has "H*" and "L*"?
What's next after you have flagged records with "*"?
INDEXC - i think this is limited to just a character you can google it.
Try this....
data want;
set have;
where indexc(var,"*");
chars=catx(" ",scan(var,-2" "),scan(var,-1," "));
run;
Hope this helps.
I need to have values only which has either "L*" or "H*" values and the rest be deleted.
data want;
set have;
where indexc(var,"*");
chars=catx(" ",scan(var,-2" "),scan(var,-1," "));
run;
It doesnt work unfortunately
var
8.3
22 *
6.7 H *
120 H *
34 L *
I need the below
var
6.7 H *
120 H *
34 L *
there is space between number and H or L and *
Here is a better version.
Try this....
data WANT;
set HAVE;
where indexc(var,"*");
char_used=ifc(find(var,"H *",1),"H *","L *");
run;
Since when a record has an "*" the values are either "H *" or "L *".
If you just want those records containing "H *" or "L *" then remove the assignment statement for char_used.
Hope this helps.
Thanks, but this just gives H * or L *
I need the entire values like if it is 345 H * i need the entire 345 H *
Something like below should do.
data have;
infile datalines truncover;
input var $20.;
datalines;
8.3
22 *
6.7 H *
120 H *
34 L *
;
run;
/* option 1 */
data want1;
set have;
if prxmatch('/(h|l)\s*\*$/oi',strip(var));
run;
/* option 2 */
data want2(drop=_:);
set have;
_var=compress(var);
if upcase(substrn(_var,lengthn(_var)-1)) in ('L*','H*');
run;
/* option 3 */
data want3;
set have;
if strip(scan(var,-1,'lh','i')) ='*';
run;
Preferred option: Option 3
where index(var, 'H *') or index(var, 'L *');
The initial focus on the * led in the direction of using INDEXC. Switch to INDEX.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.