DATA Step, Macro, Functions and more

Need Help with Proc format application.

Reply
Highlighted
Occasional Contributor ssv
Occasional Contributor
Posts: 5

Need Help with Proc format application.

[ Edited ]

Hi there!

I am facing a problem in Proc format. I was trying to convert a character variable to numeric variable by using the input function and at the same time i am applying a user defined character format to that character variable. but it is showing a warning in the log window.

 

WARNING: The data set WORK.BB may be incomplete. When this step was stopped there were 0 observations and 2 variables.
WARNING: Data set WORK.BB was not replaced because this step was stopped. 

 

The program I submitted is as follows:

 

proc format;
           value $namefmt
           'abc' = 1
          'bbc' = 2
          'ccc' = 3;
run;


data old;
       input name $;
       cards;
       abc
       bbc
       ccc
         ;
run;

 

data new;
       set old;
       newname=input(name,namefmt.);
run;

 

But if i use Put function in place of input function it is working fine with out any warning. But Put function usually converts Numeric to Character. But why is it working for a character variable? 

 

data bb;
       set ab;
       newname=put(name,namefmt.);
run;

 

If i use this program then it is working fine again.

 

data bb;
       set ab;
       newname=input(put(name,namefmt.),best.);
run;

 

Can some one please Clarify this?

Thanks in Advance.

 

Occasional Contributor VDD
Occasional Contributor
Posts: 12

Re: Need Help with Proc format application.

try this for the new dataset

 

data new;

set old;

 

newname=name;

format newname $namefmt.;

run;

 

 

Occasional Contributor VDD
Occasional Contributor
Posts: 12

Re: Need Help with Proc format application.

how about apply the $ before the format name.

 

data bb;

set old;

newname=put(name,$namefmt.);

run;

Occasional Contributor ssv
Occasional Contributor
Posts: 5

Re: Need Help with Proc format application.

Hello VDD,

Thank you for your reply. 

Actually it is working with out any errors with or with out $ sign. 

--------------------------------------------------------------------------------

data bb;

set old;

newname=put(name,namefmt.);

run;

data bb;

-------------------------------------------------

set old;

newname=put(name,$namefmt.);

run;

 

 

Both of these result in the same output.

But my question was when we are converting a character variable to Numeric we need to use input function. and in this program i am trying to convert a character variable to numeric , (in addition i need to apply a user defined format) But if i use INPUT function it throws an error. but if i use PUT function it is fine(with or without $ sign). So why we are unable to convert character variable to numeric by applying a format using INPUT variable?

Super User
Posts: 8,359

Re: Need Help with Proc format application.

if you want to use a format in an input statement, you have to create an informat:

proc format;
invalue $namefmt
  'abc' = 1
  'bbc' = 2
  'ccc' = 3
; run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor ssv
Occasional Contributor
Posts: 5

Re: Need Help with Proc format application.

Posted in reply to KurtBremser

Hello KurtBremser

 

proc format;
invalue $namefmt
  'abc' = 1
  'bbc' = 2
  'ccc' = 3;
run;

 

as you advised i have created an informat  and applied in this way.

 

data new1;
           set old;
           newname=input(name,$namefmt.);
run;

 

Yes ! now i get the output. A new variable Newname(Character variable) is created and it contains the data 1 2 and 3. but my question is Why is it a character variable? when i use a input function it should convert to numeric but why is it not converting to numeric? 

 

The same is happening with the Put function. When I use a put function it is applying the format. In general PUT function converts numeric values to character. But here if i use a  put function for a character variable it is working fine without any errors.The resultant new variable is a character again.

 

data bb;
         set old;
         newname=put(name,$namefmt.);
run;

 

Both of these programs are producing the same output. 

Super User
Posts: 8,359

Re: Need Help with Proc format application.

If you want to have a numeric value, you have to create a numeric informat, so omit the $'s:

data old;
name = 'abc';
run;

proc format;
invalue namefmt
  'abc' = 1
  'bbc' = 2
  'ccc' = 3
;
run;

data new1;
set old;
newname=input(name,namefmt.);
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Valued Guide
Posts: 642

Re: Need Help with Proc format application.

Although not immediately obvious (and a continuing source of confusion) there is a difference between INFORMATs (used by the INPUT statement and the INPUT function) and FORMATs (used by the PUT statement and the PUT function).  The INVALUE statement in PROC FORMAT creates INFORMATs and you will notice from the previous post by @KurtBremser that the type of the INFORMAT depends on the result (numeric INFORMATS yield a numeric result).  FORMATS created by the VALUE statement are numeric if the incoming value is numeric.

Ask a Question
Discussion stats
  • 7 replies
  • 180 views
  • 1 like
  • 4 in conversation