Character variable is a period, how to delete?

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

Character variable is a period, how to delete?

Hi,

I have a variable, HCPCS, that is usually numeric, but sometimes is alphanumeric (e.g. G0202). I am merging several wide datasets together, so most of them contain only numeric values for HCPCS but once I merge a file that has an alpha code, the HCPCS variable becomes a SAS-defined character variable. The problem is, when I make the data long, I would like to delete all the rows that have missing values for HCPCS, but since it's a character variable, I'm having trouble. I can delete the ones that are blank (if HCPCS=" " then delete) but when I try to delete the ones that are periods (.), they don't get deleted. I've tried if HCPCS="." then delete (which doesn't delete anything) and if HCPCS=. then delete (which converts HCPCS to a numeric variable and deletes all the codes with letters in them, which I don't want to lose).

ANy help would be greatly appreciated! Thanks.

Laurie


Accepted Solutions
Solution
‎06-18-2013 04:32 PM
Super User
Posts: 5,256

Re: Character variable is a period, how to delete?

Sounds like that you don't really have the control whther the variable is numeric or not. It should be possible to have that, or how does your merge operation look like?

If you are sure that HCPCS is char, try left(HCPCS) = '.', or just compress(HCPCS,'.') = ' '.

Data never sleeps

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: Character variable is a period, how to delete?

try:

if missing(HCPCS) then delete;

Contributor
Posts: 25

Re: Character variable is a period, how to delete?

Sorry, I should have clarified, I tried that too. That only deleted the ones that were " " type missing. For all the ones that were . missing, they were still there. Thanks though.

Solution
‎06-18-2013 04:32 PM
Super User
Posts: 5,256

Re: Character variable is a period, how to delete?

Sounds like that you don't really have the control whther the variable is numeric or not. It should be possible to have that, or how does your merge operation look like?

If you are sure that HCPCS is char, try left(HCPCS) = '.', or just compress(HCPCS,'.') = ' '.

Data never sleeps
Contributor
Posts: 25

Re: Character variable is a period, how to delete?

This code worked:

if left(HCPCS)='.' then delete;

THANK YOU!

Super User
Super User
Posts: 6,499

Re: Character variable is a period, how to delete?

When SAS does an automatic conversion from numeric to character you will see those types of right aligned values.  The worst is when the variable is longer than you screen size so when you browse the data it looks all blank.  Try this little example to see what is happening.

data check;

  length char $10 ;

  input x;

  char=x;

  put char= $quote. x=;

cards;

1

.

3

run;

You can use the CATS() function to remove the leading blanks.  The nice thing is that it will work without errors or warnings with both character and numeric variables.

data test;

  set check;

  if cats(char) in (' ','.') then delete;

  put char= $quote. x=;

run;


data test;

  set check;

  if cats(x) in (' ','.') then delete;

  put char= $quote. x=;

run;

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 1690 views
  • 0 likes
  • 4 in conversation