BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tallkell
Fluorite | Level 6

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.;) I wonder if it is possible to do this with a %array and a macro statement.

Thanks in advance.

--Tim

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

7 REPLIES 7
Tom
Super User Tom
Super User

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.

tallkell
Fluorite | Level 6

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

Linlin
Lapis Lazuli | Level 10


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;

MohammadFayaz
Calcite | Level 5

This article may be useful :

Astounding
PROC Star

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.

tallkell
Fluorite | Level 6

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

Tom
Super User Tom
Super User

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 ;

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
  • 7 replies
  • 2596 views
  • 0 likes
  • 5 in conversation