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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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