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

I hope you are all having a good day. I am currently studying for my Base SAS Certification and I have two questions. My first question deals with concatenation. Why does the code below give a value of "FA" for job category rather than "FA1"?

 

data jobcat;
jobcategory="FA";
joblevel="1";
jobcategory=jobcategory||joblevel;
run;

 

 For the code below, why do we need "else input"? What exactly is this saying?

 

Name $ 1-14 / Address $ 1-14 / City $ 1-12;
if City='New York ' then input @1 State $2.;
else input;
datalines;
Joe Conley
123 Main St.
Janesville
WI
Jane Ngyuen
555 Alpha Ave.
New York
NY
Jennifer Jason
666 Mt. Diablo
Eureka
CA
;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

The data always has a record for state. The code is written so that it only gets the state if city='New York'.

 

However, when city isn't 'New York' you would end up reading that record set's state as the next set's name variable and every other set will input the wrong information for each field.

 

Thus, the record that contains state has to be input, regardless of whether it inputs state or not.

 

Art, CEO, AnalystFinder.com

 

View solution in original post

3 REPLIES 3
ballardw
Super User

Examine the characteristics of the the variable jobcategory (proc contents, column view of data set or any of the other possibilities).

You "assigned" a length of 2 characters with the initial assignment of a value. So there isn't a place to add another character onto the end of the same value.

Consider:

data work.jobcat;
   jobcategory="FA";
   joblevel="1";
   jobcategory2=jobcategory||joblevel;
run;

Your second example is incomplete. But basically it conditionally executes one more read looking for a State value only when the City encountered in the previous not-completely shown input encounters City. The "else input" advance the input pointer one row.

 

Did you run the code without the Else? It becomes very obvious why it is needed if you do and look at the resulting data set.

 

You would have to ask the code writer why they only want a state value when the city of New York is encountered.

smw10
Fluorite | Level 6

Sorry I'm just getting to this now. Thank you for your answer. I'm still a bit confused on the second question. I thought that the input pointer would advance to the next row regardless of the else input statement. Is there any way you can dumb it down more for me? I did try it without the else input.

art297
Opal | Level 21

The data always has a record for state. The code is written so that it only gets the state if city='New York'.

 

However, when city isn't 'New York' you would end up reading that record set's state as the next set's name variable and every other set will input the wrong information for each field.

 

Thus, the record that contains state has to be input, regardless of whether it inputs state or not.

 

Art, CEO, AnalystFinder.com

 

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
  • 3 replies
  • 883 views
  • 0 likes
  • 3 in conversation