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

Hello everyone! 

 

I am a super newbie to SAS and would appreciate your help, in running the code below: 

 

duckypooh_0-1591808065757.png

I want to filter data from the variable: preg_group to include only the miscarriage and normal data, and amongst these data, define new category variables for my BMI and age subsets. 

 

But when i ran the code, i got this log: 

 

duckypooh_1-1591808172776.png

 

 

What is the problem? 

 

TIA everyone! 🙂 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

You can't use an IF command outside of a data step. Remove the RUN; after the DATA IMPORT line (because the RUN; statement ends the data step)

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

You can't use an IF command outside of a data step. Remove the RUN; after the DATA IMPORT line (because the RUN; statement ends the data step)

--
Paige Miller
Kurt_Bremser
Super User

Please do not post code and logs as pictures, because it's unnecessary work for you and makes it harder for us to point out the mistakes.

Use the </> button for logs and other text data, and the "little running man" next to it for SAS code. Just copy/paste the text into the windows opened with these buttons.

You have a RUN statement immediately after the SET statement; this creates a step boundary and ends the DATA step. Therefore the next statements appear in "open code" (outside of a data step), where they are invalid. Remove this unnecessary RUN statement.

Kurt_Bremser
Super User

Improve your coding style by giving each statement its own line, and even more if a statement consists of several elements:

proc import
  out=
  datafile=
  dbms=
  replace
;
sheet = "........";

and so on. This improves the readability of your code, and makes it easier to debug and maintain. Also see Maxim 12.

ed_sas_member
Meteorite | Level 14

Hi @duckypooh 

 

Two things:

- you can use the IF/THEN statement outside a data step. That's why you get some errors in the log. TO avoid this, remove the "RUN;" just after the SET statement

- another thing is that you need to define a sufficient length fo your categorical variables. Otherwise, they will be set to the length of the first value and it will cause unexpected results (truncation)

 

Best,

data import;
	set mcdata.import;
	/*run;  <---- remove run; */
	
	if Preg_Group in ('Miscarriage', 'Normal');
	
	
	length BMI_cat $10.; /* <---- set an appropriate length to avoid truncation */ 
	if BMI > 28 then BMI_cat = 'BMI >28';
	else if 19 <= BMI < 23 then BMI_cat = '19 <=BMI <23';
	else  BMI_cat = 'BMI Other';
	
	length Age_cat $10.; /* <---- set an appropriate length to avoid truncation */ 
	if Age <32 then Age_cat = 'Age <32';
	else if Age >= 32 then Age_cat = 'Age >=32';
	
	ln_Progestrone = log(Progestrone_nmol_L);
	
run;

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
  • 4 replies
  • 794 views
  • 0 likes
  • 4 in conversation