BookmarkSubscribeRSS Feed
Abdu
Calcite | Level 5

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):(Column).

      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.

6 REPLIES 6
ballardw
Super User

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.

Abdu
Calcite | Level 5

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?

ballardw
Super User

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.

BruceBrad
Lapis Lazuli | Level 10

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;
Astounding
PROC Star

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.

BruceBrad
Lapis Lazuli | Level 10

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; 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 6594 views
  • 0 likes
  • 4 in conversation