Hi all,
In SAS EG, I can use Ctr+Left to move to the beginning of the next word after delimiter ",". For example, I have a series of the word as below:
if GEOGN in(AUSTRALIA,AUSTRIA,BELGIUM,CANADA,SWITZERLAND,CHILE,CYPRUS,CZECH,GERMANY,DENMARK,
ESTONIA,FINLAND,FRANCE,UNITEDK,GREECE,CROATIA,HUNGARY,IRELAND,ICELAND,JAPAN,LITHUANIA,
LUXEMBOURG,LATVIA,NETHERLANDS,NORWAY,NEWZEALAND,PORTUGAL,SINGAPORE,SLOVAKIA,SLOVENIA,SWEDEN,UNITEDS)
then pol_sta=1;
So, assume the cursor is right after the last word"A" in AUSTRALIA, I need to press the combination Ctr+Left TWICE to reach the end of "AUSTRIA". Because I need to deal with this thing hundreds of times so my question is: How to assign a keyboard macro to just use only one time hit to move directly from after the last character of AUSTRALIA to after the last character of AUSTRIA.
To be more specific:
From
to
Warmest regards.
You need four semi-colons to mark the end of the in-line data when you use the CARDS4 statement (what the new documentation calls a DATALINES4 statement, but that is loo long to type and not as descriptive to me as CARDS). This to allow lines of data that include an actual semi-colon character.
The extra missing values of WORD are caused by the trailing commas at the ends of the input lines. So both the comma and the end of line are treated as delimiting the words so with the DSD option the result is a blank word. You could easily suppress those by adding by only running the PUT statement when WORD is not empty.
For the third point you will see that a comma is missing between "FRANCE" and "UNITEDK" since the PUT statement did not add one when it inserted a new line. You could fix that by writing your own comma. If you switch the output delimiter to a space then the result is more like what you want.
So this code:
data _null_;
infile cards dsd flowover ;
input word :$200. @@;
file log dsd dlm=' ';
if not missing(word) then put word ~ ',' @;
cards4;
AUSTRALIA,AUSTRIA,BELGIUM,CANADA,SWITZERLAND,CHILE,CYPRUS,CZECH,GERMANY,DENMARK,
ESTONIA,FINLAND,FRANCE,UNITEDK,GREECE,CROATIA,HUNGARY,IRELAND,ICELAND,JAPAN,LITHUANIA,
LUXEMBOURG,LATVIA,NETHERLANDS,NORWAY,NEWZEALAND,PORTUGAL,SINGAPORE,SLOVAKIA,SLOVENIA,SWEDEN,UNITEDS
;;;;
Generates:
"AUSTRALIA" ,"AUSTRIA" ,"BELGIUM" ,"CANADA" ,"SWITZERLAND" ,"CHILE" ,"CYPRUS" ,"CZECH" ,"GERMANY" ,"DENMARK" ,"ESTONIA" ,"FINLAND" , "FRANCE" ,"UNITEDK" ,"GREECE" ,"CROATIA" ,"HUNGARY" ,"IRELAND" ,"ICELAND" ,"JAPAN" ,"LITHUANIA" ,"LUXEMBOURG" ,"LATVIA" , "NETHERLANDS" ,"NORWAY" ,"NEWZEALAND" ,"PORTUGAL" ,"SINGAPORE" ,"SLOVAKIA" ,"SLOVENIA" ,"SWEDEN" ,"UNITEDS" ,
Now you just have one extra comma after the last word.
Please reconsider this phrasing:
How to assign a keyboard macro to just use only one time hit to move directly from the last word of AUSTRALIA to the end of AUSTRIA.
Since both of those are single word I am very confused about "last word of Australia".
Hi @ballardw .
Thank you for pointing it out.
I am sorry for confusing you and others. Yes, it should be
How to assign a keyboard macro to just use only one time hit to move directly from the after the last character of AUSTRALIA to after the last character of AUSTRIA.
Warm regards.
P/S: I also updated this on my original post.
You might get farther learning how to program in SAS instead of trying to learn how to program whatever editor you are using.
You could for example create a simple macro that will convert such a comma separated list into a quoted comma separated list. Let's say you made such a macro and named it QLIST then instead of working so hard to add quotes into your program text just add the macro call.
if GEOGN in %qlist(AUSTRALIA,AUSTRIA,BELGIUM,CANADA,SWITZERLAND,CHILE,CYPRUS,CZECH,GERMANY,DENMARK,
ESTONIA,FINLAND,FRANCE,UNITEDK,GREECE,CROATIA,HUNGARY,IRELAND,ICELAND,JAPAN,LITHUANIA,
LUXEMBOURG,LATVIA,NETHERLANDS,NORWAY,NEWZEALAND,PORTUGAL,SINGAPORE,SLOVAKIA,SLOVENIA,SWEDEN,UNITEDS)
then pol_sta=1;
If you had such a macro you could just execute this statement in SAS
%put %qlist(AUSTRALIA,AUSTRIA,BELGIUM,CANADA,SWITZERLAND,CHILE,CYPRUS,CZECH,GERMANY,DENMARK,
ESTONIA,FINLAND,FRANCE,UNITEDK,GREECE,CROATIA,HUNGARY,IRELAND,ICELAND,JAPAN,LITHUANIA,
LUXEMBOURG,LATVIA,NETHERLANDS,NORWAY,NEWZEALAND,PORTUGAL,SINGAPORE,SLOVAKIA,SLOVENIA,SWEDEN,UNITEDS) ;
And copy and paste the resulting string from the SAS log back into your program editor.
Or without the macro just run a simple datastep instead and copy and paste the results from the SAS log. You might need to double check that there are enough commas when the log wraps to a new line.
data _null_;
infile cards dsd flowover ;
input word :$200. @@;
file log dsd ;
put word ~ @;
cards4;
AUSTRALIA,AUSTRIA,BELGIUM,CANADA,SWITZERLAND,CHILE,CYPRUS,CZECH,GERMANY,DENMARK,
ESTONIA,FINLAND,FRANCE,UNITEDK,GREECE,CROATIA,HUNGARY,IRELAND,ICELAND,JAPAN,LITHUANIA,
LUXEMBOURG,LATVIA,NETHERLANDS,NORWAY,NEWZEALAND,PORTUGAL,SINGAPORE,SLOVAKIA,SLOVENIA,SWEDEN,UNITEDS
;;;;
Hi @Tom
Thank you for your dedicated guidance.
I did not catch your point here, is there any guidance to create this macro converting such a comma separated list into a quoted comma separated list
You could for example create a simple macro that will convert such a comma separated list into a quoted comma separated list. Let's say you made such a macro and named it QLIST
Regarding your code
data _null_;
infile cards dsd flowover ;
input word :$200. @@;
file log dsd ;
put word ~ @;
cards4;
AUSTRALIA,AUSTRIA,BELGIUM,CANADA,SWITZERLAND,CHILE,CYPRUS,CZECH,GERMANY,DENMARK,
ESTONIA,FINLAND,FRANCE,UNITEDK,GREECE,CROATIA,HUNGARY,IRELAND,ICELAND,JAPAN,LITHUANIA,
LUXEMBOURG,LATVIA,NETHERLANDS,NORWAY,NEWZEALAND,PORTUGAL,SINGAPORE,SLOVAKIA,SLOVENIA,SWEDEN,UNITEDS
;;;;
My log shows some quotation of blank as being highlighted as below:
For example, the one here "DENMARK"," ","ESTONIA"
28 data _null_;
29 infile cards dsd flowover ;
30 input word :$200. @@;
31 file log dsd ;
32 put word ~ @;
33 cards4;
"AUSTRALIA","AUSTRIA","BELGIUM","CANADA","SWITZERLAND","CHILE","CYPRUS","CZECH","GERMANY","DENMARK"," ","ESTONIA","FINLAND","FRANCE"
"UNITEDK","GREECE","CROATIA","HUNGARY","IRELAND","ICELAND","JAPAN","LITHUANIA"," ","LUXEMBOURG","LATVIA","NETHERLANDS","NORWAY"
"NEWZEALAND","PORTUGAL","SINGAPORE","SLOVAKIA","SLOVENIA","SWEDEN","UNITEDS"
Could you please lemme know how to sort it ?
And I am not familiar with this sentence, could you please tell me what do you mean here
You might need to double check that there are enough commas when the log wraps to a new line.
And can I ask why do you have four commas at the end of your code like that?
;;;;
Sorry, I know these are quite a novice questions but I am still learning and not familiar with them.
Warmest regards.
You need four semi-colons to mark the end of the in-line data when you use the CARDS4 statement (what the new documentation calls a DATALINES4 statement, but that is loo long to type and not as descriptive to me as CARDS). This to allow lines of data that include an actual semi-colon character.
The extra missing values of WORD are caused by the trailing commas at the ends of the input lines. So both the comma and the end of line are treated as delimiting the words so with the DSD option the result is a blank word. You could easily suppress those by adding by only running the PUT statement when WORD is not empty.
For the third point you will see that a comma is missing between "FRANCE" and "UNITEDK" since the PUT statement did not add one when it inserted a new line. You could fix that by writing your own comma. If you switch the output delimiter to a space then the result is more like what you want.
So this code:
data _null_;
infile cards dsd flowover ;
input word :$200. @@;
file log dsd dlm=' ';
if not missing(word) then put word ~ ',' @;
cards4;
AUSTRALIA,AUSTRIA,BELGIUM,CANADA,SWITZERLAND,CHILE,CYPRUS,CZECH,GERMANY,DENMARK,
ESTONIA,FINLAND,FRANCE,UNITEDK,GREECE,CROATIA,HUNGARY,IRELAND,ICELAND,JAPAN,LITHUANIA,
LUXEMBOURG,LATVIA,NETHERLANDS,NORWAY,NEWZEALAND,PORTUGAL,SINGAPORE,SLOVAKIA,SLOVENIA,SWEDEN,UNITEDS
;;;;
Generates:
"AUSTRALIA" ,"AUSTRIA" ,"BELGIUM" ,"CANADA" ,"SWITZERLAND" ,"CHILE" ,"CYPRUS" ,"CZECH" ,"GERMANY" ,"DENMARK" ,"ESTONIA" ,"FINLAND" , "FRANCE" ,"UNITEDK" ,"GREECE" ,"CROATIA" ,"HUNGARY" ,"IRELAND" ,"ICELAND" ,"JAPAN" ,"LITHUANIA" ,"LUXEMBOURG" ,"LATVIA" , "NETHERLANDS" ,"NORWAY" ,"NEWZEALAND" ,"PORTUGAL" ,"SINGAPORE" ,"SLOVAKIA" ,"SLOVENIA" ,"SWEDEN" ,"UNITEDS" ,
Now you just have one extra comma after the last word.
Hi @Phil_NZ,
Another simple way to put quotes around comma-separated words in the editor is to mark the list and then use "Replace in Selection" to replace the commas by quoted commas:
This gives you all the quotes you need except the first and the last, but you can add these two manually or via keyboard shortcut, as discussed yesterday. Moreover, you may need to fix quotes before line breaks, but this, again, can be done with the Replace dialog:
The \n is the regular expression for the "carriage return" character, so you just change the order of the quote and that character. (I hope this works with your SAS EG interface as well.)
Hi @FreelanceReinh and @Tom
Thank you for your bits of help and dedicated explanation. I tried and learned and can say that these two methods both work so far.
Warmest regards.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.