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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 429 views
  • 1 like
  • 2 in conversation