BookmarkSubscribeRSS Feed
bennessmith
Calcite | Level 5

Class assignment that I am stuck on.  Sorry if this is simple.  I need to split the last variable in the data City, state, zip code into 2 variables city and zip code.  In the code I am calling the variables city and zipcode.  I can not figure out how to have sas read the city separate from the zip code.  I have tried the input statement that is blocked out with the /*.......*/  (this was done without the format statement and the other input statement shown below) but the city length is not the same distance.  I tried a dlm comma statement but not all variables have commas separating,

 

I tried the copied information statement to no avail.

 

 

Data agentd;
informat customer $1. date1 yymmdd10. date2 yymmdd10. city $30. zipcode 6.;
/*input customer $ date1 : yymmdd10. date2 : yymmdd10. city $14. zipcode;*/
input customer date1 date2 city zipcode;
format date1 date2 mmddyy8.;
datalines;
1.  2014-03-11.  2014-05-31.  Bremen, KS, 66412
2.  2015-08-06   2015-09-02.  Little River, KS, 67457

 

Id love any suggestions from the masses of experts

 

Ben

4 REPLIES 4
SASKiwi
PROC Star
Data agentd;
informat customer $2. date1 yymmdd10. date2 yymmdd10. city $30. zipcode 6. city_zipcode $30.;
input customer $ date1 date2 city_zipcode & $30.;
city = scan(city_zipcode, 1, ',');
zipcode = input(scan(city_zipcode, -1, ' '), 5.);
format date1 date2 mmddyy8.;
datalines;
1. 2014-03-11 2014-05-31 Bremen, KS, 66412
2. 2015-08-06 2015-09-02 Little River, KS, 67457
;
run;
bennessmith
Calcite | Level 5

Thank you so very much.  so awesome to see the various ways to do this.

 

If I see that I need to add a variable to this dataset with the same value  how would I do that?

 

example:

 

For each customer I want there to be a variable named AGENT.   Each customer of this data set will have the value for AGENT of D.

 

I know how to add a computed value but not how to add this added consistent value.  

 

 

Finding this group is incredible

 

Thanks again

rvsidhu035
Quartz | Level 8
Data agentd;
informat customer $1. date1 yymmdd10. date2 yymmdd10.;
input customer date1 date2 city_ipcode $30.;
city= substr(city_ipcode,1,length(city_ipcode)-length(scan(city_ipcode,-1,',')));
zipcode=input(scan(city_ipcode,-1,','),6.);
format date1 date2 mmddyy8.;
datalines;
1. 2014-03-11   2014-05-31  Bremen, KS, 66412
2. 2015-08-06   2015-09-02  Little River, KS, 67457
;
run;
Ksharp
Super User
Data agentd;
informat customer $2. date1 yymmdd10. date2 yymmdd10. city $30. zipcode 6. city_zipcode $30.;
input customer $ date1 date2 city_zipcode & $30.;
city = prxchange('s/,\s*\d+$//',1,strip(city_zipcode));
zipcode = input(scan(city_zipcode, -1, ' '), 5.);
format date1 date2 mmddyy8.;
datalines;
1. 2014-03-11 2014-05-31 Bremen, KS, 66412
2. 2015-08-06 2015-09-02 Little River, KS, 67457
;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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