Expecting an arithemetic operator in SAS? - sas

I got the following error:
I don't understand my error please help.

you need space between do and _i_ (index variable) as shown below. as you have it as do_i_. your warning also gives a clue about this.
data RV2;
retain _seed_ 0;
n=20;
p=0.6;
do _i_ = 1 to 100;
binorm1= ranbin(_seed_,n, p);
output;
end;
drop _seed_ _i_;
run;

Related

PRXMATCH not working with PRXPARSE function in SAS

I have comments with multiple ids which I need to pull from comments. Each I’d in separate column is required.
Input data has 2 columns- comment_id & Comment(it has 1 or more IDs)
Desired output should have 2 columns: comment_id & ID
I am using following function.
For Parsing
data work.comments_parsed;
set work.comments;
if _N_ = 1 then do;
pasre_id=prxparse("/ab[c|d]?e?\d+/");
end;
retain pasre_id;
start = 1;
stops = length(Comment);
run;
For output generation
data work.desired_output;
set work.comments_parsed;
length ID $ 500;
call prxnext(pasre_id, start, stops, Comment, pos, len);
do while (pos >0);
ID = substr(Comment,pos,len);
output;
call prxnext(pasre_id, start, stops, Comment, pos, len);
end;
run;
ERROR: Argument 1 to the function PRXNEXT must be a positive integer returned by PRXPARSE for a valid pattern.
ERROR: Internal error detected in function PRXNEXT. The DATA step is terminating during the EXECUTION phase.
I believe error is because of incorrect parsing however when I use prxmatch function by using regular expression directly I am getting proper matching. Can you someone suggest me how I can make this code work.
This code works fine
data pattern_testing;
set work.comments_parsed;
pos = prxmatch("/ab[c|d]?e?\d+?/", Comment);
run;
But this code also gives same error:
data pattern_testing;
set work.comments_parsed;
pos = prxmatch(pasre_id,Comment);
run;
Code works when I have parsing and prxnext in same data step.
data work.comments_parsed;
set work.comments;
if _N_ = 1 then pasre_id = prxparse("/ab[c|d]?e?\d+/");
retain pasre_id;
length gen_string $ 500;
call prxnext(pasre_id, start, stops, COMMENT, pos, len);
do while (pos >0);
gen_string = substr(LAST_COMMENT,pos,len);
output;
call prxnext(pasre_id, start, stops, LAST_COMMENT, pos, len);
end;
run;

SAS FIRST.VARIABLE giving no output

I have some SAS code along the lines of:
DATA MY_SAMPLE;
SET SAMPLE;
BY A;
IF A = 1 THEN B = 1;
ELSE IF A ^= 1 THEN B = 0;
ELSE IF MISSING(A) THEN B = .;
IF FIRST.A;
RUN;
which is returning a set with 0 observations (it shouldn't do this). I have sorted the data by A and tried reading the data into an intermediate dataset before applying the IF FIRST.A but get the same results.
Am I missing something completely obvious? I use the FIRST and LAST all of the time!
Agree with #Robert, the sample code should output records, assuming there are records in your input data and it is sorted.
I would double-check the log from your real program/data, and make sure there are no errors, and that the input dataset has records.
If that doesn't help, I would add some debugging PUT statements, something like below (untested):
DATA MY_SAMPLE;
SET SAMPLE;
BY A;
IF A = 1 THEN B = 1;
ELSE IF A ^= 1 THEN B = 0;
ELSE IF MISSING(A) THEN B = .; *This will never be true ;
put "Before subsetting if " (_n_ A first.A)(=) ;
IF FIRST.A;
put "After subsetting if " (_n_ A first.A)(=) ;
RUN;
As Robert noted, as written your Else if Missing(A) would never be true, because if A is missing the prior Else if A ^= 1 will evaluate to true because SAS uses binary logic (true/false), not trinary logic(true/false/null).
Also I would check for any stray OUTPUT statements in your code.
Checked the log; checked the input; closed MSSQL down; opened it up again and lo and behold, code worked first time. Thanks for the downgrade, but I didn't realize that MSSQL is prone to twitches!

SAS regresses missing values

For some reason when SAS does proportional hazards regression it is including those observations that are specified as . as a group in the results. I suspect it has something to do with how I created my variable (and that SAS thinks my numeric variables are characters) but I can't figure out what I did wrong. I am using SAS 9.4
data final; set final;
if edu_d = 'hs less' then edu_regress = 1;
else if edu_d = 'hs' then edu_regress = 1;
else if edu_d = 'some college' then edu_regress = 2;
else if edu_d = 'college plus' then edu_regress = 3;
else if edu_d = 'missing' then edu_regress=.;
run;
Then I run my regression:
proc phreg data=final;
class edu_regress;
model fuptime*dc(0)=edu_regress/rl;
run;
And the output is as follows:
edu_regress . 1 0.10963 0.12941 0.7177 0.3969 1.116 0.866 1.438
edu_regress 1 1 0.22514 0.10949 4.2278 0.0398 1.252 1.011 1.552
edu_regress 2 1 0.21706 0.11410 3.6190 0.0571 1.242 0.993 1.554
Where . is a category instead of treated as missing.
I'm sure I'm making a rookie mistake but I just can't figure it out.
I would clear your output, and re-run the code, and check the log and output.
As I read the docs, to get missing values treated as a category you would need to have /missing on your CLASS statement, which you do not have in the code shown. Without that, I think missing values should be automatically excluded.
When I run PHREG with a CLASS variable that has missing values, I get a note in the log about observations being deleted due to missing values, and the output shows that the number of observations used is less than the number of observations read.
If SAS thinks edu_regress is character, that's possible if it already was on the dataset as character. This is one reason not to do data x; set x; and instead make a new dataset. You should see notes in the datastep when you run it the way you have now regarding numeric to character conversion, if this is indeed the problem.
Anyway, one way to adjust this is to use CALL MISSING. It sets a variable to missing correctly regardless of the type.
data final;
set final;
if edu_d = 'hs less' then edu_regress = 1;
else if edu_d = 'hs' then edu_regress = 1;
else if edu_d = 'some college' then edu_regress = 2;
else if edu_d = 'college plus' then edu_regress = 3;
else if edu_d = 'missing' then call missing(edu_Regress);
run;

If-then-else matching issue

I am getting an error while running this code
no matching if-then clause
if x=1 then do;
if diff>12 and (8<=p<=12 or 3<=p<5) then g=1;
else if (1<=p<=2) then g=2;
else g=3;
end;
pls help me understand my mistake?
I copied your code and ran the following:
data test;
x = 1;
diff = 14;
p=14;
if x=1 then do;
if diff>12 and (8<=p<=12 or 3<=p<5) then g=1;
else if (1<=p<=2) then g=2;
else g=3;
end;
run;
What seems to be the problem? Make sure x, diff and p are defined.

SAS function for using 'power' / exponential

I may be missing something obvious, but how do you calculate 'powers' in SAS?
Eg X squared, or Y cubed?
what I need is to have variable1 ^ variable2, but cannot find the syntax... (I am using SAS 9.1.3)
got it! there is no function.
you need to do:
variable1 ** variable2;
data t;
num = 5;
pow = 2;
res = num**pow;
run;
proc print data = t;
run;
Use the POWER function and, if necessary, the CONSTANT function.
nbr_squared = power(nbr, 2);
nbr_cubed = power(nbr, 3);
E_to_the_power_2 = power(constant('E'),2);