As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


User talk:Jprosenbaum8908

From sasCommunity
Jump to: navigation, search

Asking for help

Hi Jprosenbaum8908

The page Help talk:Contents is probably not the best forum to asking for help. Discussion pages are meant for discussing articles. Have you considered asking a question on SAS-L or looked at the SAS Support website? - Cameron (talk) 18:52, 25 November 2014 (CST)

I have moved your problem from Help talk:Contents to here and added some wiki formatting so that I can understand it a bit better. - Cameron (talk) 00:35, 26 November 2014 (CST)

Check Dataset for Multiple Variables

I have a dataset that I want to perform checks on:

  • 1st check: determine if all wanted variables exist
  • 2nd check: determine if there are no extra variables in the data set

Let's say I am using this data below:

data test1;
input ID SCORE1 SCORE2 SCORE3 SCORE4 SCORE5 SCORE6;
 
cards;
 
24 100 97 99 100 85 85
 
28 98 87 98 100 44 90
 
60 100 97 69 100 100 100
 
65 100 98 100 93 90 100
 
70 99 97 100 100 95 100
 
40 97 99 98 49 100 95
 
190 100 99 97 100 100 90
 
196 100 100 99 100 100 100
 
210 98 85 88 90 80 95 100
 
;
run;

I want to check if ID, SCORE1, SCORE2, SCORE3, and SCORE4 exist in the dataset (let's assume, I am pulling this dataset from a database and checking to see if someone created the correct dataset with those variables).

My desire is to perform those two checks above and either output datasets called Check_var_problem1,if not all the variables exist, and Check_var_problem2, if there exists one or more variables in the dataset. I would also accept a note in the LOG that says there is a missing variable, or there are extra variables.

I tried using a loop to check the variables and set the variables to macro variables:

%let var1 = ID;	
 
%let var2 = SCORE1; 	
 
%let var3 = SCORE2;	
 
%let var4 = SCORE3;	
 
%let var5 = SCORE4; 
 
data Check_var_problem1;
 
dsid=open('test1');
 
do i = 1 to 5; 
 
  check=varnum(dsid,'var&i');
 
  if check=0 then put 'Variable does not exist';
 
end; 
 
run;

Please help if you can. Thanks

The first thing that strikes me is that you are defining the macro variables var1, var2, var3, var4, and var5 but you don't substitute them in the DATA Step by using &var1. etc. Instead, in the statement
check=varnum(dsid,'var&i');
you are looking for a variable called var&i. Because you have used single quotes the &i is not resolved. Also, you have not defined a macro variable called &i, only a SAS variable called i. The macro language is used to write the DATA Step before it is compiled, so you need to know the value of &i before the DATA statement runs. However i is only known after the DO statement executes, which is after compilation. You would be better off forgetting about using macro statements and just using the DATA Step statements. Try using the following DO statement
DO i = 'ID    ', 'SCORE1', 'SCORE2', 'SCORE3', 'SCORE4' ;
and then just use the value of i in the VARNUM function
check=varnum(dsid,i);
and see what happens.
Note that the string starting ID needs to have 4 trailing spaces so that the variable i is six characters long so that the remaining 4 character strings will fit into it too. If you want to see want is happening inside the DO group then add an OUTPUT statement like
OUTPUT ;
before the END statement. - Cameron (talk) 00:35, 26 November 2014 (CST)