When I run the code below, macro variable &rename=
ABC=ID F1=Name F2=Sex FDG=Country ABC=id1 F1=name1 F2=sex1 FDG=country1
I expect &rename= ABC=id1 F1=name1 F2=sex1 FDG=country1
I don't understand Why _file_ has the values of two observations instead of just second observation?
Thanks - Linlin
data have;
input (ABC F1 F2 FDG)($);
cards;
ID Name Sex Country
id1 name1 sex1 country1
1 ABC M IND
2 BCD F USA
3 CDE M GER
4 DGE M UK
;
%let rename=;
filename nosee dummy;
data _null_;
file nosee;
set have (obs=2);
put (_all_) (=) @;
call symputx('rename',_file_);
run;
%put &rename;
hi (again) ... if you look near the end of the discussion at ...
https://communities.sas.com/message/107661#107661
there's another example that uses _FILE_ and you'll see that there's a second PUT statement that clears the results of the previous PUT statement that ended with the @ sign
Linlin: take a look at: http://support.sas.com/rnd/base/datastep/dsv7-sugi.pdf
Your put statement ends with an @ symbol, thus the buffer will not be cleared at the end of the iteration.
Thank you Art! I will read the paper. - Linlin
hi ... you are better off looking at Peter's answer in that you need that @ sign to keep the results in the buffer
the problem is (as Peter stated) the meaning of OBS ... you asked for two observations, not 1
Linlin
I think you misunderstand OBS=
Did you intend to use only the second obs?
Then add data set option FIRSTOBS=2 as well as OBS=2
Art,
if that @ is removed, the _file_ buffer will be empty at the call symputx() statement.
(of course, a "clearing" PUT; statement with no @ could be added after the symputx(), but I think the issue is about the SET )
Hi Peter,
Thank you for your reply! I know how OBS works. What I don't undestand is why _file_ retains the values of first observation.
Linlin
Linlin
(Since you understand what OBS= is for, I don't understand why you didn't also use FIRSTOBS=
as the macro variable will hold only the results from the final iteration of the data step)
Has Art's response clarified why the final iteration of your data step puts both sets of observations into &RENAME? (focus on that @ )
Peter
hi (again) ... if you look near the end of the discussion at ...
https://communities.sas.com/message/107661#107661
there's another example that uses _FILE_ and you'll see that there's a second PUT statement that clears the results of the previous PUT statement that ended with the @ sign
Thank you Mike and Peter! I need to read more papers to help me to understand how _file_, _infile_ work. - Linlin
Thank you for your input. I am not sure I understand put with a trailing@, put without a trailing@, _file_, _infile_ 100%, but at least I know how to use them (I consider my time well spent).
To use the second observation as variable names in my example, I need to add another put statement to my original code as Mike pointed out.
data have;
input (ABC F1 F2 FDG)($);
cards;
ID Name Sex Country
id1 name1 sex1 country1
1 ABC M IND
2 BCD F USA
3 CDE M GER
4 DGE M UK
;
%let rename=;
filename nosee dummy;
data _null_;
file nosee;
set have (obs=2);
put (_all_) (=)@ ;
call symputx('rename',_file_);
put;
run;
%put &rename;
Of course the code below also works.
filename nosee dummy;
data _null_;
file nosee;
set have (firstobs=2 obs=2);
put (_all_) (=)@ ;
call symputx('rename',_file_);
run;
%put &rename;
Linlin
hi ... more reading material about _INFILE_ ...
Now _INFILE_ is an Automatic Variable − So What?
Howard Schreier
http://www.nesug.org/proceedings/nesug01/cc/cc4018bw.pdf
More _Infile_ Magic
Peter Crawford
http://www2.sas.com/proceedings/sugi28/086-28.pdf
I haven't seen any equivalent papers on _FILE_, only postings on SAS-L and here on COMMUNITIES
Thank you Mike for the information! - Linlin
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.