DATA Step, Macro, Functions and more

Need to extract data which has "*" as value

Accepted Solution Solved
Reply
Regular Contributor
Posts: 214
Accepted Solution

Need to extract data which has "*" as value

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?

 


Accepted Solutions
Solution
‎09-21-2017 07:18 AM
Super User
Posts: 6,935

Re: Need to extract data which has "*" as value

where index(var, 'H *') or index(var, 'L *');

 

The initial focus on the * led in the direction of using INDEXC.  Switch to INDEX.

View solution in original post


All Replies
Frequent Contributor
Posts: 118

Re: Need to extract data which has "*" as value

Try this....

data WANT;
  set HAVE;
  where indexc(var_char,"*");
run;

 

Hope this helps.

Regular Contributor
Posts: 214

Re: Need to extract data which has "*" as value

Posted in reply to ShiroAmada

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*"?

Frequent Contributor
Posts: 118

Re: Need to extract data which has "*" as value

[ Edited ]

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.

Regular Contributor
Posts: 214

Re: Need to extract data which has "*" as value

Posted in reply to ShiroAmada

I need to have values only which has either "L*" or "H*" values and the rest be deleted.

Frequent Contributor
Posts: 118

Re: Need to extract data which has "*" as value

data want;
  set have;
  where indexc(var,"*");
  chars=catx(" ",scan(var,-2" "),scan(var,-1," "));
run;
Regular Contributor
Posts: 214

Re: Need to extract data which has "*" as value

Posted in reply to ShiroAmada

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 *

 

Frequent Contributor
Posts: 118

Re: Need to extract data which has "*" as value

[ Edited ]

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.

Regular Contributor
Posts: 214

Re: Need to extract data which has "*" as value

Posted in reply to ShiroAmada

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 *

Respected Advisor
Posts: 4,802

Re: Need to extract data which has "*" as value

[ Edited ]

@vraj1

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

Solution
‎09-21-2017 07:18 AM
Super User
Posts: 6,935

Re: Need to extract data which has "*" as value

where index(var, 'H *') or index(var, 'L *');

 

The initial focus on the * led in the direction of using INDEXC.  Switch to INDEX.

☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 163 views
  • 1 like
  • 4 in conversation