I have a macro varible i.e
%let x =a|b|c|d|e|f ;
I want to creat a dataset
X
a
b
c
d
e
I tried by using datalines, but getting error,
can u please help me on this.
Thanks
I don't think macro variables work below cards/datalines. Try this:
%let x =a|b|c|d|e|f ;
data want;
_temp=symget('x');
do _i=1 to countw(_temp,'|');
x=scan(_temp,_i,'|');
output;
end;
drop _:;
run;
I don't think macro variables work below cards/datalines. Try this:
%let x =a|b|c|d|e|f ;
data want;
_temp=symget('x');
do _i=1 to countw(_temp,'|');
x=scan(_temp,_i,'|');
output;
end;
drop _:;
run;
A good approach, but consider a few items.
The number of values will be 1 + countw(...)
You don't really need SYMGET. You could just refer to "&x" instead of _temp.
The length of the variable X will be longer than you might expect ... perhaps better to set that length with a LENGTH statement early in the DATA step.
The advantage of SYMGET is you don't have to worry about what quote characters might exist in the value of the macro variable or the setting of QUOTELENMAX.
Yes agree with you that i need to +1 , But using macro variable directly is not working,[may be i m missing something]
I have mentioned below both example:
%let dt= 1|2|3|4|5;
data test2;
temp=symget('dt');
do i = 1 to count(temp,'|')+1;
x= scan(temp,i,"|");
output;
end;
run;
proc print; run;
data test2;
do i = 1 to count(&dt,'|')+1;
x= scan(&dt,i,"|");
output;
end;
run;
proc print; run;
In the final DATA step, you have to add the double quotes in two places: "&dt" not &dt
Also recommend adding a PROC CONTENTS so you can see the length assigned to X.
There is really no need to count anything but you may want to account for delimiters contained in the words. And make sure to properly define the variables in the data step.
Here's how I like to scan through a variable list and create my new macro variables.
%macro loop(varlist);
%let i=1;
%do %while (%scan(&varlist, &i, "|") ^=%str());
%let var=%scan(&varlist, &i, "|");
%put &var;
*rest of SAS code goes here;
*Increment counter;
%let i=%eval(&i+1);
%end;
%mend;
%let temp=a|b|c|d|e;
%loop(&temp);
You are scanning twice and your delimiter list includes the double quote. I think you may need some macro quoting to protect your program from the words.
Thanks for the double quote correction.
I'm not too concerned about the scanning twice.
I usually use it to scan variable lists, so no issue with extra quotation marks, punctuation in the list.
Any other places that may need macro quoting?
Variable list processing is best done with PROC TRANSPOSE. With transpose you can also process "SAS Variable Lists" like name range, prefix, and enumerated lists.
%let x =a|b|c|d|e|f ;
data _null_;
file "c:\temp\temp.csv";
_file_="&x";
put;
run;
data want;
infile "c:\temp\temp.csv" dlm='|';
input x $ @@;
run;
Do a search for "infile magic" to see how that and MORE are done in one data step.
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.
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.