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

I am so lost with this homework assignment but hopefully this will be the last question! The problem instructs us to Use a DO loop and the SUBSTR function to write the SAS code (a complete SAS data step) that would do the same thing as ANYSPACE Function. We have to use two different statements one with a do index and another with a do while. I am just working on the first one for now and made a fake data set. This is what I have but it is completely wrong:

data Question4;
length City $10;
input City $;
datalines;
New York
New Mexico
Las Vegas

;
run;


data new2;
set Question4 (drop = x y);

do x = 1 to 3;
y = substr(City,x,4);
y = ANYSPACE;
end;
run;


Proc Print data=new2;
run;

 

What should I be focusing on to correct it?

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

See if this helps you

 

data Question4;
length City $10;
input City &$;
datalines;
New York
New Mexico
Las Vegas
;
run;

data withAnyspace;
   set Question4;
   x=anyspace(City);
run;

data withoutAnySpace(drop=i);
   set Question4;
   do i=1 to length(City);
      if substr(City, i, 1)=' ' then do;
         x=i;
         leave;
      end;
   end;
run;

View solution in original post

1 REPLY 1
PeterClemmensen
Tourmaline | Level 20

See if this helps you

 

data Question4;
length City $10;
input City &$;
datalines;
New York
New Mexico
Las Vegas
;
run;

data withAnyspace;
   set Question4;
   x=anyspace(City);
run;

data withoutAnySpace(drop=i);
   set Question4;
   do i=1 to length(City);
      if substr(City, i, 1)=' ' then do;
         x=i;
         leave;
      end;
   end;
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
  • 1 reply
  • 293 views
  • 0 likes
  • 2 in conversation