DATA Step, Macro, Functions and more

%let VS call symput(s)

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

%let VS call symput(s)

Hello , Good morning ,

 

I want the opinion of a specialist because in my case it's a true mystery to declare the variables. I want obtain a variable which will give like value :

jour jour

 

The SAS code :

 

 

%let jour = "jour jour";
%let jour2 = 'jour jour';
%let jour3 = 'jour';
%let jour4 = "jour";
%let jour5 = "jour jour"n;
%let jour6 = 'jour jour'n;
%let jour7 = 'jour'n;
%let jour8 = "jour"n;
 
%put &jour &jour2 &jour3 &jour4 &jour5 &jour6 &jour7 &jour8 ;
 
/* VS */
 
data _null_;
 
call symputx('jour10','jour');
call symputx('jour11','jour jour');
call symputx('jour12',"jour");
call symputx('jour13',"jour jour");
 
run;
 
%put &jour10 &jour11 &jour12 &jour13

 

The SAS log :

 

 

NOTE: Writing TAGSETS.SASREPORT13(EGSR) Body file: EGSR
22         
23         GOPTIONS ACCESSIBLE;
24         %let jour = "jour jour";
25         %let jour2 = 'jour jour';
26         %let jour3 = 'jour';
27         %let jour4 = "jour";
28         %let jour5 = "jour jour"n;
29         %let jour6 = 'jour jour'n;
30         %let jour7 = 'jour'n;
31         %let jour8 = "jour"n;
32         
33         %put &jour &jour2 &jour3 &jour4 &jour5 &jour6 &jour7 &jour8 ;
SYMBOLGEN:  Macro variable JOUR resolves to "jour jour"
SYMBOLGEN:  Macro variable JOUR2 resolves to 'jour jour'
SYMBOLGEN:  Macro variable JOUR3 resolves to 'jour'
SYMBOLGEN:  Macro variable JOUR4 resolves to "jour"
SYMBOLGEN:  Macro variable JOUR5 resolves to "jour jour"n
SYMBOLGEN:  Macro variable JOUR6 resolves to 'jour jour'n
SYMBOLGEN:  Macro variable JOUR7 resolves to 'jour'n
SYMBOLGEN:  Macro variable JOUR8 resolves to "jour"n
"jour jour" 'jour jour' 'jour' "jour" "jour jour"n 'jour jour'n 'jour'n "jour"n
34         
35         data _null_;
36         
37         call symputx('jour10','jour');
38         call symputx('jour11','jour jour');
39         call symputx('jour12',"jour");
40         call symputx('jour13',"jour jour");
41         
42         run;
 
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
 
2                                                          The SAS System                            10:51 Monday, November 27, 2017
 
 
43         
44         %put &jour10 &jour11 &jour12 &jour13;
SYMBOLGEN:  Macro variable JOUR10 resolves to jour
SYMBOLGEN:  Macro variable JOUR11 resolves to jour jour
SYMBOLGEN:  Macro variable JOUR12 resolves to jour
SYMBOLGEN:  Macro variable JOUR13 resolves to jour jour
jour jour jour jour jour jour
45         
46         GOPTIONS NOACCESSIBLE;
47         %LET _CLIENTTASKLABEL=;
48         %LET _CLIENTPROJECTPATH=;
49         %LET _CLIENTPROJECTNAME=;
50         %LET _SASPROGRAMFILE=;
51         
52         ;*';*";*/;quit;run;
53         ODS _ALL_ CLOSE;
54         
55         
56         QUIT; RUN;
57

If I use %let :

 

"jour jour"

 

or

'jour jour'

 

with the call symput(s) :

jour jour

 

It's the "**bleep**" when we use quotes in the %let , for example when the value must be contain spaces ! Smiley Mad

 

In the present case it's very dangerous , it's "better" to use call symput(s) .

 

You own a solution to around this problem ?

 

 


Accepted Solutions
Solution
‎11-28-2017 04:46 AM
Super User
Posts: 6,526

Re: %let VS call symput(s)

Posted in reply to azertyuiop

You are running into trouble, because you are trying to use macro language without learning some of the very basic principles involved.  Macro language and SAS language are different ... different in purpose, different in how to assign values to variables, different in how to refer to variables.  You will need to understand those differences.  Here are just some brief examples.

 

Your original post failed to include this possibility:

 

%let jour9 = jour jour;

 

What is that possibility omitted?

 

Given your DATA step with YEAR1, YEAR2, and YEAR3, the SAS statements become:

 

year1 = '1939';

 

That creates a character variable named YEAR1.

 

year2 = '&year';

 

That creates a character variable named YEAR2, where the first character in its value is "&".

 

year3 = "'1939'";

 

That creates a character variable named YEAR3, where the first character in its value is a single quote.

 

But your real need is threefold.  First, experiment with many examples of %LET statements and see what they do.  It's easy enough to code your own tests:

 

