DATA Step, Macro, Functions and more

Convert a Macro Variable Value to Numeric Format

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Convert a Macro Variable Value to Numeric Format

Hi. I want to convert a macro variable value, which is all numerals (which is character text, of course) to a numeric format. I followed

Chris Yindra's insightful paper "%SYSFUNC - The Brave New Macro World" but am not successful. Chris has the following example, which I can replicate to work:

 

%LET MYDATE = 971006;

%put NOTE: original value: &MYDATE.;

%macro chngfmt(invar,infmt);

%let &invar = %sysfunc(inputn(&&&invar,&infmt));

%mend chngfmt;

%chngfmt(MYDATE,YYMMDD6.);

%put NOTE: sas data value: &MYDATE.;

 

But, I want to convert to a simple comma format. Typically, I want to display the row count in output with commas. My code looks like the following, but does not change the format (I used a %let statement in my example for simplicity):

 

%let row_count=10000;

%put NOTE: original value: &row_count.;

%macro chngfmt(invar,infmt);

%let &invar = %sysfunc(inputn(&&&invar,&infmt));

%mend chngfmt;

%chngfmt(row_count,comma15.);

%put NOTE: sas data value: &row_count.;

 

Any suggestions?


Accepted Solutions
Solution
‎10-16-2017 02:36 PM
Super User
Posts: 19,861

Re: Convert a Macro Variable Value to Numeric Format

Posted in reply to CurtisSmithDCAA

You want PUT, not INPUT.

 

You may also have scope issues, ie is the macro variable global or local.

 

%let &invar = %sysfunc(putn(&&&invar,&infmt));

 

 

View solution in original post


All Replies
Solution
‎10-16-2017 02:36 PM
Super User
Posts: 19,861

Re: Convert a Macro Variable Value to Numeric Format

Posted in reply to CurtisSmithDCAA

You want PUT, not INPUT.

 

You may also have scope issues, ie is the macro variable global or local.

 

%let &invar = %sysfunc(putn(&&&invar,&infmt));

 

 

Occasional Contributor
Posts: 19

Re: Convert a Macro Variable Value to Numeric Format

Thanks. I was tinkering with putn before but could not get it to work. But, with your encouragement, tried it again and viola!

 

 

Super User
Super User
Posts: 7,076

Re: Convert a Macro Variable Value to Numeric Format

[ Edited ]
Posted in reply to CurtisSmithDCAA

To format the number with commas you want to use the comma FORMAT, not the comma INFORMAT.

If you are going to have macro variables with values in formatted style then perhaps your macro should include both an INFORMAT and FORMAT. That way you could read in YYMMDD values and output DATE9 values, for example.

 

Let's build in some debugging logic for our testing here.

 

%macro chngfmt(invar,infmt,outfmt,debug=0);
%if (&debug) %then %put Input value: %superq(&invar);
%let &invar = %sysfunc(inputn(%superq(&invar),&infmt),&outfmt);
%if (&debug) %then %put Result value: %superq(&invar);
%mend chngfmt;

 

Then your examples might look like this.

7004   %LET MYDATE = 971006;
7005   %chngfmt(MYDATE,YYMMDD6,debug=1);
Input value: 971006
Result value: 13793
7006
7007   %let row_count=10000;
7008   %chngfmt(row_count,f32,comma15,debug=1);
Input value: 10000
Result value: 10,000
7009
7010   %chngfmt(MYDATE,F32,DATE9,debug=1);
Input value: 13793
Result value: 06OCT1997
7011   %chngfmt(row_count,comma15,debug=1);
Input value: 10,000
Result value: 10000

 

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 100 views
  • 1 like
  • 3 in conversation