assigning multiple values in a single Macro Variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 81
Accepted Solution

assigning multiple values in a single Macro Variable

Hi All,

I am trying to assign multiple values in single macro variable but it contains only last value out of four values and using following code:

data new;

input columnX @@;

call symput(macvar,columnX);

datalines;

1 2 3 4

;

run;

however if I take two variable then it works fine like this:

data team1;

input position : $8. player : $12.;

call symput(position,player);

datalines;

shortstp Ann

pitcher Tom

frstbase Bill

;

run;

Here three macro variable gets created and each value of position variable has the respective value from variable Player.

Is there any other way to create a macro variable having many values using symput or any other method?????????????

Regards

Anand


Accepted Solutions
Solution
‎09-11-2014 04:25 PM
Super User
Super User
Posts: 7,039

Re: assigning multiple values in a single Macro Variable

Posted in reply to AnandSahu

In SQL you can use the SEPARATED BY clause of the INTO clause or you could create a series of macro variables.

Try this code:

proc sql noprint;

select name into :namelist separated by ' '

from sashelp.class

;

%put |&namelist|;

select name into :name1-:name9999 from sashelp.class;

%put sqlobs=&sqlobs name1=|&name1| name2=|&name2| ;

quit;

View solution in original post


All Replies
Super User
Posts: 5,424

Re: assigning multiple values in a single Macro Variable

Posted in reply to AnandSahu

If you wish to append a value to an existing value you need to use the appropriate syntax, it doesn't do it by itself.

Call symput will reassign the macro variable for each row in the input file, hence the last rows value is the last standing.

You are quite vague in what you wish to achieve, but it sounds like you could get your desired result by looking into sql select into-construct.

Data never sleeps
Frequent Contributor
Posts: 81

Re: assigning multiple values in a single Macro Variable

Hi LinusH,

I know its a vague question but it was asked in an interview so could you please provide a sample code of Select into clause.

Regards

Anand

Solution
‎09-11-2014 04:25 PM
Super User
Super User
Posts: 7,039

Re: assigning multiple values in a single Macro Variable

Posted in reply to AnandSahu

In SQL you can use the SEPARATED BY clause of the INTO clause or you could create a series of macro variables.

Try this code:

proc sql noprint;

select name into :namelist separated by ' '

from sashelp.class

;

%put |&namelist|;

select name into :name1-:name9999 from sashelp.class;

%put sqlobs=&sqlobs name1=|&name1| name2=|&name2| ;

quit;

Frequent Contributor
Posts: 81

Re: assigning multiple values in a single Macro Variable

Thank you so much Tom. This code really enhanced my understanding. Smiley Happy

Could you please explain the macro variable SQLOBS, I don't find how is it created???

Super User
Super User
Posts: 7,039

Re: assigning multiple values in a single Macro Variable

Posted in reply to AnandSahu
Frequent Contributor
Posts: 81

Re: assigning multiple values in a single Macro Variable

thanks Tom, got so many new stuffs....thanks a lot Smiley Happy

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 827 views
  • 4 likes
  • 3 in conversation