BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Geo-
Quartz | Level 8

proc sql;
select distinct variable into:numVarList separated by ' ' from Map_num;
quit;

I got a macro array:

variable
age
agnc_non_ccbt_fnd_bal
marriage
education
loan

 

%put &numVarList.;

result:

age agnc_non_ccbt_fnd_balmarraige education loan

 

%put %sysfunc(scan(&numVarList.,1,str()));

result:

age agnc_non_ccb

what's wrong with this?does it supposed to be 'age'?

 

%put %sysfunc(scan(&numVarList.,1));

result:

age

 

 

and I am going to join  two table:

proc sql;

create table numVar_test as

select start into:start_num separated by ' ' from Map_num

where variable=%sysfunc(scan(&numVarList.,1));

result:

could not find column age.why would be this?

 

proc sql;

create table numVar_test as

select start into:start_num separated by ' ' from Map_num

where variable=%sysfunc(scan(&numVarList.,1,str()));

result is could not be match by the where clause

 

 

proc sql;

create table numVar_test as

select start into:start_num separated by ' ' from Map_num

where variable='age';

the result is what I am looking for.

 

 

table Map_num:

variablestartendwoebeta
age-99999999957853-13
age5785389756-1.13
age89756897452-1.23
age8974529999999999-1.33
agnc_non_ccbt_fnd_bal-999999999423523
agnc_non_ccbt_fnd_bal42356578533
agnc_non_ccbt_fnd_bal65785999999999943
marriage-99999999996733.13
marriage9673753412.13
marriage75341985431.13
marriage985435678640.13
marriage5678649999999999-13
education-999999999837653
education83769384763
education93847999999999973
loan-9999999995675332
loan56753546562442
loan5465624999999999912

 

please check the question color in red,thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Here are a few suggestions.  However, I agree with the thought that you are doing the wrong thing by trying to learn SAS language and macro language at the same time.  You should focus on SAS language first and forget about macro language for at least 6 months.

 

For the %PUT statement:

 

%put %scan(&numvarlist, 1, %str( ) );

 

For the WHERE clause:

 

where variable=scan("&numvarlist", 1);

View solution in original post

6 REPLIES 6
Astounding
PROC Star

The source of the problem is the delimiter.  This would be the correct delimiter:

 

%str( )

 

When using this delimiter:

 

str()

 

This is actually saying that there are 5 characters that can all serve as a delimiter:  "s"  "t"  "r"  "("  and  ")"

 

Also note that there is a %SCAN function.  You don't need to apply %SYSFUNC to SCAN.

Geo-
Quartz | Level 8

thank you.So how exactly the where clause should be?please write out the code,I am new in SAS.And plus,is %SYSFUNC should be used when there is Macro.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

"please write out the code,I am new in SAS." - then why on earth are you writing code like this in the first place?  Thats like trying to lear by jumping in a formula 1 car blinfolded.  Lear Base SAS - which is the programming language - thoroughly.  Once you know that 100% you will realise that you do not need macro ever.  Then you can look at effective areas for incorperating some macro code to make sharing code, or generic tools easier.  Macro does not replace Base SAS!!!

Kurt_Bremser
Super User

@Geo- wrote:

thank you.So how exactly the where clause should be?please write out the code,I am new in SAS.And plus,is %SYSFUNC should be used when there is Macro.


"I am new in SAS"

 

Learn to walk before you try to run. Meaning, you should work through the basic tutorials available with SAS UE first before engaging in complicated programming like you are trying with this code. If you don't want to do that, hire a contractor/consultant.

Astounding
PROC Star

Here are a few suggestions.  However, I agree with the thought that you are doing the wrong thing by trying to learn SAS language and macro language at the same time.  You should focus on SAS language first and forget about macro language for at least 6 months.

 

For the %PUT statement:

 

%put %scan(&numvarlist, 1, %str( ) );

 

For the WHERE clause:

 

where variable=scan("&numvarlist", 1);

Kurt_Bremser
Super User
proc sql;
create table numVar_test as
select start into:start_num separated by ' ' from Map_num
where variable=%sysfunc(scan(&numVarList.,1));

If the result of the scan into &numvarlist is a string, you need to use quotes, otherwise SAS will mis-interpret it as a variable name. Do this instead:

proc sql;
create table numVar_test as
select start into:start_num separated by ' ' from Map_num
where variable="%scan(&numVarList.,1)";

 

sas-innovate-2024.png

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.

 

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
  • 6 replies
  • 771 views
  • 1 like
  • 4 in conversation