DATA Step, Macro, Functions and more

Assign variable format Dynamically in Macro

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

Assign variable format Dynamically in Macro

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;


Accepted Solutions
Solution
‎06-02-2014 09:28 PM
Super User
Super User
Posts: 7,039

Re: Assign variable format Dynamically in Macro

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.

View solution in original post


All Replies
Super User
Posts: 19,769

Re: Assign variable format Dynamically in Macro

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.

Contributor
Posts: 35

Re: Assign variable format Dynamically in Macro

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

Super User
Posts: 11,343

Re: Assign variable format Dynamically in Macro

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.

Solution
‎06-02-2014 09:28 PM
Super User
Super User
Posts: 7,039

Re: Assign variable format Dynamically in Macro

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.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 635 views
  • 4 likes
  • 4 in conversation