DATA Step, Macro, Functions and more

SUBSTR, CALL SYMPUT, and years 1999 & 2000

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

SUBSTR, CALL SYMPUT, and years 1999 & 2000

This is surely an FAQ.  I'm passing a year (1999 or 2000) into a SAS program.  Everything works fine for 1999:


1    %PUT &yyyy;
1999
2    %LET yy=%SUBSTR(&yyyy,3);             /* last two digits of yyyy */
3    %PUT &yy;
99
4   
5    DATA _null_;
6       CALL SYMPUT('fpath', cat("/wrds/taq.",&yyyy,"/taq",&yy,"d/sasdata"));
7    RUN;   
8    %PUT &fpath;
/wrds/taq.1999/taq99d/sasdata


but fails for 2000:


1    %PUT &yyyy;
2000
2    %LET yy=%SUBSTR(&yyyy,3);             /* last two digits of yyyy */
3    %PUT &yy;
00
4   
5    DATA _null_;
6       CALL SYMPUT('fpath', cat("/wrds/taq.",&yyyy,"/taq",&yy,"d/sasdata"));
7    RUN;
8    %PUT &fpath;
/wrds/taq.2000/taq0d/sasdata


I need the final character string to be "/wrds/taq.2000/taq00d/sasdata" (two zeros before d, not one!)  For some reason, &yy doesn't give me 00 when it appears within the CALL SYMPUT argument list.

 

How, please, can I fix this problem?  Thank you!


Accepted Solutions
Solution
‎10-13-2017 12:42 PM
Super User
Posts: 13,046

Re: SUBSTR, CALL SYMPUT, and years 1999 & 2000

Besides making it way to too complicate the offending element is in this line:

CALL SYMPUT('fpath', cat("/wrds/taq.",&yyyy,"/taq",&yy,"d/sasdata"));

 

Since you do not have &yy in quotes it is treated as a numeric value and the CAT function uses the BEST format to turn the numeric into a text value to concatenate. So is equivalent to ,put(&yy, best8.) which generates a 0.

Try:

 

%let Fpath = /wrds/taq&yyyy,/taq%substr(&yyyy,3,2)d/sasdata;

 

The macro concatenation works just fine no data step, no call symput and no data step functions needed

View solution in original post


All Replies
Super User
Posts: 22,850

Re: SUBSTR, CALL SYMPUT, and years 1999 & 2000

CAT automatically converts numbers using the BEST format. 99 stays 99, but 00 becomes 0. 

Use an explicit format instead, ie PUT(..., Z2.)

Solution
‎10-13-2017 12:42 PM
Super User
Posts: 13,046

Re: SUBSTR, CALL SYMPUT, and years 1999 & 2000

Besides making it way to too complicate the offending element is in this line:

CALL SYMPUT('fpath', cat("/wrds/taq.",&yyyy,"/taq",&yy,"d/sasdata"));

 

Since you do not have &yy in quotes it is treated as a numeric value and the CAT function uses the BEST format to turn the numeric into a text value to concatenate. So is equivalent to ,put(&yy, best8.) which generates a 0.

Try:

 

%let Fpath = /wrds/taq&yyyy,/taq%substr(&yyyy,3,2)d/sasdata;

 

The macro concatenation works just fine no data step, no call symput and no data step functions needed

Occasional Contributor
Posts: 15

Re: SUBSTR, CALL SYMPUT, and years 1999 & 2000

Your command

 

%LET fpath = /wrds/taq.&yyyy/taq%SUBSTR(&yyyy,3,2)d/sasdata;

 

works perfectly: thank you!  It certainly is more concise as well.

 

Looking back at my code, why does SAS treat &yyyy differently than &yy? 

 

While PUT(&yy,Z2.) was necessary to format &yy correctly, &yyyy required no additional intervention.  This asymmetry is puzzling to me.  Is an explanation possible? 

 

Super User
Posts: 13,046

Re: SUBSTR, CALL SYMPUT, and years 1999 & 2000


sfinch wrote:

Your command

 

%LET fpath = /wrds/taq.&yyyy/taq%SUBSTR(&yyyy,3,2)d/sasdata;

 

works perfectly: thank you!  It certainly is more concise as well.

 

Looking back at my code, why does SAS treat &yyyy differently than &yy? 

 

While PUT(&yy,Z2.) was necessary to format &yy correctly, &yyyy required no additional intervention.  This asymmetry is puzzling to me.  Is an explanation possible? 

 


Try it with YYYY=0000 and it will do the same thing. Since your values for YYYY were likely to be in 19xx or 20xx or such range then the first significant digit appears in the first of 4 digits.

Or try YY = 0015, you'll get 15 in the output for the &yyyy for the same reason: conversion using best. format.

Super User
Super User
Posts: 7,857

Re: SUBSTR, CALL SYMPUT, and years 1999 & 2000

[ Edited ]

Pass &YY to the CAT() function as a string and not a number.  So use "&YY" instead of just &YY.

DATA _null_;
  CALL SYMPUT('fpath', cat("/wrds/taq.","&yyyy","/taq","&yy","d/sasdata"));
RUN;   

Note that there is no real need to use CAT() function for macro variables.

%let fpath =/wrds/taq.&yyyy./taq&yy.d/sasdata ;

To see what CAT() is doing with the number try a little experiment yourself.

17   data _null_;
18     do num=9 to 10;
19       str=put(num,z2.);
20       length catnum catstr $100;
21       catnum=cats('before',num,'after');
22       catstr=cats('before',str,'after');
23       put (_all_) (=/);
24     end;
25   run;


num=9
str=09
catnum=before9after
catstr=before09after

num=10
str=10
catnum=before10after
catstr=before10after

 

Occasional Contributor
Posts: 15

Re: SUBSTR, CALL SYMPUT, and years 1999 & 2000

Thank you most kindly!

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 165 views
  • 1 like
  • 4 in conversation