BookmarkSubscribeRSS Feed
dmosack
Fluorite | Level 6

Hello!

 

I am working on some existing code, which I did not write. The goal, in one segment, is to convert a list of names (in a macro var) to a list of sequential ID variables, for sorting. E.g.:

Proc format; 

value $A_ORD

"James" = 1

"Pushyami" = 2

; run;

 

The problem is that although this works fine in base SAS, for some reason when I run it in ultraedit it gives me "Syntax error: expecting one of the following: a quoted string, a format name." So, if I had written the program, I would simply not do things this way (or even just reverse the two, using the macro to create the format), but it would be a big big pain to rewrite around this problem. On the other hand, if I simply add quotation marks around each numeral, then the sorting will be alphabetic, which is also not desired. I believe there is a way to ask for numeric sorting, but it would also be a (small) pain. I assume this latter option is what I'll have to do, unless I'm overlooking something.

 

The main thing I'm curious about is why this problem exists at all, which is somewhat baffling to me. Is anyone familiar with using SAS in UltraEdit and able to fill me in?

 

Thank you!

6 REPLIES 6
SASKiwi
PROC Star

Your test program works fine for me in EG. Make sure you don't type MS Word-style opening and closing quotes as they won't work. Try a copy of the code that worked in the SAS editor into UltraEdit. 

dmosack
Fluorite | Level 6

That's what's so strange: I'm not just trying the same logic or a copy of the code -- it's the very same program. When I open and run it in base SAS, it runs fine. In UltraEdit, this error cascades and breaks the program. 

 

We use UltraEdit for regulatory reasons, and we run programs using the "Run DOS command" option under the advanced tab. Because I'm new to this way of using SAS, I don't know if that's normal or not (or how it works really, to be honest). Could it be part of the issue?

Tom
Super User Tom
Super User

You will have to tell us what you are doing with UltraEdit.  What exactly does it do to run SAS code as if it is a DOS command?  Does it save the code into a .sas file and run SAS from the command line?  Does it generate a  SAS log file?

 

Or is it trying to mimic SAS/Studio and send only snippets of code to a running SAS session?

 

If so you probably have the same problem that the SAS extension for VSCode had.  It was not properly quoting the strings it tried to pass and the operating system was "eating" then $ characters.

 

Can you try other SAS code that has $ characters in it? Do they work?  

SASKiwi
PROC Star

Your SAS log should offer some clues as to what is happening here. Please add this to your post if you want us to investigate further.

ballardw
Super User

For SORTING purpose I would use an INVALUE and create a new variable with the value.

 

Proc format; 
invalue A_ORD
"James" = 2
"Pushyami" = 1
"Mary"=3
; 
run;

data  have;
  input name $;
  order = input(name,a_ord.);
datalines;
James
Pushyami
Mary
;

proc sort data=have;
   by order;
run;

I used different "order" values as your small sample was the same as alphabetic.

 

An additional option to consider with this approach is the Invalue option (upcase), which upcases the values before comparing to the list of values. This would be helpful if you may have values like "james" "James" "jAMES" or similar that should all receive the same sort order. The Proc Format code would look like:

Proc format; 
invalue A_ORD (upcase)
"JAMES" = 2
"PUSHYAMI" = 1
"MARY"=3
; 
run;

Another option to consider is adding an Other =_error_ value. That would show an invalid data message in the Log with some details so you can address the case when someone sneaks in a "Jammes" (that would be fixed by added "JAMMES" to the list of values mapped to 2).

 

I use this frequently. I have one report where the sources refuse to spell consistently and I have an invalue like this one that maps about 80 different text values to 5 report level codes.

Tom
Super User Tom
Super User

Note also that to convert character values to numeric values you need to use an INFORMAT.

 

INFORMATs convert text to values.  FORMATs convert values to text.

 

So a numeric informat can convert character to numeric.  But there is no type of format that can convert anything to numeric.  Formats only produce character strings.

 

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
  • 568 views
  • 2 likes
  • 4 in conversation