BookmarkSubscribeRSS Feed
alparle
Calcite | Level 5

Hi everyone, 

 
%let alparle = NCEPA7,NCEPA8;   (note: ncepa7 and ncepa8 are names of variables in work.datahave)
 
proc sql;  create table levo as select ALT,
scan("&alparle." ,1,  ",") 
from work.datahave ; quit;  
 
i want to call only NCEPA7 as variable to my table but the scan function output is "ncepa7" for this reason,
my tablo look like as :
 
ALT_TEMA001
ANCNCEPA7
NON_MARKNCEPA7
MARK_SPECNCEPA7
ANCNCEPA7
But i want to get the table as :
ALTNCEPA7
ANC5
NON_MARK7
MARK_SPEC15
ANC1
 
so which function should i use or how can i eliminate the  quotation marks? 
thank you in advance Best Regards.
 
6 REPLIES 6
russt_sas
SAS Employee

You just need to change this:

scan("&alparle." ,1,  ",") 

to this:

scan(&alparle ,1,  ",") 

This will give you the output you showed.

Quentin
Super User

I'm confused, if NCEP7 is a variable that exists in datahave, what are its values? It looks like you're trying to overwrite that variable with new values (5, 7, 15, 1)?  What is the meaning of those new values, how do you want to calculate them?

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
ballardw
Super User

When you run this code:

%let alparle = NCEPA7,NCEPA8;   (note: ncepa7 and ncepa8 are names of variables in work.datahave)
 
proc sql;  create table levo as select ALT,
scan("&alparle." ,1,  ",") 
from work.datahave ; quit;  
 

The macro variable Alparle is replaced by the defintion.

So SAS sees this as code:

%let alparle = NCEPA7,NCEPA8;   (note: ncepa7 and ncepa8 are names of variables in work.datahave)
 
proc sql;  create table levo as select ALT,
scan("NCEPA7,NCEPA8" ,1,  ",") 
from work.datahave ; quit;  
 

So the SCAN finds the first result in the literal string which is NCEPA7

If  you expect SAS to use code something like this where NCEPA7 is a variable in the Datahave:

proc sql;  create table levo as select ALT,
NCEPA7 
from work.datahave ; quit;  

And especially if you expect to go on to creating some sort of loop to get multiple variables you should describe the whole problem.

alparle
Calcite | Level 5

thanks, it works with " |"  as delimiter but i could not find the define comma as delimiter. 

 

Tom
Super User Tom
Super User

@alparle wrote:

thanks, it works with " |"  as delimiter but i could not find the define comma as delimiter. 

 


So if you use " |" as the delimiter in a %SCAN() call that means that it should treat any of the three characters double quote, space or vertical bar as a possible delimiter.

 

If you want to use comma as the delimiter then you need to use macro quoting (or actual quoting) to make sure the commas do not get seen by SAS as argument delimiters instead of as part of the string.

 

Macro quoting:

%let alparle = NCEPA7,NCEPA8;  
proc sql;  
create table levo as
  select ALT, %scan(%quote(&alparle),1,%str(,)) 
  from work.datahave 
; 
quit;  

Actual quote characters.

%let alparle = NCEPA7,NCEPA8;  
proc sql;  
create table levo as
  select ALT, %scan("&alparle",1,",") 
  from work.datahave 
; 
quit;  

The only added value of throwing space in as an additional delimiter characters is eliminates any spaces that might exist around the actual delimiters.

Example:

48   %let list= A  ,  B;
49   %put |%scan("&list",1,",")|;
|A  |
50   %put |%scan("&list",1," ,")|;
|A|
Tom
Super User Tom
Super User

If you want to generate code then use MACRO LOGIC and not SAS LOGIC.

So use the macro function %SCAN() instead of the SAS function SCAN().

 

Note it will be much easier if you use any other character then comma.  For example you could use |.

%let alparle = NCEPA7|NCEPA8;  
proc sql;
create table levo as 
  select ALT, %scan(&alparle.,1,|) 
  from work.datahave 
; 
quit;  

And if you want to use your variable list in normal SAS code then use space as the delimiter.

%let alparle = NCEPA7 NCEPA8;  
data levo_alparle;
  set datahave (keep=alt &alparle);
run;

To tell %SCAN() that space is the delimiter use macro quoting.

%let alparle = NCEPA7 NCEPA8;  
proc sql;
create table levo as 
  select ALT, %scan(&alparle.,1,%str( )) 
  from work.datahave 
; 
quit;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 507 views
  • 3 likes
  • 5 in conversation