DATA Step, Macro, Functions and more

Datalines in macro

Accepted Solution Solved
Reply
Super Contributor
Posts: 266
Accepted Solution

Datalines in macro

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


Accepted Solutions
Solution
‎03-11-2014 09:40 AM
Respected Advisor
Posts: 3,156

Re: Datalines in macro

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;

View solution in original post


All Replies
Solution
‎03-11-2014 09:40 AM
Respected Advisor
Posts: 3,156

Re: Datalines in macro

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;

Super User
Posts: 5,499

Re: Datalines in macro

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.

Respected Advisor
Posts: 3,799

Re: Datalines in macro

Posted in reply to Astounding

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.

Super Contributor
Posts: 266

Re: Datalines in macro

Posted in reply to Astounding

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;

Super User
Posts: 5,499

Re: Datalines in macro

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.

Respected Advisor
Posts: 3,799

Re: Datalines in macro

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.

%let dt= 1|2|'3|4'|4|5;
data test2;
   length temp $256 x $8;
   temp=symget(
'dt');
   do i = 1 by 1;
     
x=scan(temp,i,"|",'Q');
      if missing(x) then leave;
      output;
     
end;
  
stop;
  
drop temp;
   run;
Super User
Posts: 19,772

Re: Datalines in macro

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);

Respected Advisor
Posts: 3,799

Re: Datalines in macro

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.

Super User
Posts: 19,772

Re: Datalines in macro

Posted in reply to data_null__

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?

Respected Advisor
Posts: 3,799

Re: Datalines in macro

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.

Super Contributor
Posts: 275

Re: Datalines in macro

%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;

Respected Advisor
Posts: 3,799

Re: Datalines in macro

Do a search for "infile magic" to see how that and MORE are done in one data step.

🔒 This topic is solved and locked.

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

Discussion stats
  • 12 replies
  • 663 views
  • 8 likes
  • 6 in conversation