BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DavePrinsloo
Pyrite | Level 9

As of SAS release 9.4m3 it is possible to determine the "type" or category of a  SAS column by using the FMTINFO function. 

 

Examples 

%let fmttype=%sysfunc(fmtinfo(DATE,cat));   returned value: date
%let fmttype=%sysfunc(fmtinfo(DATETIME20.2,cat));  returned value: UNKNOWN
%let fmttype=%sysfunc(fmtinfo(DATETIME,cat)); returned value: datetime

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Why not just use the CAT type value from the FMTINFO() function?

%macro fmtcat(format);
%local i base;
%if %length(&format) %then %do;
  %let i=%sysfunc(verify(%qsysfunc(reverse(&format)),.0123456789));
  %if &i %then %let base=%sysfunc(substrn(&format,1,%length(&format)-&i+1));
  %else %let base=F;
  %sysfunc(fmtinfo(&base,cat))
%end;
%mend fmtcat;

Examples:

195  %put %fmtcat(datetime24.3);
datetime
196  %put %fmtcat(time8.);
time
197  %put %fmtcat(best);
num
198  %put %fmtcat($char);
char
199  %put %fmtcat(32.);
num
200  %put %fmtcat(F);
num
201  %put %fmtcat($);
char
202  %put %fmtcat($10);
char
203  %put %fmtcat($10.);
char
204  %put %fmtcat();

205  %put %fmtcat(xyz);
UNKNOWN

View solution in original post

4 REPLIES 4
Quentin
Super User

Design question:

 

Why do you have two parameters posfmt and format= ? 

 

%macro ut_fmt2type(posfmt,format=, lognote=N);

%local _this_fmt_type varfmt ;
/* allow positional and named parameter, prefer named parameter */
%let varfmt=%sysfunc(coalescec(&format.,&posfmt.));

It looks like this is a positional parameter and a keyword parameter for the same parameter, so that a user can call in either manner.

 

But even if you define a parameter as positional, a user is still free to call it as a keyword parameter, which is a really nice feature of the macro language.  So I would think you could do this as:

 

%macro ut_fmt2type(format, lognote=N);
The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
DavePrinsloo
Pyrite | Level 9
I have been using SAS for 30+ years and I never knew that, Thanks!
I will modify the macro and repost. (But I need to leave to go to a drive-in Coronavirus test in 30 minutes, so that will have to wait!)
Quentin
Super User
Good luck with your test!
The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
Tom
Super User Tom
Super User

Why not just use the CAT type value from the FMTINFO() function?

%macro fmtcat(format);
%local i base;
%if %length(&format) %then %do;
  %let i=%sysfunc(verify(%qsysfunc(reverse(&format)),.0123456789));
  %if &i %then %let base=%sysfunc(substrn(&format,1,%length(&format)-&i+1));
  %else %let base=F;
  %sysfunc(fmtinfo(&base,cat))
%end;
%mend fmtcat;

Examples:

195  %put %fmtcat(datetime24.3);
datetime
196  %put %fmtcat(time8.);
time
197  %put %fmtcat(best);
num
198  %put %fmtcat($char);
char
199  %put %fmtcat(32.);
num
200  %put %fmtcat(F);
num
201  %put %fmtcat($);
char
202  %put %fmtcat($10);
char
203  %put %fmtcat($10.);
char
204  %put %fmtcat();

205  %put %fmtcat(xyz);
UNKNOWN

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 1689 views
  • 14 likes
  • 3 in conversation