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

I want to convert a column in dataset to character. I need to do this for 20 columns (different datasets). Below is the macro I came up with. After using options spool, still I am getting " Width specified for format F is invalid" issue. Could anyone please help me with correcting this macro.

 

Thanks in advance.

 

DATA SAM;
SET SASHELP.CLASS;
D='30JUN2021'D; FORMAT D DATE9.;
RUN;

options spool;
%MACRO SUPP (D1=,VAR=);
DATA &D1 ;
LENGTH VAR $200;
SET SAM (WHERE=(~MISSING(&VAR)));

IF VTYPE(&VAR)= "C" THEN VAR=UPCASE(&VAR);
ELSE IF VTYPE(&VAR)="N" AND VFORMAT(&VAR)="BEST12." THEN VAR=PUT(&VAR,200.);
ELSE IF VTYPE(&VAR)="N" AND VFORMAT(&VAR)="DATE9." THEN VAR=PUT(&VAR, DATE9.);

keep name &var; RUN; %MEND; %SUPP(D1=S1, VAR=AGE); %SUPP(D1=S2, VAR=D);
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
IF VTYPE(&VAR)="N" AND VFORMAT(&VAR)="BEST12." THEN VAR=PUT(&VAR,32.);

32 is the max width for this format, which should be fine for a numeric variable, as numbers beyond 15 digits (which these are not) cannot be exactly represented either.


Adding: when writing macros, you need to FIRST create working code without macros and without macro variables that does what you want for one instance (data set S1, variable AGE). You did not do this. (And most people either don't know this advice — or worse ignore it, which is not a good practice) You have code that doesn't work without macros, and so it will NEVER work with macros. In essence, this is not a macro error at all, this is a Base SAS error.

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
IF VTYPE(&VAR)="N" AND VFORMAT(&VAR)="BEST12." THEN VAR=PUT(&VAR,32.);

32 is the max width for this format, which should be fine for a numeric variable, as numbers beyond 15 digits (which these are not) cannot be exactly represented either.


Adding: when writing macros, you need to FIRST create working code without macros and without macro variables that does what you want for one instance (data set S1, variable AGE). You did not do this. (And most people either don't know this advice — or worse ignore it, which is not a good practice) You have code that doesn't work without macros, and so it will NEVER work with macros. In essence, this is not a macro error at all, this is a Base SAS error.

--
Paige Miller

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 2 replies
  • 670 views
  • 1 like
  • 2 in conversation