BookmarkSubscribeRSS Feed
aw016
Obsidian | Level 7

Hello there, 

 

I used "if...then do;...end;" statement to get a new dataset, however, the new created variables are all missing values. I already tried the same format codes to generate another similar dataset with no errors.

 

Here is my codes:

data my_OR; set OR;
if effect="LDL_10 at gluc_10=8.80" then do group=1; gluc=88; end;
if effect="LDL_10 at gluc_10=9.90" then do group=1; gluc=99; end;
if effect="LDL_10 at gluc_10=12.50" then do group=1; gluc=125; end;
if effect="Gluc_10 at LDL_10=9.05" then do group=2; LDL=90.5; end;
if effect="Gluc_10 at LDL_10=13.51" then do group=2; LDL=135.1; end;
if effect="Gluc_10 at LDL_10=18.75" then do group=2; LDL=187.5; end;
run;

 

The log looks fine.

NOTE: There were 6 observations read from the data set WORK.OR.
NOTE: The data set WORK.MY_OR has 6 observations and 8 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.03 seconds

 

Here is the output:

aw016_0-1614982497428.png

 

Thank you so much!

 

4 REPLIES 4
ballardw
Super User

How about running this code:

data junk; 
   set OR;
   newvar = quote(effect);
run;
proc print data=junk noobs;
   var newvar;
run;

and show us the result by pasting into a text box opened on the forum using the </> icon.

 

One suspects one or more spelling problems possible with the values of effect in the IF statements. The sneakiest to find are often spaces in the front of the value or in the middle where proportionate fonts make it hard to see the embedded spaces. The Quote function I show will place " " around the value and if there is a leading space (or two) it should be noticeable.

 

Proc Print and most of the procs like Freq will left align text and "lose" a leading space. So you may not see it.

 

 

Reeza
Super User

Your IF statements are not formatted correctly. To do multiple assignments in each IF condition, you need a colon after the DO. 

 

data my_OR; 
set OR;


if effect="LDL_10 at gluc_10=8.80" then do; group=1; gluc=88; end;
else if effect="LDL_10 at gluc_10=9.90" then do; group=1; gluc=99; end;
else if effect="LDL_10 at gluc_10=12.50" then do; group=1; gluc=125; end;
else if effect="Gluc_10 at LDL_10=9.05" then do; group=2; LDL=90.5; end;
else if effect="Gluc_10 at LDL_10=13.51" then do; group=2; LDL=135.1; end;
else if effect="Gluc_10 at LDL_10=18.75" then do; group=2; LDL=187.5; end;
run;

It also seems like you could simplify this drastically:

 

if find(effect, "LDL")>0 then do; 
group=1; 
LDL = input(scan(effect, 2, "="), 8.)*10;
end;
else do;
 group = 2;
gluc = input(scan(effect, 2, "="), 8.)*10;
end;

/*or this way, which is likely what I would do*/
if find(effect, "LDL")>0 then group=1;
else group=2; 
measure = input(scan(effect, 2, "="), 8.)*10;


@aw016 wrote:

Hello there, 

 

I used "if...then do;...end;" statement to get a new dataset, however, the new created variables are all missing values. I already tried the same format codes to generate another similar dataset with no errors.

 

Here is my codes:

data my_OR; set OR;
if effect="LDL_10 at gluc_10=8.80" then do group=1; gluc=88; end;
if effect="LDL_10 at gluc_10=9.90" then do group=1; gluc=99; end;
if effect="LDL_10 at gluc_10=12.50" then do group=1; gluc=125; end;
if effect="Gluc_10 at LDL_10=9.05" then do group=2; LDL=90.5; end;
if effect="Gluc_10 at LDL_10=13.51" then do group=2; LDL=135.1; end;
if effect="Gluc_10 at LDL_10=18.75" then do group=2; LDL=187.5; end;
run;

 

The log looks fine.

NOTE: There were 6 observations read from the data set WORK.OR.
NOTE: The data set WORK.MY_OR has 6 observations and 8 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.03 seconds

 

Here is the output:

aw016_0-1614982497428.png

 

Thank you so much!

 


 

mkeintz
PROC Star

Although @Reeza said you should put semi-colons after DO (and I agree for purposes of good form and clarity), that is not the reason your program fails.  I used it unchanged below and it works as expected.

 

In this particular case the absence of a semi-colon works just fine, because the statement

if effect="LDL_10 at gluc_10=8.80" then do group=1; gluc=88; end;

is effectively 

if effect="LDL_10 at gluc_10=8.80" then do group=1 while (group=1); gluc=88; end;

Edit: (added the "while (group=1)" component above).  The prior "do group=1 to 1" ended up incrementing group to 2.  

 

You probably thought of GROUP as just one of the variables to be assigned a value inside a DO group (I know I did at first), but SAS thinks GROUP is a do loop index.

 

As I said the problem is not in the code - it has to be in the data.  Your code seems to work unchanged here:

data OR;
 length effect $30;
 effect="LDL_10 at gluc_10=8.80" ; output;
 effect="LDL_10 at gluc_10=9.90" ; output;
 effect="LDL_10 at gluc_10=12.50"; output;
 effect="Gluc_10 at LDL_10=9.05" ; output;
 effect="Gluc_10 at LDL_10=13.51"; output;
 effect="Gluc_10 at LDL_10=18.75"; output;
run;

data my_OR; set OR;
if effect="LDL_10 at gluc_10=8.80" then do group=1; gluc=88; end;
if effect="LDL_10 at gluc_10=9.90" then do group=1; gluc=99; end;
if effect="LDL_10 at gluc_10=12.50" then do group=1; gluc=125; end;
if effect="Gluc_10 at LDL_10=9.05" then do group=2; LDL=90.5; end;
if effect="Gluc_10 at LDL_10=13.51" then do group=2; LDL=135.1; end;
if effect="Gluc_10 at LDL_10=18.75" then do group=2; LDL=187.5; end;
put (_all_) (=);
run;

 which produces populated values for group, gluc and LDL in the log.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Tom
Super User Tom
Super User

Right:

do group=1; ... end;

Is an example of this syntax:

do group=1,5,10; ... end;

but with only one item in the list of values.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 449 views
  • 0 likes
  • 5 in conversation