BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
abcd123
Fluorite | Level 6

Hello, All

I have following codes:

%let string=%str(cards; 1 2 3 4);

data test;

        input var @@;

        &string

    ;

proc print data=test;

run;

I am expecting it will work as:

data test;

input var @@;

cards;

1 2 3 4

;

However, it doesn't work. How should I re-write the code to make it work? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Seems to me the macro variable STRING was not defined. In that case the RESOLVE function will leave the card's content unchanged as that is what SAS macro processor does when it sees what looks like macro variable reference, but the variable being referenced dose not exist.

3149  %let string=1 5 7 3 4 ;

3150  data test;

3151    input @@;

3152    _infile_ = resolve(_infile_);

3153    input var @@;

3154    put _n_= var= ;

3155  cards;

_N_=1 var=1

_N_=2 var=5

_N_=3 var=7

_N_=4 var=3

_N_=5 var=4

View solution in original post

11 REPLIES 11
Tom
Super User Tom
Super User

%let string=1 2 3 4 ;

data test;

  input @@;

  _infile_ = resolve(_infile_);

  input var @@;

cards;

&string

run;

abcd123
Fluorite | Level 6

Thank you for your reply; unfortunately, the code didn't work. The following is the log:

1%let string=1 2 3 4 ;

2

3data test;

4

input @@;

6

_infile_ = resolve(_infile_);

8

input var @@;

10

11   cards;

NOTE: Invalid data for var in line 13 1-7.

RULE:  ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+--
13     &string

var=. _ERROR_=1 _INFILE_=&string _N_=1

NOTE: SAS went to a new line when INPUT statement reached past the end of a line.

NOTE: The data set WORK.TEST has 1 observations and 1 variables.

NOTE: DATA statement used (Total process time):

  real time       0.25 seconds
  cpu time        0.03 seconds
Tom
Super User Tom
Super User

Seems to me the macro variable STRING was not defined. In that case the RESOLVE function will leave the card's content unchanged as that is what SAS macro processor does when it sees what looks like macro variable reference, but the variable being referenced dose not exist.

3149  %let string=1 5 7 3 4 ;

3150  data test;

3151    input @@;

3152    _infile_ = resolve(_infile_);

3153    input var @@;

3154    put _n_= var= ;

3155  cards;

_N_=1 var=1

_N_=2 var=5

_N_=3 var=7

_N_=4 var=3

_N_=5 var=4

abcd123
Fluorite | Level 6

Can you write your full code? I couldn't reproduce your result. Thanks.

Astounding
PROC Star

Here's a variation that I got to work ... but there are two changes.  First, the CARDS statement has to appear separately, outside of the value for &STRING.  And the blank line after CARDS is important.

%let string=%str(1 2 3 4);

data test;

input @@;

_infile_ = "&string";

input var @@;

cards;

;

Is there any reason you actually need to do this, or are you just poking and prodding to see what SAS will do?

abcd123
Fluorite | Level 6

To Astounding: the reason I am doing this related to my work, where I have a very long single line of characters that I have to separate and find out some values based on specific conditions.

Thanks again.

Astounding
PROC Star

abcd123,

You're correct in that run; is not needed after a CARDS statement.  The very next semicolon tells SAS that the data lines are complete and that SAS statements are resuming.  And since CARDS can only appear at the end of the DATA step, that semicolon also tells SAS that the DATA step should run.

A more commonplace approach would be to store your long string in a separate file, and use an INFILE statement.  But use whatever is easy ... good luck.

abcd123
Fluorite | Level 6

Thank you for the recommendation. However due to some circumstance, I hope I can use this method instead of creating a new separate file.

Actually I just started a new thread which is also related to this work. Anyone who has time and could take a look, I will greatly appreciate it.

modsoul1
Calcite | Level 5

I copied this code exactly and it didn't work.  

boyangbian
Calcite | Level 5

Hi 

do you mind to help me with a similar issue?

instead of the original case, I want a series of text string like the following &key_words,


%let key_words=
'SELR BIPLR AQUA
BIPLR SELR AQUA
Aquamantys
Aquamantis
sealr aqua
biplr aqua';

data test;
input ;
_infile_ = resolve(_infile_);
input t_name $20. ;
cards;
&key_words
run;

 

Much appreciated!

abcd123
Fluorite | Level 6

Thanks for all your help.

It seems that I should not add     run;     in the code; after I remove this statement, the code works.

Again thank you very much.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6309 views
  • 3 likes
  • 5 in conversation