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.

sas-innovate-2024.png

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.

 

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