DATA Step, Macro, Functions and more

quote and catq usage explanation

Reply
Occasional Contributor
Posts: 11

quote and catq usage explanation

I would like to make changes to the following program in order to get 2 dataset, instead of 3, each dataset with two columns label1 and label2 containing respectively the strings ONE and TWO. I've seen around i can use catq and off course quote, but i can't figure out how.

 

%MACRO TEST;
  %LET VAR1 = 'ONE';
  %LET VAR2 = TWO;
  DATA TEMP&VAR1. TEMP&VAR2.;
    LABEL1 = &VAR1.;
    LABEL2 = &VAR2.;
  RUN;
%MEND;

%TEST;

Valued Guide
Posts: 624

Re: quote and catq usage explanation

Posted in reply to arjunascagnetto

Before you start using macros have working code (working= no errors, warnings and unexpected notes). Then ask yourself: is a macro really necessary? Does it reduce the amount of code required to solve an issue? If you can't say YES to both questions, stick to normal code.

When working with macros, enable the options mprint, mlogic and symbolgen. Then execute the code and read the log.

 

Some hints:

  • don't use quotes in %let
  • use quotes only when you need a string
  • in macro-code everything is string
  • see the log to understand what the macro-compiler generated

 

Another point: please avoid coding all upcase.

 

 

Super User
Super User
Posts: 9,799

Re: quote and catq usage explanation

Posted in reply to arjunascagnetto

Please describe what you want to do by presenting test data in the form of a datastep, and what you want the output to look like.  As noted there are various issues with the code.  Also, is there a good reason to split one dataset to two - very rarely is this the case.

The code you presented actually does nothing more than:

data temp_one temp_two;
  label one "one" two "two";
run;

So the question is why that code at all, and in fact why the code full stop, as the datastep does nothing.

Respected Advisor
Posts: 3,251

Re: quote and catq usage explanation

Posted in reply to arjunascagnetto

@arjunascagnetto wrote:

I would like to make changes to the following program in order to get 2 dataset, instead of 3, each dataset with two columns label1 and label2 containing respectively the strings ONE and TWO. I've seen around i can use catq and off course quote, but i can't figure out how.

 

%MACRO TEST;
  %LET VAR1 = 'ONE';
  %LET VAR2 = TWO;
  DATA TEMP&VAR1. TEMP&VAR2.;
    LABEL1 = &VAR1.;
    LABEL2 = &VAR2.;
  RUN;
%MEND;

%TEST;


There is no CATQ function in SAS.

 

You have code that does nothing even if it were to work properly. You need to get the code working properly without macros FIRST (as others have pointed out) and then you might find out you don't need macros (which you don't) in this case.

--
Paige Miller
Occasional Contributor
Posts: 11

Re: quote and catq usage explanation

[ Edited ]
Posted in reply to PaigeMiller

it's related to a more complex problem. This is the summary of the problem. Please think about it as a proof of concept, not a real problem. Show me only how to get 2 dataset with two columns named label1 and label2 containing the strings ONE and TWO. Nothing else.

Respected Advisor
Posts: 3,251

Re: quote and catq usage explanation

Posted in reply to arjunascagnetto

Well then, @arjunascagnetto, your summary is code that does nothing, and unnecessarily uses macros when no macros are needed, and so we cannot advise your further.

 

Can you show us the relevant parts of the more complex problem?

--
Paige Miller
Occasional Contributor
Posts: 11

Re: quote and catq usage explanation

Posted in reply to PaigeMiller
i know it does nothing it's just an example. I don't know if macros are realy useless. I'll show you something more similar to the problem.

lista comes from a into statement inside a proc sql.

%MACRO SFOGLIAM;
%LET LEN=%SYSFUNC(COUNTW(&LISTA.,','));
%DO I=1 %TO &LEN.;
%LET VAR = %SCAN(&LISTA.,&I.,',');
DATA TEMP_&VAR.;
LABEL = &VAR.;
/* other code not relevant*/
RUN;
%END;
%MEND;
Respected Advisor
Posts: 3,251

Re: quote and catq usage explanation

[ Edited ]
Posted in reply to arjunascagnetto

@arjunascagnetto wrote:
i know it does nothing it's just an example. I don't know if macros are realy useless. I'll show you something more similar to the problem.



I did not say macros are useless. I said they were unnecessary in the original example you showed.

 

I'll show you something more similar to the problem.

lista comes from a into statement inside a proc sql.

%MACRO SFOGLIAM;
%LET LEN=%SYSFUNC(COUNTW(&LISTA.,','));
%DO I=1 %TO &LEN.;
%LET VAR = %SCAN(&LISTA.,&I.,',');
DATA TEMP_&VAR.;
LABEL = &VAR.;
/* other code not relevant*/
RUN;
%END;
%MEND;

 

Clearly here there seems to be some value in have the macro variable &LISTA parsed and used, but again, we cannot help you with the original problem because you have not explained the original problem, other than showing us code that doesn't work and does nothing if it did work.

 

We are asking you to now clearly state the problem you face, and show us working code that does the job you want in a single instance. If we can see what the code does in a single instance, and we understand the problem, we can help you make the code dynamic by using macros, if that is the best solution (which it may not be).

--
Paige Miller
Occasional Contributor
Posts: 11

Re: quote and catq usage explanation

Posted in reply to PaigeMiller
Occasional Contributor
Posts: 11

Re: quote and catq usage explanation

Posted in reply to arjunascagnetto
Easy solution for half of the problem. The other half i think has no solution.

%MACRO TEST;
%LET VAR1 = 'ONE';
%LET VAR2 = TWO;
DATA TEMP&VAR1. TEMP&VAR2.;
LABEL1 = &VAR1.;
LABEL2 = "&VAR2.";
RUN;
%MEND;

%TEST;
Super User
Posts: 6,903

Re: quote and catq usage explanation

Posted in reply to arjunascagnetto

Let's start with the original question.  Given that it has value because it is a simplification of a more complex problem:

 

%macro test;

   %let var1 = ONE;

   %let var2 = TWO;

   data temp&one temp&two;

      label1 = "&var1";

      label2 = "&var2";

   run;

%mend test;

%test

 

Once you agree with that as a starting point, we can consider something that approaches real-life complexity.

Occasional Contributor
Posts: 11

Re: quote and catq usage explanation

Posted in reply to Astounding
You removed the quote from the first let statemente and you add the double quotes to label1 and label2. My code was different since the problem was quoting. Anyway i found out the solution by my self, thanks anyway because you make me think.
Ask a Question
Discussion stats
  • 11 replies
  • 109 views
  • 0 likes
  • 5 in conversation