BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sfinch
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

6 REPLIES 6
Reeza
Super User

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

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

ballardw
Super User

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

sfinch
Calcite | Level 5

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? 

 

ballardw
Super User

@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.

Tom
Super User Tom
Super User

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

 

sfinch
Calcite | Level 5

Thank you most kindly!

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
  • 6 replies
  • 1675 views
  • 1 like
  • 4 in conversation