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

Hello Experts,

 

I'm wondering if it is a good way to create the variable New? :

I mean : can I create the variable while merge statement in this way :

/***New contrats***/
data new_contract;
attrib "New?"n format=$8. length=$8. informat=$8.;
	merge DOSS3(in=IN1) DOSS3_N_1_ter(in=IN2);
	by NO_POL;

	if IN1=1;

	if IN2=0 then
		"New?"n="OUI";
	else "New?"n="NON";
run;

Thank you for your advice !

1 ACCEPTED SOLUTION

Accepted Solutions
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @SASdevAnneMarie 

Did you run your code? - If it gave the expected result, it can be done in this way.

 

But it is a BAD IDEA to use name literals to create a new SAS variable. It is a feature implemented i SAS to make it possible to read DBMS column names that do not follow the SAS naming conventions, and It requires the option VALIDVARNAME=ANY to be set in the session. If you create new SAS variables with illegal SAS names, it works only if the option is also set in all following sessions that reads the data set. It shouldn't be necessary to put a questiom mark in a variable name, I would call the variable "New" and assign a boolean value o ot 1 to it.

 

And why do you create the new variable with a length of 8 and formats/informats attached, when you know the possible values to be OUI or NON?

 

I would write the code slightly different myself, but that is a matter of taste, it should work as written.

 

 

/***New contrats***/
data New_contract;
  length New 8; /* Only to get the new variable as first column, otherwise unnecessary */
  merge DOSS3(in=IN1) DOSS3_N_1_ter(in=IN2);
  by NO_POL;

  if IN1=1 then do;
    New = IN2; /* transfers the value of IN2 to a variable that is written to output */
    output;
  end;
run;

 

 

 

 

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

I would never name a variable with a ? in the variable name. I would name the variable NEW. Typing NEW is so much easier (and shorter) than typing "New?"n, and typing NEW will be less prone to typographical errors than typing "New?"n.

 

As far as creating values for NEW, binary variables are best set to numeric 0 and 1 rather than YES/NO or OUI/NON. Not only (again) is this much less typing and less chance for typographical errors. But another benefit to numeric 0 and 1 is that if you compute the mean of these values, the mean is actually the percent of records that have a value of 1.

 

You can use labels and formats to change the appearance, for example, with the proper format 0 will appear as NON and 1 will appear as OUI.

--
Paige Miller
Kurt_Bremser
Super User
data new_contract;
attrib New length=3;
merge DOSS3(in=IN1) DOSS3_N_1_ter(in=IN2);
by NO_POL;
if IN1=1;
New = in2;
run;
  1. Never use name literals. Simply don't.
  2. Code Boolean variables as numeric with 0/1, and assign a format that displays them as wanted if the need arises
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @SASdevAnneMarie 

Did you run your code? - If it gave the expected result, it can be done in this way.

 

But it is a BAD IDEA to use name literals to create a new SAS variable. It is a feature implemented i SAS to make it possible to read DBMS column names that do not follow the SAS naming conventions, and It requires the option VALIDVARNAME=ANY to be set in the session. If you create new SAS variables with illegal SAS names, it works only if the option is also set in all following sessions that reads the data set. It shouldn't be necessary to put a questiom mark in a variable name, I would call the variable "New" and assign a boolean value o ot 1 to it.

 

And why do you create the new variable with a length of 8 and formats/informats attached, when you know the possible values to be OUI or NON?

 

I would write the code slightly different myself, but that is a matter of taste, it should work as written.

 

 

/***New contrats***/
data New_contract;
  length New 8; /* Only to get the new variable as first column, otherwise unnecessary */
  merge DOSS3(in=IN1) DOSS3_N_1_ter(in=IN2);
  by NO_POL;

  if IN1=1 then do;
    New = IN2; /* transfers the value of IN2 to a variable that is written to output */
    output;
  end;
run;

 

 

 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 784 views
  • 2 likes
  • 4 in conversation