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

Hello, I got a problem with next piece of code

 

proc sort data = dbraw.subjtbl(keep=s_siteid s_subjid trtgrp screen_fail)
           out = rand(where=(trtgrp ne ' '));
   by s_siteid s_subjid;
run;

in this case variable trtgrp in subjtbl is character, but in rand dataset I need it to be a numeric. How can I get numeric trtgrp? Thanks in advance

 

1 ACCEPTED SOLUTION

Accepted Solutions
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

what are you really asking to be solved.  You have datasteps that just don't make since, you have formats that need quotes, and it seems that your original question is not even supported by the sample code that you provided.

 

to change trtgrp to numeric you have to assign a dummy variable to trtgrp, drop trtgrp and then recreate trtgrp as numeric.

 

@IGK22 wrote How can I get numeric trtgrp?

data have2;

length dummy 8.;

set have;

dummy = trtgrp;

drop trtgrp;

run;

data have3;

set have2;

trtgrp=dummy;

drop dummy;

run;

 

 

View solution in original post

11 REPLIES 11
Kurt_Bremser
Super User

I see no input function here. Please post the code you have the problem with (as stated in your subject).


@IGK22 wrote:

Hello, I got a problem with next piece of code

 

proc sort data = dbraw.subjtbl(keep=s_siteid s_subjid trtgrp screen_fail)
           out = rand(where=(trtgrp ne ' '));
   by s_siteid s_subjid;
run;

in this case variable trtgrp in subjtbl is character, but in rand dataset I need it to be a numeric. How can I get numeric trtgrp? Thanks in advance

 


 

IGK22
Calcite | Level 5

I just don't know where to put this input function :/, if i do just input(trtgrp) in doesn't work

PaigeMiller
Diamond | Level 26

The INPUT function must be used in a SAS data step, or in PROC SQL. The data step would come after your PROC SORT, and you will have to create a new variable with a new name that is numeric, that's the only way — you can't really change a variable from character to numeric, you have to create a new variable that is numeric.

--
Paige Miller
IGK22
Calcite | Level 5

so firstly there should be

 

proc sort data = dbraw.subjtbl(keep=s_siteid s_subjid trtgrp screen_fail)
           out = rand;
   by s_siteid s_subjid;
run;

and then i just created data step rand where i'm using input? the problem is i don't know how to use it at all, here what i got.

 

 

data rand(where=(trtgrp ne .));
input(trtgrp, TRTMNT.);
run;

and the result 

 

 

ERROR 22-322: Syntax error, expecting one of the following: a name, arrayname, ), -, :, [, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_, {.

ERROR 76-322: Syntax error, statement will be ignored.

469  run;

ERROR: No DATALINES or INFILE statement.
ERROR: Variable trtgrp is not on file WORK.TRTDATA.

I also got created format TRMNT so i want to attach this format to trtgrp, that's why i need to change from character to numeric.

value TRTMNT
	  1 = DRUG ABC
	  2 = COMPARE XYZ
	  ;

 

 

Kurt_Bremser
Super User

@IGK22 wrote:


 

data rand(where=(trtgrp ne .));
input(trtgrp, TRTMNT.);
run;

 

Functions return values and are not designed to be used on their own, but as a part of an expression that is on the right side of an assignment or part of a condition (the exception is substr()).

CALL routines, OTOH, are used as stand-alone statements.

PaigeMiller
Diamond | Level 26

@IGK22 wrote:

so firstly there should be

 

proc sort data = dbraw.subjtbl(keep=s_siteid s_subjid trtgrp screen_fail)
           out = rand;
   by s_siteid s_subjid;
run;

and then i just created data step rand where i'm using input? the problem is i don't know how to use it at all, here what i got.

 

 

data rand(where=(trtgrp ne .));
input(trtgrp, TRTMNT.);
run;

and the result 

 

 

ERROR 22-322: Syntax error, expecting one of the following: a name, arrayname, ), -, :, [, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_, {.

ERROR 76-322: Syntax error, statement will be ignored.

469  run;

ERROR: No DATALINES or INFILE statement.
ERROR: Variable trtgrp is not on file WORK.TRTDATA.

 


 

The SAS documentation for the INPUT function is very clear and gives examples of use.

https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=lefunctionsref&docsetTarget=p...

 

Assuming you have properly defined the format TRTMNT, then this is the correct syntax

 

newvariablename = input(trtgrp, TRTMNT.);
--
Paige Miller
IGK22
Calcite | Level 5

also if i do in this way i got an error - ERROR 48-59: The informat TRTMNT was not found or could not be loaded. why cant it see my formats?

data trtdata(where=(trtgrp ne .));
  input trtgrp TRTMNT.;
run;

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

what are you really asking to be solved.  You have datasteps that just don't make since, you have formats that need quotes, and it seems that your original question is not even supported by the sample code that you provided.

 

to change trtgrp to numeric you have to assign a dummy variable to trtgrp, drop trtgrp and then recreate trtgrp as numeric.

 

@IGK22 wrote How can I get numeric trtgrp?

data have2;

length dummy 8.;

set have;

dummy = trtgrp;

drop trtgrp;

run;

data have3;

set have2;

trtgrp=dummy;

drop dummy;

run;

 

 

IGK22
Calcite | Level 5
That's exactly what i needed, thanx
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

If your problem has been solved please mark the solution that applies.

Thank You. Smiley Happy

Astounding
PROC Star

You have several issues here.  Locating formats is just one (and not the easiest one).  So let me try to steer you in a different direction in case it is appropriate.

 

data want;

set rand;

if trtgrp='DRUG ABC' then new_group = 1;

else if trtgrp='COMPARE XYZ' then new_group = 2;

 

*** optionally:  drop trtgrp;

*** optionally:  rename new_group = trtgrp;

run;

 

If you only have these two categories, this makes the most sense.  A more complex program that incorporates PROC FORMAT would only be appropriate if you have many more categories.

 

You have the option of just using a new variable (and applying y our format to the new variable) or replacing the original variable through the combination of drop and rename.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 3140 views
  • 0 likes
  • 5 in conversation