DATA Step, Macro, Functions and more

Getting error when reformatting character data type to numeric data type if a value is not entered.

Reply
Frequent Contributor
Posts: 82

Getting error when reformatting character data type to numeric data type if a value is not entered.

Hello,

I have several prompts, 'FGR', 'DATA1' & DATA2'.  This data is character so I defined the prompt in SMC as character type.  However, I need to compare to a numeric value so in my code I am using 'call symput' to convert to a numeric value.

 

What I am finding in testing is that if I have a value in the prompt, the code works.  However, the user may only enter one value and leave the other 2 blank.  In my example I have entered a value into 'FGR' but left 'DATA1' & 'DATA2' blank.

 

Any suggestions would be greatly appreciated.

80            Call symput(('NEW_FGR),(input(&FGR,16.)));
82            Call symput(('NEW_DATA1'),(input(&DATA1,16.)));
                                                     _
                                                     22
83                Call symput(('NEW_DATA2'),(input(&DATA,16.)));
                                                        _
                                                        22
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, 
              a missing value, INPUT, PUT.  
              a missing value, INPUT, PUT.
Super User
Posts: 17,962

Re: Getting error when reformatting character data type to numeric data type if a value is not enter

CALL SYMPUT creates a macro variable that will always be text.

Please post an example of how resolving the macro variable is causing issues and we can make suggestions on how to resolve it.

Frequent Contributor
Posts: 82

Re: Getting error when reformatting character data type to numeric data type if a value is not enter

Not sure I understand what you want me to post.  I posted the source code that I am using to convert the character to numeric value.  Since you mentioned that 'call symput' returns a text value then I do not need to use it, I want numeric returned.

 

I tried another way to assign the converted character to numeric value to a variable by doing this:

%let NEW_FGR = input(&FGR,16.); 

 

I used a put statement to display 'NEW_FGR' and this is what was returned:

116         %put &NEW_FGR_FROM_AMT;

input(3903918260,16.)

 

It appears that it did not apply the 'input' to convert from char to numeric but rather used the whole string as the new value for 'NEW_FGR'.   This seems like it be easy to do just to convert to numeric and assign to a variable but I just can't seem to get it right!  I have spent several hours on this!!!

 

 

 

Super User
Posts: 17,962

Re: Getting error when reformatting character data type to numeric data type if a value is not enter

Where are you trying to use those macro variables?

Creating new macro variables will not change the type, it changes the appearance. 

All macro variables are text - how you use them is what matters.

 

 

 

 

Super User
Posts: 5,099

Re: Getting error when reformatting character data type to numeric data type if a value is not enter

Reeza has hit on the right issue here.  Your macro variables are fine.  It's how you use them later that matters.  To give you an example:

 

%let year = 2016;

 

&YEAR is always the set of characters:  2016

SAS will see how you use &YEAR, and make a determination as to whether they are text of a number.  For example:

 

data fy&year;

 

SAS decides that 2016 is text, and "fy2016" is the name of the data set to create.

 

next_year = &year + 1;

 

SAS decides that 2016 is numeric, ans uses it to perform math.  

 

In both cases, SAS is deciding whether to treat the characters as text or as numeric.  Macro langauge is not deciding.

Contributor
Posts: 39

Re: Getting error when reformatting character data type to numeric data type if a value is not enter

You could try using %SYSFUNC and inputn/putn.

 

See below:

 

%let FGR = 3903918260;
%let NEW_FGR = %sysfunc(putn(&FGR,comma16.));
%put NEW_FGR is: *** &NEW_FGR ***;

 

Output the following to the Log:

 

NEW_FGR is: *** 3,903,918,260 ***

 

 

Ask a Question
Discussion stats
  • 5 replies
  • 308 views
  • 2 likes
  • 4 in conversation