%let year = '1939';

%put *&year*;

 

Second, experiment with many examples of CALL SYMPUT and see what they do.  Again, easy enough:

 

data _null_;

call symputx('year', '1939');

run;

%put *&year*;

 

Finally, appreciate that macro language and SAS language are separate, and do different things.  SAS language processes your data.  Macro language constructs a program.  This is a much more involved topic, and requires finding a few good references to study.  Good luck in your studies.

View solution in original post


All Replies
Super User
Super User
Posts: 9,193

Re: %let VS call symput(s)

Posted in reply to azertyuiop

I think the problem, as with your other questions on the subject, is your refusal to use Base SAS for any task. Why do you need 13 macro variables, some using named literals, with quotes?  This is not in any sense a good starting point. Your question:

I want obtain a variable which will give like value :

jour jour

 Makes no sense as:

%let myvar=jour jour;

Is the answer, so what is the other 12 varaibles for, why are some symputted and others let? 

The simple question is Why is any of this code?

Contributor
Posts: 68

Re: %let VS call symput(s)

@RW9  The other topic is an other question . We can this topic later .

 

Here my question in only on %let VS call symput(s) .

 

In the present case I test the fonction %let but this a problem with this solution.

 

I want give to SAS a value , but when SAS keep this value in memory an kink appear :

 

 

%let myvar="jour jour";

or

 

 

 

%let myvar='jour jour';

 

 

I look for the bonus character to unread / no print the double or sample quote and consider only work :

 

jour jour

 

I have tested this code :

 

%let jour ='jour jour';

data _null_;

call symputx('jour2','jour jour');
call symputx('result','true');

if "&jour"="&jour2" then do;

%put &result;

end;

run; 

SAS log :

23         GOPTIONS ACCESSIBLE;
24         %let jour ='jour jour';
25         
26         data _null_;
27         
28         call symputx('jour2','jour jour jour');
29         call symputx('result','true');
30         
SYMBOLGEN:  Macro variable JOUR resolves to 'jour jour'
31         if "&jour"="&jour2" then do;
SYMBOLGEN:  Macro variable JOUR2 resolves to jour jour

32         
33         %put &result;
SYMBOLGEN:  Macro variable RESULT resolves to true
true
34         
35         end;
36         
37         run;

 

Now with a little difference  :

 

 

%let jour ='jour jour';

data _null_;

call symputx('jour2','jour jour jour');
call symputx('result','true');

if "&jour"="&jour2" then do;

%put &result;

end;

run; 

 

SAS log : 

 

23         GOPTIONS ACCESSIBLE;
24         %let jour ='jour jour';
25         
26         data _null_;
27         
28         call symputx('jour2','jour jour jour');
29         call symputx('result','true');
30         
SYMBOLGEN:  Macro variable JOUR resolves to 'jour jour'
31         if "&jour"="&jour2" then do;
SYMBOLGEN:  Macro variable JOUR2 resolves to jour jour jour

32         
33         %put &result;
SYMBOLGEN:  Macro variable RESULT resolves to true
true
34         
35         end;
36         
37         run;

 

@KurtBremser

 

You see the kink in SAS log ?

 

 

 

 

 

 

 

Super User
Posts: 9,549

Re: %let VS call symput(s)

Posted in reply to azertyuiop

You very obviously have NOT tested your code, as this is the log:

24         %let jour ='jour jour';
25         
26         data _null_;
27         
28         call symputx('jour2','jour jour');
29         call symputx('result','true');
30         
31         if "&jour"="&jour2" then do;
WARNING: Apparent symbolic reference JOUR2 not resolved.
32         
33         %put &result;
WARNING: Apparent symbolic reference RESULT not resolved.
&result
34         
35         end;
36         
37         run;

I repeat for at least the third time:

YOU CANNOT USE A MACRO VARIABLE WITH &varname WHEN YOU SET IT IN THE SAME STEP WITH CALL SYMPUT!!!

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 68

Re: %let VS call symput(s)

Posted in reply to KurtBremser

@KurtBremser

YOU CANNOT USE A MACRO VARIABLE WITH &varname WHEN YOU SET IT IN THE SAME STEP WITH CALL SYMPUT!!!

 

I note this information.

 

So , now we suppose a separate %let and call symput(x) without link :

 

/* test1 */

%let jour ='jour jour';

%put &jour;

data _null_;

/* test2 */

call symputx('jour2','jour jour');

%put &jour2;

run; 

You see the difference ?

 

24         /* test1 */
25         
26         %let jour ='jour jour';
27         
28         %put &jour;
SYMBOLGEN:  Macro variable JOUR resolves to 'jour jour'
'jour jour'
29         
30         data _null_;
31         
32         /* test2 */
33         
34         call symputx('jour2','jour jour');
35         
36         %put &jour2;
SYMBOLGEN:  Macro variable JOUR2 resolves to jour jour
jour jour
37         
38         run;

 

