BookmarkSubscribeRSS Feed
arjunascagnetto
Fluorite | Level 6

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;

11 REPLIES 11
andreas_lds
Jade | Level 19

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.

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

PaigeMiller
Diamond | Level 26

@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
arjunascagnetto
Fluorite | Level 6

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.

PaigeMiller
Diamond | Level 26

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
arjunascagnetto
Fluorite | Level 6
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;
PaigeMiller
Diamond | Level 26

@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
arjunascagnetto
Fluorite | Level 6
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;
Astounding
PROC Star

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.

arjunascagnetto
Fluorite | Level 6
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 1118 views
  • 0 likes
  • 5 in conversation