DATA Step, Macro, Functions and more

help me to understand _file_

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,636
Accepted Solution

help me to understand _file_

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;


Accepted Solutions
Solution
‎04-09-2012 01:12 PM
Valued Guide
Posts: 765

Re: help me to understand _file_

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

View solution in original post


All Replies
PROC Star
Posts: 7,364

Re: help me to understand _file_

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.

Super Contributor
Posts: 1,636

Re: help me to understand _file_

Thank you Art! I will read the paper. - Linlin

Valued Guide
Posts: 765

Re: help me to understand _file_

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

Valued Guide
Posts: 2,175

Re: help me to understand _file_

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 )

Super Contributor
Posts: 1,636

Re: help me to understand _file_

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

Valued Guide
Posts: 2,175

Re: help me to understand _file_

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

Solution
‎04-09-2012 01:12 PM
Valued Guide
Posts: 765

Re: help me to understand _file_

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

Super Contributor
Posts: 1,636

Re: help me to understand _file_

Thank you Mike and Peter! I need to read more papers to help me to understand how _file_, _infile_ work.  - Linlin


Super Contributor
Posts: 1,636

Re: help me to understand _file_

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 spentSmiley Happy).

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

Valued Guide
Posts: 765

Re: help me to understand _file_

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

Super Contributor
Posts: 1,636

Re: help me to understand _file_

Thank you Mike for the information! - Linlin

PROC Star
Posts: 7,364

Re: help me to understand _file_

☑ This topic is solved.

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

Discussion stats
  • 12 replies
  • 434 views
  • 7 likes
  • 4 in conversation