BookmarkSubscribeRSS Feed
jchiefs1101
Calcite | Level 5

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!

6 REPLIES 6
Cynthia_sas
SAS Super FREQ

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
Calcite | Level 5
It is numeric; however, could i simply do a string if-then statement specifying = '391' ? Would that work?

Thanks so much!

Cynthia_sas
SAS Super FREQ
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
Tom
Super User Tom
Super User

@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.

SASKiwi
PROC Star

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

Astounding
PROC Star

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;

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!

What is Bayesian Analysis?

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.

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
  • 6 replies
  • 838 views
  • 4 likes
  • 5 in conversation