BookmarkSubscribeRSS Feed
RobertWF1
Quartz | Level 8

I'm trying to create the binary format binary3. by concatenating the values 'binary', n, and '.', where n=3 is a value that will be read in from the data step (or possibly a macro variable).

 

So far the following code doesn't work - it should output a list of binary values for the integers 1 through 8 in the log file. Any ideas what's wrong with my format statement?

 

data _null_;
n = 3;
rows = 2**n;
/** construct one row at a time **/
format = cats('binary', char(n,1), '.');
do i = 1 to rows;
put @1 i format;
/* put @1 i binary3.;*/
end;
run;

4 REPLIES 4
andreas_lds
Jade | Level 19

Only the putc/putn function allow dynamic formats.

You could move the format-name to a macro-variable:

%let n = 3;
%let binaryformat = binary&n..;

data _null_;
   rows = 2**&n.;
   
   do i = 1 to rows;
      put i &binaryformat.;
      /* put @1 i binary3.;*/
   end;
run;
RobertWF1
Quartz | Level 8
Fantastic, thank you Andreas!
yabwon
Amethyst | Level 16
data _null_;
  n = 3;
  rows = 2**n;
  /** construct one row at a time **/
  format = cats('binary', n, '.');
  do i = 1 to rows;
    b = putn(i, format);
    put @1 i b;
  end;
run;

B.

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



RobertWF1
Quartz | Level 8
Beautiful, thank you!

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1051 views
  • 4 likes
  • 3 in conversation