DATA Step, Macro, Functions and more

Assign format to new variables by copy the existing variable format

Reply
Contributor
Posts: 35

Assign format to new variables by copy the existing variable format

Hi All,

I was coding a macro, in that I was trying to automatically assign a format to a series variables using the format of the existing variable format, but was not successful.  So I did a test, here is the result.  Could anyone explain to me what is wrong here?  Also what is the format of "F."?  I did google search, I can not find the answer.  Thanks in advance.

Best,

Abdu

1    DATA Ori;

2

3        FORMAT a $20. b best5.;

4        a = 'Char';

5        b = 123;

6

7        CFmt=vinformat(a);

8        NFmt=vinformat(b);

9

10       FORMAT a1 CFmt b1 NFmt;

11

12       PUT CFmt= NFmt=;

13

14       CFmt2=vinformat(a1);

15       NFmt2=vinformat(b1);

16

17       PUT CFmt2= NFmt2=;

18

19       a1= strip(a) || '1';

20       b1= b * 10;

21

22   RUN;

NOTE: Character values have been converted to numeric values at the places given by: (Line)Smiley SadColumn).

      19:18

CFmt=$20. NFmt=F.

CFmt2=F. NFmt2=F.

NOTE: Invalid numeric data, 'Char1' , at line 19 column 18.

a=Char b=123 CFmt=$20. NFmt=F. a1=. b1=1230 CFmt2=F. NFmt2=F. _ERROR_=1 _N_=1

NOTE: The data set WORK.ORI has 1 observations and 8 variables.

Super User
Posts: 10,538

Re: Assign format to new variables by copy the existing variable format

Simple part: F. is Fixed format, your example data for variable B could be considered F3.0. If you looked at the table such as in proc contents you would see the informat listed as (likely) 5.

You get an error for A1 because you reference A1 in the Vinformat function before it is created, therefore the variable was created as numeric by default.

If you want to determine the display format you want the VFORMAT functions, VINFORMAT is the INFORMAT which you haven't set.

One cludge to get matching characteristics is

a1=a;

a1 = <other operation>;

There are functions such as Call Label that allow setting characteristics but SAS hasn't provided one for Formats or Informats that allow use of variables to set them. Probably because a variable can only have one format and one informat associated at any time.

You could pull the desired information from the data set after creation and then create calls to either Proc Datasets or Proc SQL alter tables to set the format and / or informat after creation.

Contributor
Posts: 35

Re: Assign format to new variables by copy the existing variable format

Thank you, Ballardw.   Yes, I should use vformat instead of vinformat.  As I can get the format strings such as CFmt='$20', VFmt='best5.'.  What is the way to assign these format to other variables.  Apparently, FORMAT statement : FORMAT NewVariable CFmt; does not work.  Seems SAS does not take CFmt as string '$20'.  In the same data step, how can we turn CFmt to '$20' in the FORMAT statement using some kind of Macro variable or other way?

Super User
Posts: 10,538

Re: Assign format to new variables by copy the existing variable format

I have done similar by creating macro variables with the formats and such and using Proc Datasets to modify variables; but as I said, A1= A; as the FIRST use of variable A1 will assign the format and informat of A. Much easier coding task. Of course if you are running lots of records wrap these assignments in a If _n_ = 1 then do; <assignments>; end; structure to one execute once to reduce the redundant execution. Once the variable has an assigned format it is kept.

Frequent Contributor
Posts: 129

Re: Assign format to new variables by copy the existing variable format

This doesn't work for me. Eg

data temp;
var1 = 1;
label var1="test";
format var1 10.3;
run;

data temp2;
set temp;
var2 = var1;
put var1= var2=;
run;

proc print data=temp2 label;run;
Super User
Posts: 5,092

Re: Assign format to new variables by copy the existing variable format

You might it easier to copy all the attributes in this way:

data want;

  set have;

  if 0 then set have (keep=oldvar rename=(oldvar=newvar));

run;

If necessary, you could remove attributes you don't want, such as adding:

label newvar=' ';

Since 0 is false, none of the data will be copied in.  But just mentioning the variable will set up its attributes.

Good luck.

Frequent Contributor
Posts: 129

Re: Assign format to new variables by copy the existing variable format

Returning to this. This macro seems to do the trick.

 

%macro varfmt(dsn, vname) ; 
/***********************************************************************
   Returns the format of the variable. (From a pre-existing data set).
   Example.
   data temp; 
     v1=0;
     format v1 f10.5;
     output;
   run;
   data temp2;
     set temp;
     v2=0;
     * give v2 the same format as v1;
     format v2 %varfmt(temp,v1); 
   run;
   proc print;run;
 *********************************************************************/
   %local exist dsid varcnt rc ; 
   %let exist = 0 ; 
   %if not %sysfunc (exist (&dsn)) %then %do ; 
      %put ERROR: Data set %upcase (&dsn) does not exist. ; 
      %goto mexit ; 
   %end ; 
   %let dsid  = %sysfunc (open (&dsn)) ; 
   %let fmt = %sysfunc(varfmt(&dsid,%sysfunc(varnum(&dsid,&vname))));
   %let rc = %sysfunc(close(&dsid)); 
   &fmt 
   %mexit: 
%mend varfmt; 
Ask a Question
Discussion stats
  • 6 replies
  • 1768 views
  • 0 likes
  • 4 in conversation