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.
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
%let string=1 2 3 4 ;
data test;
input @@;
_infile_ = resolve(_infile_);
input var @@;
cards;
&string
run;
Thank you for your reply; unfortunately, the code didn't work. The following is the log:
1 | %let string=1 2 3 4 ; |
2
3 | data test; |
4
5 | input @@; |
6
7 | _infile_ = resolve(_infile_); |
8
9 | 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 |
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
Can you write your full code? I couldn't reproduce your result. Thanks.
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?
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.
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.
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.
I copied this code exactly and it didn't work.
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!
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.