Assigning SAS formats to a list of variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Assigning SAS formats to a list of variables

Hi,

I wonder if someone can offer a solution to my problem of assigning SAS formats to a list of variables.

I So, have a list of variables with a variety of SAS variable naming conventions (e.g., SEX, AGE_GROUP, var1-var100, a1A, a1B, etc.). I also have formats for each variable using the name of the variable plus an 'f' suffix as a naming convention (e.g., SEXf, AGE_GROUPf, var1f, a1Af, etc).

Instead of writing a format statement that assigns the format for each variable (e.g., format var1 var1f.Smiley Wink I wonder if it is possible to do this with a %array and a macro statement.

Thanks in advance.

--Tim


Accepted Solutions
Solution
‎01-03-2013 04:08 PM
Super User
Super User
Posts: 7,039

Re: Assigning SAS formats to a list of variables

Pull the list of variables from metadata.  Assume you have a dataset named WORK.HAVE.

This will work if all of your variables are numeric.

proc sql noprint ;

  select catx(' ',name,catx(name,'F.'))

    into :varlist separated by ' '

    from dictionary.columns

    where libname = 'WORK'

      and mename = 'HAVE'

  ;

quit;

Now you can use &VARLIST in a FORMAT statement of another DATA or PROC step.  Or you could use PROC DATASETS to modify the data set and permanently attach the formats.

proc print data=have;

  format &varlist ;

run;

If some of your variables are character then you should really use "$nameF." for the format name, but SAS usually will automatically correct that type of error.  That is if you try to attach character format $SEXF. to a numeric variable SAS will automatically instead search for the numeric format SEXF.

View solution in original post


All Replies
Solution
‎01-03-2013 04:08 PM
Super User
Super User
Posts: 7,039

Re: Assigning SAS formats to a list of variables

Pull the list of variables from metadata.  Assume you have a dataset named WORK.HAVE.

This will work if all of your variables are numeric.

proc sql noprint ;

  select catx(' ',name,catx(name,'F.'))

    into :varlist separated by ' '

    from dictionary.columns

    where libname = 'WORK'

      and mename = 'HAVE'

  ;

quit;

Now you can use &VARLIST in a FORMAT statement of another DATA or PROC step.  Or you could use PROC DATASETS to modify the data set and permanently attach the formats.

proc print data=have;

  format &varlist ;

run;

If some of your variables are character then you should really use "$nameF." for the format name, but SAS usually will automatically correct that type of error.  That is if you try to attach character format $SEXF. to a numeric variable SAS will automatically instead search for the numeric format SEXF.

Occasional Contributor
Posts: 12

Re: Assigning SAS formats to a list of variables

Thanks for the help Tom.

My database doesn't include CHARACTER fields, but it's helpful to know how to assign a character format using this approach.

--Tim

Super Contributor
Posts: 1,636

Re: Assigning SAS formats to a list of variables


example(red parts must in upcase):

proc sql noprint;

select catx(' ',name,cats(name,'f.')) into :names separated by ' '

   from dictionary.columns

     where libname='SASHELP' and memname='CLASS';

quit;

%put &names;

Contributor
Posts: 62

Re: Assigning SAS formats to a list of variables

This article may be useful :

Super User
Posts: 5,499

Re: Assigning SAS formats to a list of variables

Tim,

The posted solutions are pointing in the right direction ... there's another issue I wanted to raise.  Does &VARLIST include all the variables in your data set, or just some.  At this point, the solutions generate formats for every variable in the data set.  If &VARLIST contains just a subset, you may want to start off with:

proc contents data=have (keep=&varlist) noprint out=_contents_ (keep=name);

run;

Then you could use _contents_ instead of dictionary.columns in the proposed solutions.

Also note that you could save TYPE as well as NAME in the output data set.  That might be helpful if you want to add $ at the beginning of the format names for character variables.

Occasional Contributor
Posts: 12

Re: Assigning SAS formats to a list of variables

Posted in reply to Astounding

Thanks Astounding,

For this problem, the &VARLIST would only include variables that require a format. Therefore, not all of the variables in my database would be included in the list. So, the _contents_ solution would be helpful.

Thanks.

--Tim

Super User
Super User
Posts: 7,039

Re: Assigning SAS formats to a list of variables

If you already have the list of variables then simple string manipulation will let you generate the required list of name/format pairs.

%let varlist=age sex;

data _null_;

   length varlist fmtlist $3000 ;

   varlist=symget('varlist');

   do i=1 to countw(varlist);

       fmtlist=catx(' ',fmtlist,scan(varlist,i),scan(varlist,i)||'F.');

   end;

   call symputx('fmtlist',fmtlist);

run;

....

format &fmtlist ;

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 386 views
  • 0 likes
  • 5 in conversation