BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Linlin
Lapis Lazuli | Level 10

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;

1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

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

12 REPLIES 12
art297
Opal | Level 21

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.

Linlin
Lapis Lazuli | Level 10

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

MikeZdeb
Rhodochrosite | Level 12

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

Peter_C
Rhodochrosite | Level 12

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 )

Linlin
Lapis Lazuli | Level 10

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

Peter_C
Rhodochrosite | Level 12

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

MikeZdeb
Rhodochrosite | Level 12

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
Lapis Lazuli | Level 10

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


Linlin
Lapis Lazuli | Level 10

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

MikeZdeb
Rhodochrosite | Level 12

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

Linlin
Lapis Lazuli | Level 10

Thank you Mike for the information! - Linlin

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 12 replies
  • 1527 views
  • 7 likes
  • 4 in conversation