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

This might be asking to much, but the last step asks me to use a format width in the put statement for arguments without a format.

So I need to produce the following output with the macro:

data_null_ ;

set books.list;

file "list4.data";

put @1 bookid 4. author 5-34 @35 isbn isbn18. @43 yearpub comma6.;

run;

with the a macro call of : %writeoutD (books.list,list4.data,bookid author isbn yearpub,4. 30 isbn18. comma6.);

I tried changing it to the following:

%local newput i newpointer;

%do i=1 %to 4;

%if &i=1

%then %let newpointer=1;

%else %let newpointer=%eval(&newpointer+%sysfunc(scan(&formatlist,%eval(&i-1),%str( $),ap)));

%let newput=&newput @&newpointer %sysfunc(scan(&variables,&i,%str( ))) %sysfunc(scan(&formatlist,&i,%str( )));

%if %sysfunc(findc("scan(&formatlist,&i)",'.'))=0

%then %let newput=&newput %sysfunc(scan(&variables,&i,%str( ))) %eval(&newpointer)-%eval(&newpointer+%sysfunc(scan(&formatlist,&i,%str( $),ap))+1);

%end;

but it was a no-go, not sure if its in the wrong spot or something...any ideas?

data_null__
Jade | Level 19

This looks to be working.  I created more macro variables, too hard to work with all those nested functions for me, I'm too old.

%macro writeoutC (dataset,datafile,variables,formatlist);
  
%local newput i newpointer f d w v wm1;
  
%let wm1=0;
   %do i=1 %to 4;
     
%let f=%scan(&formatlist,&i,%str( ));
      %let d=%sysevalF(%index(&f,.),boolean); /*Format or width*/
     
%let w=%substr(_&f,%sysfunc(findc(_&f,$.,dkb)));
      %put NOTE: &=f &=w;
      %let w=%sysfunc(compress(&f,%str(.),DK));
     
%let v=%scan(&variables,&i,%str( ));
     
%put NOTE: &=f &=d &=w &=v &=wm1;

     
%if &i eq 1
        
%then %let newpointer=1;
         %else %let newpointer=%sysevalf(&newpointer+&wm1,INTEGER);
      %if &d
        
%then %let newput=&newput @&newpointer &v:&f;
         %else %let newput=&newput &v &newpointer-%sysevalf(&w+&newpointer-1);
      %let wm1 = &w;
      %end;

  
%put NOTE: %superq(newput);
  
/*
   data _null_;
   set &dataset;
   file "&datafile";
   put @1 &newput;
   run;
   */

  
%mend writeoutC;

/*Part 10*/

%
writeoutC (books.list,list2.txt,bookid author isbn yearpub,4. 30 isbn18. comma6.);

NOTE: @
1 bookid:4. author 5-34 @35 isbn:isbn18. @53 yearpub:comma6.

Message was edited by: data _null_

zkreflex
Calcite | Level 5

You're the best.  A few questions:

1) %sysevalF(not not %index(&f,.)) < what is this going to return? I understand the index function finds the character position of the . in each format argument, but the double not is confusing me..


2) You use the structure "_&f" < what does this do?


3) Is the %sysevalF function just an alternative to %eval?


Thanks for everything, enjoy the holiday!

data_null__
Jade | Level 19

zkreflex wrote:

You're the best.  A few questions:

1) %sysevalF(not not %index(&f,.)) < what is this going to return? I understand the index function finds the character position of the . in each format argument, but the double not is confusing me..

Not not just make it a Boolean I should have use the Boolean option.  %let d=%sysevalF(%index(&f,.),boolean);


2) You use the structure "_&f" < what does this do?

We need to have something the FIND when the format has only numbers and dot.  For example 10. would return 0 and we don't want that.


3) Is the %sysevalF function just an alternative to %eval?

Sort of but it does floating point arithmetic and has options like INTEGER and BOOLEAN.


Thanks for everything, enjoy the holiday!

Astounding
PROC Star

Getting closer.  "scan" should become "%scan" in two places, and the hard-coded @1 is not needed.  It will be part of &NEWPUT.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 19 replies
  • 3669 views
  • 0 likes
  • 6 in conversation