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?
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));
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));
Thanks. I was tinkering with putn before but could not get it to work. But, with your encouragement, tried it again and viola!
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
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.
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.