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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

12 REPLIES 12
Haikuo
Onyx | Level 15

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;

Astounding
PROC Star

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.

data_null__
Jade | Level 19

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.

Aman4SAS
Obsidian | Level 7

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;

Astounding
PROC Star

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.

data_null__
Jade | Level 19

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;
Reeza
Super User

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

data_null__
Jade | Level 19

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.

Reeza
Super User

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?

data_null__
Jade | Level 19

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.

slchen
Lapis Lazuli | Level 10

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

data_null__
Jade | Level 19

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

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!

How to Concatenate Values

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.

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
  • 12 replies
  • 3910 views
  • 8 likes
  • 6 in conversation