SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
CurtisSmithDCAA
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

3 REPLIES 3
Reeza
Super User

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));

 

 

CurtisSmithDCAA
Obsidian | Level 7

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

 

 

Tom
Super User Tom
Super User

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

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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