Help using Base SAS procedures

macro variable Substitution in Cards (or Datalines statement)

Accepted Solution Solved
Reply
Contributor
Posts: 51
Accepted Solution

macro variable Substitution in Cards (or Datalines statement)

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.


Accepted Solutions
Solution
‎07-24-2013 09:57 AM
Super User
Super User
Posts: 6,502

Re: macro variable Substitution in Cards (or Datalines statement)

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


All Replies
Super User
Super User
Posts: 6,502

Re: macro variable Substitution in Cards (or Datalines statement)

%let string=1 2 3 4 ;

data test;

  input @@;

  _infile_ = resolve(_infile_);

  input var @@;

cards;

&string

run;

Contributor
Posts: 51

Re: macro variable Substitution in Cards (or Datalines statement)

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
Solution
‎07-24-2013 09:57 AM
Super User
Super User
Posts: 6,502

Re: macro variable Substitution in Cards (or Datalines statement)

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

Contributor
Posts: 51

Re: macro variable Substitution in Cards (or Datalines statement)

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

Super User
Posts: 5,092

Re: macro variable Substitution in Cards (or Datalines statement)

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?

Contributor
Posts: 51

Re: macro variable Substitution in Cards (or Datalines statement)

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.

Super User
Posts: 5,092

Re: macro variable Substitution in Cards (or Datalines statement)

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.

Contributor
Posts: 51

Re: macro variable Substitution in Cards (or Datalines statement)

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.

New Contributor
Posts: 2

Re: macro variable Substitution in Cards (or Datalines statement)

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

Learner
Posts: 1

Re: macro variable Substitution in Cards (or Datalines statement)

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!

Contributor
Posts: 51

Re: macro variable Substitution in Cards (or Datalines statement)

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.

☑ This topic is solved.

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

Discussion stats
  • 11 replies
  • 2649 views
  • 3 likes
  • 5 in conversation