BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ABritinAus
Obsidian | Level 7

Hi.

One of my SAS users has asked me to explain why the code below does not do what they expect - ie create a numeric variable (newvar) which is equal to 1 when the character variable myvar has a value of D01a_tot and is missing otherwise.

 

Now I realise there are many workarounds to this, which I have already fedback, and also that using the informat to code just one value is probably not necessary.

 

But I'd still be keen to know why ALL records get assigned a value of 1 when the numeric variable is created. Clearly all the records start with "D01a_Tot" and I guess that is causing the issue.

 

Can anyone explain why its happening and whether it's actually not "unexpected".

 

Thanks!

 

Code below (running on SAS 9.4M3)

data temp;

length myvar $20;

myvar="D01a_Tot_D02"; output;

myvar="D01a_TotP09"; output;

myvar="D01a_TotP12"; output;

myvar="D01a_TotP15"; output;

myvar="D01a_TotP16"; output;

myvar="D01a_Tot"; output;

run;

 

proc format;

INVALUE my_order

"D01a_Tot" = 1;

run;

 

data temp2;

set temp;

newvar=input(myvar,my_order.);

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
LaurieF
Barite | Level 11

The default length of the invalue is the length of D01a_Tot - 8, which is the length of the comparison being made on your myvar variable; everything after that is ignored.

 

If you change the format to

proc format;
INVALUE my_order(default=20)
"D01a_Tot" = 1
other = .;             /* Stops error messages */
run;

 

or the data step to:

data temp2;
set temp;
newvar=input(myvar, my_order20.);
run;

then you'll get the result you require. Note that the second suggestion doesn't stop error messages.

 

I would recommend the former, because you can reuse the format in all your other data steps without thinking about it.

View solution in original post

5 REPLIES 5
LaurieF
Barite | Level 11

The default length of the invalue is the length of D01a_Tot - 8, which is the length of the comparison being made on your myvar variable; everything after that is ignored.

 

If you change the format to

proc format;
INVALUE my_order(default=20)
"D01a_Tot" = 1
other = .;             /* Stops error messages */
run;

 

or the data step to:

data temp2;
set temp;
newvar=input(myvar, my_order20.);
run;

then you'll get the result you require. Note that the second suggestion doesn't stop error messages.

 

I would recommend the former, because you can reuse the format in all your other data steps without thinking about it.

ABritinAus
Obsidian | Level 7

Thanks.

 

That all makes sense.

 

 

ballardw
Super User

The INPUT function sometimes behaves a bit differently than an input statement. Please see this:

proc format library=work;
INVALUE my_order
"D01a_Tot" = 1
other = .
;
run;
data temp;
   informat x my_order.;
   input x;
datalines;
D01a_Tot_D02
D01a_TotP09
D01a_TotP12
D01a_TotP15
D01a_TotP16
D01a_Tot
run;

Note that to assign other values such as missing it is a good idea to explicitly set that in the INVALUE so there isn't any doubt of intent.

 

 

It helps to post code into a code box opened using either the {i} or "running man" icon in this forum.

ABritinAus
Obsidian | Level 7

Thanks for the prompt reply - the time zone here is handy for overnight responsesSmiley Happy

 

Will use that for reading in data using informats.  

 

And thanks for the tip re inserting the code: will note for future.

LaurieF
Barite | Level 11

I'm only two hours (probably) ahead of you - Wellington!

 

Glad to have helped.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 1031 views
  • 0 likes
  • 3 in conversation