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!
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
@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.
If your code is a character column then - if code = '391' - will only pick up rows where code is 391 and not 0391.
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.