BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shiv999
Calcite | Level 5
%let fdate = 09272018
%put &fdate.;
Data scores;
Input name $20.;
Datalines;
Shiv_&fdate.
Chara_&fdate.
;

I want the date to be populated at end of the name
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

To resolve macro triggers in data (which is usually a very bad idea, anyway), use the resolve() function:

%let fdate=20180927;

data want;
infile datalines;
input name $30.;
name = resolve(name);
datalines;
Shiva_&fdate._test
Kumar_test_&fdate.you
Faha_&fdate._you
Hsjsb_&fdate._hjj
;
run;

proc print data=want noobs;
run;

Result:

         name

Shiva_20180927_test   
Kumar_test_20180927you
Faha_20180927_you     
Hsjsb_20180927_hjj    

View solution in original post

11 REPLIES 11
yabwon
Onyx | Level 15

Hi,

 

I think there is ";" missing in the %let statement.

 

And about adding the date, I would offer:

 

%let fdate = 09272018;
%put &fdate.;
Data scores;
Input name $20.;
name = catx("_", name, "&fdate.");
Datalines;
Shiv
Chara
;
run;

 

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



RW9
Diamond | Level 26 RW9
Diamond | Level 26

As @yabwon has mentioned you are missing a semi-colon.  Also you will need to follow:

http://support.sas.com/kb/43/295.html

 

To get your macro variable resolved as natively macro does not resolve in datalines.

 

Which leads onto my question of why on earth woulf you want to do this as this is effectively the same, simpler:

%let fdate = 09272018;
data scores;
  input name $20.;
  name=cats(name,"_&fdate.");
datalines;
Shiv
Chara
;
run;
Kurt_Bremser
Super User

Maxim 2: read the log.

%let fdate = 09272018
%put &fdate.;

Log:

ERROR: Open code statement recursion detected.
27         %let fdate = 09272018
28         %put &fdate.;

Since the error happens right at the start, everything else is insignificant. Fix code from the top down.

 

Second, macro triggers are NEVER resolved in datalines.

 

Third, dates like that should be ordered year-month-day, and not month-day-year or day-month-year, as you can't sort them properly in further processing.

 

For the concatenating, @yabwon has given you the answer.

shiv999
Calcite | Level 5
Thanks Rw9 , I was not using data lines but using infiles . What would be the solution for infiles
Kurt_Bremser
Super User

@shiv999 wrote:
Thanks Rw9 , I was not using data lines but using infiles . What would be the solution for infiles

Remove the datalines block and add the appropriate infile statement. The rest of the code stays as-is.

 

Having a datalines block lets the data step compiler implicitly add a

infile datalines;

statement, if no explicit infile statement is found.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Do you mean then you are reading from a filename which has that information in, if so:

data want;
  infile "c:\tmp\shiv_&fdate..csv";
...
run;

If not please show examples of what you mean.

shiv999
Calcite | Level 5
Let me elobrate my issue .
In my infile data is like
In below form . I used translate and tranwrd functions . So here I want repalce this &fdate. With 09222018

Shiva_&fdate._test
Kumar_test_&fdate.you
Faha_&fdate_.you
Hsjsb_&fdate._hjj
Kurt_Bremser
Super User

To resolve macro triggers in data (which is usually a very bad idea, anyway), use the resolve() function:

%let fdate=20180927;

data want;
infile datalines;
input name $30.;
name = resolve(name);
datalines;
Shiva_&fdate._test
Kumar_test_&fdate.you
Faha_&fdate._you
Hsjsb_&fdate._hjj
;
run;

proc print data=want noobs;
run;

Result:

         name

Shiva_20180927_test   
Kumar_test_20180927you
Faha_20180927_you     
Hsjsb_20180927_hjj    
yabwon
Onyx | Level 15

HI,

 

if you know that your date should be: 09222018

 

and you have external text file (let say: C:\my_file.txt) 

with contents:

Shiva_&fdate._test
Kumar_test_&fdate.you
Faha_&fdate._.you
Hsjsb_&fdate._hjj

 

and you want to readin that file into dataset and resolve macrovariables, you should do:

 

%let fdate = 09272018;
%put &fdate.;
Data scores;
infile "C:\my_file.txt";
Input name : $20.;
name = resolve(name);
run;

all the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



RW9
Diamond | Level 26 RW9
Diamond | Level 26

The question here really then is why do you have macro (which is SAS only) in a plain text file where there is no link to SAS, nor method of processing such a thing.  I am afraid you have a bad process there.

shiv999
Calcite | Level 5
Yes bad idea buts it's my infile . Yup great this worked thanks alot

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2017 views
  • 3 likes
  • 4 in conversation