DATA Step, Macro, Functions and more

Two Formats for one variable

Accepted Solution Solved
Reply
Contributor
Posts: 60
Accepted Solution

Two Formats for one variable

I want dollar32. format for non-decimnal value. and dollar32.2 for decimal containing value..

 

I've write below code but its not working as expected.  

 


data ds5;
input var5 32. ;
if scan(var5,2,'.') gt 0 then var5=input(var5,dollar32.2);
else var5=input(var5,dollar32.);


datalines;
625355
856422.54121
818935.584
456464.51
560843.1
558.55
58.00
5
;
run;


Accepted Solutions
Solution
‎06-28-2017 09:12 AM
Super User
Posts: 9,687

Re: Two Formats for one variable

Try PROC FCMP + PROC FORMAT .

 

proc fcmp outlib=work.func.t;
function xx(x) $;
length temp $ 40;
  if mod(x,1)=0 then temp=put(x,dollar32.);
   else temp=put(x,dollar32.2);
  return (temp);
endsub;
run;

options cmplib=work.func;
proc format;
value fmt
 low-high=[xx()];
run;


data ds5;
input var5 32. ;
format var5 fmt40.;
datalines;
625355
856422.54121
818935.584
456464.51
560843.1
558.55
58.00
5
;
run;
proc print;run;

View solution in original post


All Replies
Super User
Posts: 5,085

Re: Two Formats for one variable

You are asking about formats, but showing a program that doesn't use any formats.  All of these are informats within the context of your program:

 

32.

dollar32.

dollar32.2

 

What is it that you are trying to accomplish as the end result?

Contributor
Posts: 60

Re: Two Formats for one variable

I wants value like in want column : 

 

have              want
625355          $625,355
856422.5412 $856,422.54
818935.584   $818,935.58
456464.51     $456,464.51
560843.1       $560,843.10
558.55           $558.55
58.00             $58
5                    $5

PROC Star
Posts: 552

Re: Two Formats for one variable

A variable can have no more than 1 format. What you are using here though in your INPUT functions are INformats, which are used to tell SAS how to READ data. not WRITE it.

SAS Super FREQ
Posts: 8,743

Re: Two Formats for one variable

[ Edited ]

Hi:
Also, your var5 is a numeric variable. The SCAN function is designed for character variables. So you should be seeing some messages in your log.

It is not clear what you are trying to do. The INPUT function is how you convert a character variable (such as a character string for date or a number with currency) into a number. But you are starting with a number. I don't understand what you're trying to do. Have you even looked at the output from your program without the INPUT???

not_understand_scan.png

 

You do not need different formats for storing the internal value of the variable. Do you want different formats for displaying the value of VAR5 in a report? If so, there are other ways to accomplish that. But what you are doing in your program is not clear.


cynthia

Solution
‎06-28-2017 09:12 AM
Super User
Posts: 9,687

Re: Two Formats for one variable

Try PROC FCMP + PROC FORMAT .

 

proc fcmp outlib=work.func.t;
function xx(x) $;
length temp $ 40;
  if mod(x,1)=0 then temp=put(x,dollar32.);
   else temp=put(x,dollar32.2);
  return (temp);
endsub;
run;

options cmplib=work.func;
proc format;
value fmt
 low-high=[xx()];
run;


data ds5;
input var5 32. ;
format var5 fmt40.;
datalines;
625355
856422.54121
818935.584
456464.51
560843.1
558.55
58.00
5
;
run;
proc print;run;
Contributor
Posts: 60

Re: Two Formats for one variable

Thank you so much.... Smiley Happy
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 115 views
  • 0 likes
  • 5 in conversation