BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
stataq
Quartz | Level 8

hello,

 

I have a file for footnotes, like below:

data fnt;
length fnt_1 $80   fnt_2 $80  fnt_3  $80  fnt_4  $80;
 infile datalines dlm=',' ;
 input fnt_1    fnt_2   fnt_3    fnt_4 ;
 datalines;
"Teach us the most important lessons in life", "Give the best advice and know the right thing to say." ,"Given us two of the best things we could ever have: life and love.", "Loved us unconditionally"
;
run;

I would like to use do loop to write my footnotes. I had my codes as:

data _null_;
set fnt;

  do i=1 to 4.;
       call execute( cat( "footnote"
                                    , i
                                     , " j = l "
                                                 ,  fnt_i
                                        
                                    , " ;"
                                    )
                                ) ;
         	end ;
run;

However I got errors as below. 1-4 were added to footnote, but I could not get the value of fnt_1 to fnt_4 out.

stataq_0-1710435339550.png

Could anyone guide me on this?  Ideal outcome is I can generate the line as:

footnote1 j=l "Teach us the most important lessons in life";
footnote2 j=l "Give the best advice and know the right thing to say.";

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

you could do it like this:

data _null_;
set fnt;
  do fnt = fnt_1, fnt_2, fnt_3, fnt_4;
   i+1;
   call execute( cat( cats("footnote",i), " j = l ", fnt, " ;")) ;
  end ;
run;

but I wouldn't call it "good programming practice"

 

Array in SAS, in it's fundamental version, is just a way to loop over multiple variables conveniently.

 

Bart

_______________
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



View solution in original post

4 REPLIES 4
yabwon
Onyx | Level 15
data _null_;
set fnt;
  array fnt[*] fnt_:;
  do i=1 to 4;
   call execute( cat( cats("footnote",i), " j = l ", fnt[i], " ;")) ;
   end ;
run;
_______________
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



stataq
Quartz | Level 8

thanks so much!

I am new to sas.  Please pardon me if my question is too silly : Do we have to do array? Is it possible to directly use value from fnt.fnt_1 to fnt_4?

Tom
Super User Tom
Super User

Sure.  

If you want FNT_1 then just type that string into you program instead.  Get it to work to generate one footnote from one variable.  Then replicate the line and change the 1's to 2's.  Repeat.  That is called WALLPAPER code since it has a repeating pattern like a wallpaper pattern.

 

But the ARRAY statement allows you to reference variables by an index so that it is possible to loop over the index.

yabwon
Onyx | Level 15

you could do it like this:

data _null_;
set fnt;
  do fnt = fnt_1, fnt_2, fnt_3, fnt_4;
   i+1;
   call execute( cat( cats("footnote",i), " j = l ", fnt, " ;")) ;
  end ;
run;

but I wouldn't call it "good programming practice"

 

Array in SAS, in it's fundamental version, is just a way to loop over multiple variables conveniently.

 

Bart

_______________
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



SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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