Help using Base SAS procedures

Help distinguishing codes with leading zeroes

Reply
New Contributor
Posts: 2

Help distinguishing codes with leading zeroes

Hi All,

 

I am trying to figure out how to identify observations with a variable code of 391; however, some observations have a code of 0391, which has a different meaning in this database. This is probably a silly question, but I am new to SAS so any help would be appreciated!

 

 

Thanks so much!

SAS Super FREQ
Posts: 9,367

Re: Help distinguishing codes with leading zeroes

[ Edited ]
Posted in reply to jchiefs1101

Hi:
Can you do a PROC CONTENTS on the data set and tell whether your variable of interest is character or numeric? If numeric, then these 2 WHERE statements would work the same:
WHERE numvar = 0391;
or
WHERE numvar = 391;

On the other hand, if you have a CHARACTER variable, then there is a difference between the string '391' and the string '0391'. Here's a program you can run to illustrate that point.

data testdata;
  infile datalines dlm=',';
  input numvar charvar $;
datalines;
0,0
0391,0391
391,391
0011,0011
2220,2220
;
run;
  
ods select variables;
proc contents data=testdata;
  title '1) How are variables defined';
run;
 
proc print data=testdata;
  title '2) where with NUMVAR';
  where numvar = 0391;
run;
 
proc print data=testdata;
  title '3) alternative with NUMVAR';
  where numvar = 391;
run;
 
proc print data=testdata;
  title '4) check CHARVAR';
  where charvar = '0391';
run;
 
proc print data=testdata;
  title '5) alternative with CHARVAR';
  where charvar = '391';
run;

Note that in the TESTDATA data set, the same value is read as character and as numeric and then used with a WHERE in PROC PRINT. Reviewing the output shows the difference in numeric and character variables.

cynthia

New Contributor
Posts: 2

Re: Help distinguishing codes with leading zeroes

Posted in reply to Cynthia_sas
It is numeric; however, could i simply do a string if-then statement specifying = '391' ? Would that work?

Thanks so much!

SAS Super FREQ
Posts: 9,367

Re: Help distinguishing codes with leading zeroes

Posted in reply to jchiefs1101
Hi:
Run my program and look carefully at the rows selected by print #2 and print #3. In some instances, SAS will do an automatic conversion if you do this:
IF numvar = '0391' -- but the automatic conversion on the string '0391' is not going to be what you are hoping for. An automatic conversion would cause both the value 391 and 0391 to be treated the same.

Did you run a PROC CONTENTS to verify that the variable TYPE is numeric -- not the value of the variable, but the TYPE in the data set.
cynthia
Super User
Super User
Posts: 8,089

Re: Help distinguishing codes with leading zeroes

Posted in reply to jchiefs1101

jchiefs1101 wrote:
It is numeric; however, could i simply do a string if-then statement specifying = '391' ? Would that work?

Thanks so much!


There are no "leading zeros" on a number. The number 391 is the same as 0391 and 00391.  If you need to distinguish between 0391 and 391 then you need read the original data as a character string.

Super User
Posts: 3,918

Re: Help distinguishing codes with leading zeroes

[ Edited ]
Posted in reply to jchiefs1101

If your code is a character column then - if code = '391' - will only pick up rows where code is 391 and not 0391.

Super User
Posts: 6,762

Re: Help distinguishing codes with leading zeroes

Posted in reply to jchiefs1101

If you need to distinguish 0391 from 391, you must create the variable as character when you extract it from the database.  If  your variable is numeric, all of these find the same observations:

 

where var = 391;

where var = 0391;

where var = 00391;

where var = 389+2;

Ask a Question
Discussion stats
  • 6 replies
  • 217 views
  • 4 likes
  • 5 in conversation