01-15-2018 02:31 PM
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!
01-15-2018 02:46 PM - edited 01-15-2018 02:48 PM
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;
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.
01-15-2018 02:55 PM
01-15-2018 03:26 PM
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.
01-15-2018 02:47 PM - edited 01-15-2018 02:47 PM
If your code is a character column then - if code = '391' - will only pick up rows where code is 391 and not 0391.
01-15-2018 02:58 PM
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;