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

I am new to SAS and I am currently trying to write a program that uses Proc SQL; to create a table. After the table is created, I am trying to call variable/fields from that table in SCL to use later. Below is the section of code that is getting the error (names of tables and variables have been changed), and the error itself. Any advice on what I'm doing wrong would be greatly appreciated.

 

SUBMIT continue; 

proc sql; 
create table work.TestCount 
as select * , count(*) as field3 
from sasTest.table1 
where field1 ne ""; 
quit; 

endsubmit; 

refTest='work.TestCount'; 
FieldTest=open(refTest,'u'); 

TestField1=getvarc(FieldTest,varnum(FieldTest,field1)); <-- ERROR IS STOPPING PROGRAM HERE 
TestField2=getvarc(FieldTest,varnum(FieldTest,field2)); 
TestField3=getvarn(FieldTest,varnum(FieldTest,field3));

 

*** Line 130: [ERROR: #70] Calling function GETVARC. ERROR: Variable is not defined or does not exist.
Arguments passed to GETVARC:
1 FieldTest = 3
2 (Numeric Expression $T0) = 0
Program returning prematurely at line 130

1 ACCEPTED SOLUTION

Accepted Solutions
gregl_sas
SAS Employee

There are a couple things happening with the GETVARC function in your code:

 

1 - The way you have the GETVARC function defined:

           TestField1=getvarc(FieldTest,varnum(FieldTest,field1));

This code is expecting the FIELD1 variable in the VARNUM function to be a character value.  The way that you have this coded, FIELD1 is a variable that would need to contain the character value 'field1'.  So, your GETVARC statements should look like this:

 

         TestField1=getvarc(FieldTest,varnum(FieldTest,'field1'));

 

2 - Just making the above change is not enough to make the code work though (you will get a different error message).  With the GETVARC/N functions, you need to make an observation from the table available.  This is done with the FETCH or FETCHOBS function.  So, the code after your SUBMIT block would need to look like this:

 

...

endsubmit;

 

refTest='work.TestCount';
FieldTest=open(refTest,'u');

 

rc=fetch(FieldTest);

 

TestField1=getvarc(FieldTest,varnum(FieldTest,'field1'));/* <-- ERROR IS STOPPING PROGRAM HERE */
TestField2=getvarc(FieldTest,varnum(FieldTest,'field2'));
TestField3=getvarn(FieldTest,varnum(FieldTest,'field3'));

 

 

Let me know if you have any problems or questions.

 

Thanks,

Greg

View solution in original post

8 REPLIES 8
pink_poodle
Barite | Level 11

This is a very interesting question. Please try replacing this fragment

refTest='work.TestCount'; 
FieldTest=open(refTest,'u'); 

TestField1=getvarc(FieldTest,varnum(FieldTest,field1));

with this one:

dsid = open( "work.TestCount", "i" );

position = varnum( dsid, field1 );

TestField1 = getvarc( dsid, position );

References

[1] Varnum() function

[2] Open() function

mdlucas1
Fluorite | Level 6

Thanks for replying. This is unfortunately giving a similar error on the Getvarc again. 

 

Here is a copy of the error:

 

*** Line 130: [ERROR: #70] Calling function GETVARC. ERROR: Variable is not defined or does not exist.
Arguments passed to GETVARC:
1 dsid = 3
2 TestField1 = 0
Program returning prematurely at line 130

pink_poodle
Barite | Level 11

What happens if you type

TestField1 = getvarc( dsid, 1 );
mdlucas1
Fluorite | Level 6

It seems to be having trouble reading in any observations from the table even with the 1 hardcoded in.

 

Error:

 

ERROR: No observations have been read from data set WORK.TESTCOUNT.

Arguments passed to GETVARC:
1 dsid = 3
2 (Numeric Literal) = 1
Program returning prematurely at line 130

pink_poodle
Barite | Level 11

@gregl_sas helped me nicely with my SCL question:

 

https://communities.sas.com/t5/New-SAS-User/Using-SCL-to-prevent-entries-already-in-dset-through-PRO...

 

Maybe this person can have a look at this question, too.

gregl_sas
SAS Employee

There are a couple things happening with the GETVARC function in your code:

 

1 - The way you have the GETVARC function defined:

           TestField1=getvarc(FieldTest,varnum(FieldTest,field1));

This code is expecting the FIELD1 variable in the VARNUM function to be a character value.  The way that you have this coded, FIELD1 is a variable that would need to contain the character value 'field1'.  So, your GETVARC statements should look like this:

 

         TestField1=getvarc(FieldTest,varnum(FieldTest,'field1'));

 

2 - Just making the above change is not enough to make the code work though (you will get a different error message).  With the GETVARC/N functions, you need to make an observation from the table available.  This is done with the FETCH or FETCHOBS function.  So, the code after your SUBMIT block would need to look like this:

 

...

endsubmit;

 

refTest='work.TestCount';
FieldTest=open(refTest,'u');

 

rc=fetch(FieldTest);

 

TestField1=getvarc(FieldTest,varnum(FieldTest,'field1'));/* <-- ERROR IS STOPPING PROGRAM HERE */
TestField2=getvarc(FieldTest,varnum(FieldTest,'field2'));
TestField3=getvarn(FieldTest,varnum(FieldTest,'field3'));

 

 

Let me know if you have any problems or questions.

 

Thanks,

Greg

mdlucas1
Fluorite | Level 6

Hi Greg,

 

This worked perfectly. Thanks so much!

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!

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
  • 8 replies
  • 2040 views
  • 3 likes
  • 3 in conversation