BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
meemo
Calcite | Level 5

Hi everyone, im planning to take the SAS base cert exam and have signed up for a 3rd party websote that provides a bank of questions to run practice tests on. 

I find that many questions are riddled with typos which make identifying syntax errors a little tricky. It worries me that some answers they provide may be incorrect too. Theres a question here I cant seem to figure out why I got wrong (I answered D and the correct answer is C), any opinions would be helpful!

 

 

Question:

The following program is submitted.

data WORK.TEST;
input Name $ Age;
datalines;
John +35;
run;

Which values are stored in the output data set?

Response:

 

Name              Age
---------------------
(missing value)   (missing value)

 

The DATA step fails execution due to data errors.

Should have chosen

Name              Age
---------------------
John               35

Wrong

Name              Age
---------------------
John              (missing value)

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

SAS understands plus and minus signs as valid numeric characters.  However, there can't be embedded blanks.  So "+35" can be read as numeric, but "+ 35" cannot.

View solution in original post

14 REPLIES 14
Astounding
PROC Star

SAS understands plus and minus signs as valid numeric characters.  However, there can't be embedded blanks.  So "+35" can be read as numeric, but "+ 35" cannot.

meemo
Calcite | Level 5

Hi Astounding, thanks for your reply!

in Programming 1 (Lesson 3) it says a SAS numeric variable can only contain numbers, a single decimal point, a minus sign and E for scientific notation. wouldnt this mean a + sign wont be recognised in a numeric variable? 

 

(i have no programming experience and have never worked in IT, just trying to wrap my brain around this!)

Astounding
PROC Star

I can't confirm what is written in the lesson materials.  But + is legal.  Try it for yourself:

 

data test;

input numval;

cards;

+35

;

meemo
Calcite | Level 5

Are there any other symbols that you know of (apart from the ones above) that are allowed in a numeric variable?

Tom
Super User Tom
Super User

I believe that it will also allow D instead of E for scientific notation. 

Tom
Super User Tom
Super User

Depending on values listed in the MISSING statement then it will also accept single letters or underscore as meaning the corresponding special missing value. A single period is interpreted as regular missing value.

missing a b c d ;
data test;
  input x;
cards;
123
+123
-123
123E10
123D10
123.5
A
B
c
d
.
;
ballardw
Super User

@meemo wrote:

Are there any other symbols that you know of (apart from the ones above) that are allowed in a numeric variable?


Depends on what you mean by "allowed in a numeric variable" and what use.

SAS will allow you to create custom informats to read any specific text value(s) into numeric values.

 

proc format library=work;
invalue oddvals
'NA' = -1
'PDQ' = 27
;
run;

data work.example;
  informat x oddvals.;
  input x;
datalines;
1
NA
123456
PDQ
;
run;

And formats can display a numeric value as practically any text you want:

 

proc format library=work;
value oddvals
-1 = "Closed for repair"
27 = "Poppa done good"
;
run;
proc print data=work.example;
   format x oddvals.;
run;

If a format like oddvals is permanently associated with a variable then anytime the format is active in the current SAS session the values will display as shown.

 

And you can override or change formats on the fly:

 

Proc freq data=work.example;
   tables x;
   format x best3.;
run;

Which show the larger value of the variable with only 3 columns so shifts to scientific notation.

 

FreelanceReinh
Jade | Level 19

Also, please note that "what a numeric variable can contain" and "what can be read into a numeric variable using an informat (or more specifically: the default w. informat)" are two different questions.

 

In particular, I strongly disagree with the wording that a SAS numeric variable can "contain ... E for scientific notation". That letter "E" can never be found in the actual content of a numeric variable (which is a certain binary representation). It can only be part of a formatted value of this variable (which is a character string).

meemo
Calcite | Level 5

I understand what you are saying re what variables can contain and what can be read as a num variable.  this entire string is about the question i posted in the beginning, where +35 was recognised as a numeric variable. In asking what is allowed in numeric variables I was referring to what SAS is able to read as valid numeric characters, without needing an informat. "Numeric variables can contain a minus sign etc......" was quoted word for word from the SAS e-learning site. 

 

If you have an issue with the semantics I suggest you bring it up with the people involved with writing the SAS curriculum. You'd be able to explain it better than I would, and they could probably use the input! I'm guessing the elearning is designed for people with some basic IT knowledge. I'm a pharmacist learning to use SAS for application in clinical trials and not knowing jargon/ basic principles of how things work has made it a little difficult to try to communicate with professionals such as yourself

hashman
Ammonite | Level 13

@meemo>In asking what is allowed in numeric variables I was referring to what SAS is able to read as valid numeric characters, without needing an informat.<

 

It's more accurate to say "when I don't specify an informat explicitly". This is because SAS always uses an informat to turn any data outside the SAS System into SAS data, even if no informat is explicitly specified. This is the case with your specific INPUT statement where $ is a directive to use the $8. informat and the informat omitted after the second field is assumed to be BEST32. It's of paramount importance to understand these things since otherwise you one can't be sure what kind of data enters the system. My personal rule of defensive programming in this regard is to always specify informats explicitly, which can be done in a variety of ways as far as the INPUT statement is concerned. This way, one knows the rules of data conversion as long as one knows the rules of the corresponding informats.   

 

Good luck with using SAS

Paul D.

Tom
Super User Tom
Super User

Not that I mean to nit-pick with the master, but I think that SAS actually processes numeric strings in program code differently than the way that the normal numeric informat converts string data.  There was a question about this in the past where someone asked why they got a slightly different floating number for a constant in a program that they did when reading similar number with an input statement.

 

Also there isn't really a BEST informat, like there is a BEST format.  SAS will treat BESTw.d as an alias for the normal w.d informat, just like it also allows the use of Fw.d as an alias.

image.png

 

hashman
Ammonite | Level 13

@Tom:

 

There's nothing I can disagree with. Some constants (if that's what you mean) may be processed somewhat differently, yet they're still processed using informats, sometimes with twists like in the case of the character and hex literals. You're right that the BESTw. informat isn't listed in the docs but since SAS recognizes it as an alias and doesn't vomit on the shoes of someone who uses it, I mark it as "existing". Heck, there're no implicit arrays or proc DELETE in the docs, either, yet they work just fine.

 

Best

Paul D. 

FreelanceReinh
Jade | Level 19

Thanks for providing more context.


@meemo wrote:

"Numeric variables can contain a minus sign etc......" was quoted word for word from the SAS e-learning site. 


That's unfortunate, but it happens in other fields (math, science, ...) as well: Introductory presentations are sometimes oversimplified to the point of being inaccurate or misleading. This may help some learners while confusing others.

nohassles
Obsidian | Level 7

Was the semicolon following the +35 a typo? It results in a syntax error making work.test a dataset with 0 observations making b the best answer.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 14 replies
  • 3841 views
  • 5 likes
  • 7 in conversation