I look for a bonus character to suppress the quote if I use %let ...

Super User
Posts: 9,549

Re: %let VS call symput(s)

Posted in reply to azertyuiop

Stilll cannot work. It only "works" with you because you do not test it on a fresh SAS session, so the remnants of your earlier mistakes hide your current ones:

24         /* test1 */
25         
26         %let jour ='jour jour';
27         
28         %put &jour;
'jour jour'
29         
30         data _null_;
31         
32         /* test2 */
33         
34         call symputx('jour2','jour jour');
35         
36         %put &jour2;
WARNING: Apparent symbolic reference JOUR2 not resolved.
&jour2
37         
38         run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 68

Re: %let VS call symput(s)

Posted in reply to KurtBremser

@KurtBremser

 

I have tested the code in a new session I confirm the problem :

run 1 :

23         /* test1 */
24         
25         %let jour ='jour jour';
26         
27         %put &jour;
SYMBOLGEN:  Macro variable JOUR resolves to 'jour jour'
'jour jour'
28         
29         data _null_;
30         
31         /* test2 */
32         
33         call symputx('jour2','jour jour');
34         
35         %put &jour2;
WARNING: Apparent symbolic reference JOUR2 not resolved.
&jour2
36         
37         run;

run 2 :


23         /* test1 */
24         
25         %let jour ='jour jour';
26         
27         %put &jour;
SYMBOLGEN:  Macro variable JOUR resolves to 'jour jour'
'jour jour'
28         
29         data _null_;
30         
31         /* test2 */
32         
33         call symputx('jour2','jour jour');
34         
35         %put &jour2;
SYMBOLGEN:  Macro variable JOUR2 resolves to jour jour
jour jour
36         
37         run;

 

It's only in the second run that the value is recognised ...

 

 

 

Super User
Posts: 9,549

Re: %let VS call symput(s)

[ Edited ]
Posted in reply to azertyuiop

What for?

90% (or better) probability that you're abusing the macro facility for something it's not meant for.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 68

Re: %let VS call symput(s)

Posted in reply to KurtBremser

Can see the problem here  : https://support.sas.com/rnd/papers/sgf07/Macros_Dont_Work.pdf , page 5

 

What if quotes are
part of the value?
%let year='1939';
Data awards;
year1=&year;
year2='&year';
year3="&year";
Run;
What are the values and types (char or num) of each
variable?
Solution
‎11-28-2017 04:46 AM
Super User
Posts: 6,526

Re: %let VS call symput(s)

Posted in reply to azertyuiop

You are running into trouble, because you are trying to use macro language without learning some of the very basic principles involved.  Macro language and SAS language are different ... different in purpose, different in how to assign values to variables, different in how to refer to variables.  You will need to understand those differences.  Here are just some brief examples.

 

Your original post failed to include this possibility:

 

%let jour9 = jour jour;

 

What is that possibility omitted?

 

Given your DATA step with YEAR1, YEAR2, and YEAR3, the SAS statements become:

 

year1 = '1939';

 

That creates a character variable named YEAR1.

 

year2 = '&year';

 

That creates a character variable named YEAR2, where the first character in its value is "&".

 

year3 = "'1939'";

 

That creates a character variable named YEAR3, where the first character in its value is a single quote.

 

But your real need is threefold.  First, experiment with many examples of %LET statements and see what they do.  It's easy enough to code your own tests:

 

%let year = '1939';

%put *&year*;

 

Second, experiment with many examples of CALL SYMPUT and see what they do.  Again, easy enough:

 

data _null_;

call symputx('year', '1939');

run;

%put *&year*;

 

Finally, appreciate that macro language and SAS language are separate, and do different things.  SAS language processes your data.  Macro language constructs a program.  This is a much more involved topic, and requires finding a few good references to study.  Good luck in your studies.

Contributor
Posts: 68

Re: %let VS call symput(s)

Posted in reply to Astounding

Hello , Good morning ,

After a great number of test , I obtain a concluding result to past the variable which come from %let :

With this code :

%let lien = '\\serveur\sous - dossier\dossiertoto\' ;

Data _null_

fic1=&lien; /* give > \\serveur\sous - dossier\dossiertoto\ */ /* case to obtain a number if %let is a numeric */
fic2='&lien'; /* give > &lien */
fic3="&lien"; /* give > \\serveur\sous - dossier\dossiertoto\ */ /* case to obtain a string if %let is a numeric */

Run;



Here you can see page 5 of this pdf : https://support.sas.com/rnd/papers/sgf07/Macros_Dont_Work.pdf

year1=1939; > num, 1939
year2='&year'; > char, &year
year3="1939"; > char, 1939


I note all informations that you have gave.

Error are corrected.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 249 views
  • 0 likes
  • 4 in conversation