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

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

10 REPLIES 10
ShiroAmada
Lapis Lazuli | Level 10

Try this....

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

 

Hope this helps.

vraj1
Quartz | Level 8

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

ShiroAmada
Lapis Lazuli | Level 10

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.

vraj1
Quartz | Level 8

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

ShiroAmada
Lapis Lazuli | Level 10
data want;
  set have;
  where indexc(var,"*");
  chars=catx(" ",scan(var,-2" "),scan(var,-1," "));
run;
vraj1
Quartz | Level 8

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 *

 

ShiroAmada
Lapis Lazuli | Level 10

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.

vraj1
Quartz | Level 8

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 *

Patrick
Opal | Level 21

@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

Astounding
PROC Star

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

 

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

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
  • 10 replies
  • 2091 views
  • 2 likes
  • 4 in conversation