How can I make a character variable equal to the formatted value of a numeric variable for arbitrary SAS formats?

前端 未结 5 570
不知归路
不知归路 2021-02-08 19:57

If I have a numeric variable with a format, is there a way to get the formatted value as a character variable?

e.g. I would like to write something like the following to

5条回答
  •  忘了有多久
    2021-02-08 20:22

    This seemed to work for a couple that I tried. I used VARFMT and a macro function to retrieve the format of the given variable.

     data test;
      format i ddmmyy10. b comma12.;
      i = "10JUN2009"d;
      b = 123405321;
    run;
    
    
    %macro  varlabel(variable) ;
      %let dsid=%sysfunc(open(&SYSLAST.)) ;
      %let varnum=%sysfunc(varnum(&dsid,&variable)) ;
      %let fmt=%sysfunc(varfmt(&dsid,&varnum));
      %let dsid=%sysfunc(close(&dsid)) ;
      &fmt
    %mend varlabel;
    
    data test2;
      set test;
      i_formatted = put(i, %varlabel(i) );
      b_formatted = put(b, %varlabel(b) );
      put i_formatted=;
      put b_formatted=;
    run;
    

    This gave me:

    i_formatted=10/06/2009
    b_formatted=123,405,321
    

提交回复
热议问题