Hi All,
I want to assign format to new variables automatically. But have trouble to assign it. I have two issues here:
1. I used Varfmt function to get the variable's format. Question: If the variable's format did not specified, what value it return? I tried '', it did not work, now I used length function to test the format string length. I got a lot of error messages.
2. In my program, my FORMAT statement does not work for macro variables: FORMAT &&RetainV&n $200.;
Could anyone get me out of here? I have worked on this issue for more than a week. I really need your help. I pasted all my code here.
Thank you so much.
Abdu.
DM LOG 'Clear';
DATA Ori;
FORMAT a $20. b best5.;
a = 'Char';
b = 123;
c = 'testChar';
d = 456;
RUN;
%MACRO abcd(InDsn=, Var1=, Var2=, Var3=, Var4=,
RetainV1=, RetainV2=, RetainV3=, RetainV4=, OutDsn=);
%LET DSID = %SYSFUNC(OPEN(&InDsn, i));
%DO n = 1 %TO 4;
%LET Col_n = %SYSFUNC(varnum(&DSID, &&Var&n));
%IF &Col_n = 0 %THEN %DO;
%LET Var&n = ;
%END;
%ELSE %DO;
%LET Fmt&n = %SYSFUNC(varfmt(&DSID, &Col_n));
%PUT NOTE: Var&n=&&Var&n Format=&&Fmt&n;
%END;
%END;
%IF %SYSFUNC(CLOSE(&DSID)) ^= 0 %THEN %DO;
%PUT NOTE: DATASET &InDsn=&DSID did not close properly!;
%END;
DATA &OutDsn;
SET &InDsn;
IF _N_ = 1 THEN DO;
%DO n = 1 %TO 4;
IF VTYPE(&&Var&n) = 'C' THEN DO;
LENGTH &&RetainV&n $200;
IF LENGTH(%STR(&&Fmt&n)) = 0 THEN DO;
FORMAT &&RetainV&n $200.;
END;
ELSE FORMAT &&RetainV&n %STR(&&Fmt&n);
RETAIN &&RetainV&n;
END;
ELSE IF VTYPE(&&Var&n)= 'N' THEN DO;
IF LENGTH(%STR(&&Fmt&n)) = 0 THEN DO;
FORMAT &&RetainV&n BEST12.;
END;
ELSE FORMAT &&RetainV&n %STR(&&Fmt&n);
RETAIN &&RetainV&n;
END;
%END;
END;
RUN;
%MEND abcd;
OPTIONS SYMBOLGEN MPRINT MLOGIC;
%abcd(InDsn=Ori, Var1=a, Var2=b, Var3=c, Var4=d,
RetainV1=RtnV1, RetainV2=RtnV2, RetainV3=RtnV3, RetainV4=RtnV4, OutDsn=Out);
OPTIONS NOSYMBOLGEN NOMPRINT NOMLOGIC;
You cannot conditionally execute FORMAT statement in a DATA step, but since you are running a macro anyway why are you even trying?
You already opened the dataset and examined the metadata why not go ahead and get the vartype while you have it open. Then use macro logic to conditionally generate the appropriate data step statements.
I don't think format statements are conditional, i.e. you can't use them the way you want.
How are you identifying new variables and what's your rules for determining the format for a new variable?
You can look as sashelp.vcolumn or diction.column table to see what formats are applied to each variable.
Thank you for the response, Reeza.
I want to dynamically generate new variables according some variables in the dataset. If FORMAT statement can not be used conditionally, is there other ways to set a new variables to a specific format? I can identify the format of the existing variables, but have trouble to set new variables to proper format. Please suggest.
Best,
Abdu
You might be better off looking at your variables and then generating code for Proc Datasets to change varible attributes such as format after the set is created especially if you need custom character formats.
Unspecified numeric variables will get a Best type format but the width, such as Best12. vs Best32. are somewhat esoteric.
Character variables will have a default $w where w correspondes to something related to how the varible was created. Note that some functions will explicitly state the width will be 200 but others don't.
You cannot conditionally execute FORMAT statement in a DATA step, but since you are running a macro anyway why are you even trying?
You already opened the dataset and examined the metadata why not go ahead and get the vartype while you have it open. Then use macro logic to conditionally generate the appropriate data step statements.
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!
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.