BookmarkSubscribeRSS Feed
yabwon
Amethyst | Level 16

Short and rough answer to your question is: because the code is poorly written. I know, it's "zero value" answer. Now let me give you some value.

 

1) I think I can guess what you want to do with your code, but since I'm not 100% sure that's why I asked: "what do you want to do?". 

2) My best guess is that, for a given dataset A you want to print the list of variables names and their types for that dataset and add a title saying which dataset it is.

If this is the case, use code from my answer

 

3) Now let's look at your code and explain why I think it's "poorly written".

a) first thing is: you rewrote the macro two times with just one change which is constant hard-coded value of title statement, you have 2 copies of almost identical code (stored under the same macro name), so if you want to change something in it you have to change it twice - this is very error prone... and the second overwrite the first one. An Macros are all about re-usability.

b) since it it only one statement that differentiate those codes, and it is statement that is related to your data, I would add new parameter to your macro which would hold the title value. This would make that 2 macro one. Something like this:

 

%macro check_column_types(libname=, memname=, myTitle=);
    proc contents data=&libname..&memname out=columns_info(keep=name type);
    run;

    /* Skapa temp-dataset */
    data column_types;
        set columns_info;
/*ATTRN (en sas-funktion som används för att hämta attribut för en variabel i ett dataset)  bestämemer datatypen*/
        array char_cols[*] _character_;
        array num_cols[*] _numeric_;
        do i = 1 to dim(char_cols);
            if vname(char_cols[i]) = name then do;
                column_type = 'CHAR';
                output;
            end;
        end;
        do i = 1 to dim(num_cols);
            if vname(num_cols[i]) = name then do;
                column_type = 'NUM';
                output;
            end;
        end;
    run;

    proc print data=column_types noobs;
        title &myTitle.;
    run;
    
    /* Rensa temp-datasets */
    proc datasets library=work nolist;
        delete columns_info column_types;
    quit;
%mend;
%check_column_types(libname=work, memname=banana, myTitle='BANANA NNANANBANANABNASBA');

%check_column_types(libname=work, memname=strawberry,myTitle='STRAWBERRY BERYRYEYRYRWEBERBE');

c) Now, this is not yet the answer to your question "why banana prints out but strawberry doesn't?" It's just names the code "less bad programming"

 

d) You asked: "why banana prints out but strawberry doesn't?" but in fact you should ask: "why banana prints out wrong AND strawberry doesn't at all?" If you take all your original code (it's irrelevant what it does for a moment) and run it in a fresh sas session you will see 2 (two) outputs printed, the firs one (for banana data) titled: "The SAS System" and the second one (for strawberry data) titled: "BANANA NNANANBANANABNASBA".

The reason why it happens this way is the following: In your code you have the following "order" of steps and statements: 

i) proc contents (which produces a data set and an output)

ii) data step which does something (and btw. produces an empty data set)

iii) title statement which sets the title for your output (and overwrites the default one, i.e., "The SAS System")

iv) proc print which does not print anything (because data set is empty)

v) proc datasets to delete your temporary datasets

 

NOTE: the title statement is a global one so doing the code this way:

 title &myTitle.;
 proc print data=column_types noobs;
 run;

would be better idea (there is no confusion what is the order of execution)

 

If you look at the code execution order you will see that whenever the macro is executed the code which produces output (only proc contents in your case) is run before title is set. That's why when you first time run the macro the default title is used, then the title is set to "banana...", and when you run the macro for the second time newly set title ("banana....") is used. The end of a macro execution does not "reset" titles, they last for all the session, you have to run 

title;

explicitly to clear them.

 

I hope this explains you a bit.

 

And, as I wrote, if you want variables and their types printed, use my code.

 

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



Reeza
Super User

Here's an example that shows you title persists and repeats for each printed procedure until you reset it.

 

title 'this is my title which will repeat';
proc print data=sashelp.class;
run;

proc print data=sashelp.air(obs=10);
run;

title;
*no more title;
proc print data=sashelp.cars(obs=10);
run;


@melhaf wrote:

But Banana is still coming from another dataste then strawberry. Sorry, but I seriously don't get 


 

Tom
Super User Tom
Super User

@melhaf wrote:

But Banana is still coming from another dataste then strawberry. Sorry, but I seriously don't get 


I am not sure why you care about what TITLE is printed when the rest of the code is gibberish.

 

You have two places in those macros that could produce output.  PROC CONTENTS and PROC PRINT.

 

In the first one you do not set any title. So the titles previously set are used.

In the second one you do set TITLE1, which will replace all existing titles since existing TITLE2 to TITLE10 are cleared then TITLE1 is set.

 

Make the code take full control of the titles.  So set near the top of the macro and reset it at the end.

proc contents data=&libname..&memname out=columns_info(keep=name type);
    title 'BANANA NNANANBANANABNASBA';
run;
....
proc print data=column_types noobs;
/* no need to change the title here since it remembers the previous setting */
run;
....
* Clear the title ;
title ; 
PaigeMiller
Diamond | Level 26

As I read the original post, the request from @melhaf was not discussing the title. Nothing in that post said the concern is that titles are not printing.

 

Now, 20 or so replies later, is it really the title not printing that is why the thread was created?  @melhaf please clarify this.

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 18 replies
  • 3442 views
  • 2 likes
  • 6 in conversation