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
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
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;
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 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.
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.
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
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
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.
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!
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.