BookmarkSubscribeRSS Feed
me_turcotte
Calcite | Level 5

Hi,

I'm learning SAS and I have a very slow learning curve... I'm getting frustrated :smileysilly:

I have the variable Cow, with that kind of observations :

Cow

1

2

3

3

3

3

3.4

4

2.3

1.2.3

3

3

What I'd like to do is to create a new varibales:

Cow_1 Cow_2 Cow_3 Cow_4

and code, 0 or 1 for each observation according to original observations under the variable cow.

So here is what I tried, but it doesn't work.... I get 0 everywhere :

Data librairy.new;

  set librairy.old;

  Cow_1=0; Cow_2=0; Cow_3=0; Cow_4=0;

  do i = 1 to 4;

  if substr(Cow, i, 1)= 1 then Cow_1= 1;

  if substr(Cow, i, 1)= 2 then Cow_2= 1;

  if substr(Cow, i, 1)= 3 then Cow_3= 1;

  if substr(Cow, i, 1)= 4 then Cow_4= 1;

  end;

  run;

Help please !!!!!

6 REPLIES 6
phkrogh
Calcite | Level 5

The problem is that you use a character function, substr, on number.

So you need to left justify the number, which is automatically converted
to a character variable by the substr function, to get the substr function
to fetch the first character of the previous number:

Data new;

  set a;

  Cow_1=0; Cow_2=0; Cow_3=0; Cow_4=0;

  do i = 1 to 4;

  if substr(left(Cow), i, 1)= 1 then Cow_1= 1;

  if substr(left(Cow), i, 1)= 2 then Cow_2= 1;

  if substr(left(Cow), i, 1)= 3 then Cow_3= 1;

  if substr(left(Cow), i, 1)= 4 then Cow_4= 1;

  end;

  run;

If you do not use the left function substring will see e.g. the number  3.4 as the string "          3.4"

as 3.4 is converted to the string format $12,

thus it will always only fetch a space " " with substr.

I reckon that you still do not attempt to get the output from the program that you really want.

But that would need a precise formulation of your purpose.

Regards PH Krogh

me_turcotte
Calcite | Level 5

Thank you phkroh  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Patrick : This variable is numeric (and the databank is huge !!!!! I juste put an exemple here)

Tom
Super User Tom
Super User

I am not sure what you are trying to count.  Looks like you want to count how many times the digits '1','2','3', and '4' appear in the first four digits of the displayed value of the numbers in the variable COW.   Unless that has some meaning in the coding of the variable COW I cannot figure out why you would want to do that.

But if you want to do that then you can use the COUNTC() function.  Use CATS() to convert COW to a string without SAS writing a note to the log. (Note if some of the values are less than four characters you might need to use CATS(cow,'....') so that you can be sure to have at least four digits for the SUBSTR() function to work on.)

cow_1 = countc( substr( cats( cow ), 1, 4), '1');

cow_2 = countc( substr( cats( cow ), 1, 4), '2');

cow_3 = countc( substr( cats( cow ), 1, 4), '3');

cow_4 = countc( substr( cats( cow ), 1, 4), '4');

art297
Opal | Level 21

SAS does have a steep learning curve but, IMHO, one worth taking the time to learn.

If you read your example data in as numeric, then record 10 would have had a missing value as 1.2.3 is not a number.

That said, and not knowing why you want to create the group of dummy variables you have asked how you might create, they can be created with something like the following:

data want;

  set have (rename=(cow=cows));

  array cow(4);

  do _n_=1 to 4;

    cow(_n_)=0;

  end;

  if substr(strip(put(cows,12.)),1,1) gt 0 then

   cow(substr(strip(put(cows,12.)),1,1))=1;

run;

Patrick
Opal | Level 21

I assume you actually wanted variable COW to be a character variable and not a numeric one. If done so then your code will work.

data have;
  input Cow $;
datalines;
1
2
3
3
3
3
3.4
4
2.3
1.2.3
3
3
;
run;

Data want;
  set have;
  Cow_1=0; Cow_2=0; Cow_3=0; Cow_4=0;
  do i = 1 to 4;
    if substr(Cow, i, 1)= 1 then Cow_1= 1;
    if substr(Cow, i, 1)= 2 then Cow_2= 1;
    if substr(Cow, i, 1)= 3 then Cow_3= 1;
    if substr(Cow, i, 1)= 4 then Cow_4= 1;
  end;
run;


data_null__
Jade | Level 19

data have;

   input Cow $ @@;

   array cow_[4];

  Cow_1=0; Cow_2=0; Cow_3=0; Cow_4=0;

   do j = 1 by 1;

   i = input(scan(cow,j,'.'),f8.);

      if missing(i) then leave;

   cow_ = 1;

      end;

   drop i j;

   datalines;

1 2 3 3 3 3 3.4 4 2.3 1.2.3 3 3

;;;;

   run;

proc print;

   run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2480 views
  • 0 likes
  • 6 in conversation