Learning SAS? Welcome to the exclusive online community for all SAS learners.

Need Solutions for Chapter 4 Question 37 in 'Exercises and Projects for The Little SAS Book'

Reply
New Contributor kbc
New Contributor
Posts: 4

Need Solutions for Chapter 4 Question 37 in 'Exercises and Projects for The Little SAS Book'

I am new to SAS and would like to seek advice on how to read the data set in the SAS data set which looks like this:

Screenshot 2018-04-18 09.26.24.png

I have typed the following code but this results in an error below.

Screenshot 2018-04-18 09.26.47.png

 

Please advise how I could modify my code to read the file correctly.

 

Thank you!

 

Super User
Posts: 23,958

Re: Need Solutions for Chapter 4 Question 37 in 'Exercises and Projects for The Little SAS Book'

 

You DO NOT read a SAS7BDAT file, you simply reference it. You may want to review Chapter 3 again, about how to work with your data. 

 

Screen Shot 2018-04-17 at 7.57.43 PM.png

 

Images for code are not particularly helpful, please post your code as text in the future. 

 


@kbc wrote:

I am new to SAS and would like to seek advice on how to read the data set in the SAS data set which looks like this:

Screenshot 2018-04-18 09.26.24.png

I have typed the following code but this results in an error below.

Screenshot 2018-04-18 09.26.47.png

 

Please advise how I could modify my code to read the file correctly.

 

Thank you!

 



@kbc wrote:

I am new to SAS and would like to seek advice on how to read the data set in the SAS data set which looks like this:

Screenshot 2018-04-18 09.26.24.png

I have typed the following code but this results in an error below.

Screenshot 2018-04-18 09.26.47.png

 

Please advise how I could modify my code to read the file correctly.

 

Thank you!

 


 

New Contributor kbc
New Contributor
Posts: 4

Re: Need Solutions for Chapter 4 Question 37 in 'Exercises and Projects for The Little SAS Book'

Thanks for your help, could you advise how I could change my code so that the substr function could read up to 3 digits?

 

I have modified my code to be as such but I am unable to read the RGB code of my data set when it is less than 3 digits. RGB which are less than 3 digits appear as missing data in the output data set. 

 

My code is as such:

 

LIBNAME Crayola ‘folders/myfolders/Chapter4_data’;

 

DATA Crayons;

            SET Crayola.Crayons;

            Red = INPUT(SUBSTR(RGB, 2,3), 3.);

            Blue = INPUT (SUBSTR(RGB,2,3), 3.);

            Green = INPUT (SUBSTR(RGB,2,3), 3.);

 

PROC SORT DATA = Crayons;

            BY Red Green Blue;

 

 

Thanks for your help!

Super User
Posts: 23,958

Re: Need Solutions for Chapter 4 Question 37 in 'Exercises and Projects for The Little SAS Book'

Read the docs.

 

And I doubt your code is correct, because you're converting the same string to all RGB values. 

 

If you're not sure of the functions do not nest them when testing until they're working.

New Contributor kbc
New Contributor
Posts: 4

Re: Need Solutions for Chapter 4 Question 37 in 'Exercises and Projects for The Little SAS Book'

[ Edited ]

Thanks Reeza for looking through, but the task in this question is to sort the colours in the data set by RGB colours, which is why I converted the string to the RGB colours. 

 

I have tested my data set and only those values with 2 digits and lesser cannot be read.

 

Attached my output for reference.

 

Thanks so much for looking through, and do let me know I should use other methods instead. 

Super User
Posts: 23,958

Re: Need Solutions for Chapter 4 Question 37 in 'Exercises and Projects for The Little SAS Book'

  Red = INPUT(SUBSTR(RGB, 2,3), 3.);

 

Your SUBSTR is always pulling the exact same portion of the RGB string across all the functions. Therefore it is not correct.

 

 

LIBNAME Crayola ‘folders/myfolders/Chapter4_data’;

 

DATA Crayons;

            SET Crayola.Crayons;

            Red = INPUT(SUBSTR(RGB, 2,3), 3.);

            Blue = INPUT (SUBSTR(RGB,2,3), 3.);

            Green = INPUT (SUBSTR(RGB,2,3), 3.);

 

PROC SORT DATA = Crayons;

            BY Red Green Blue;


@kbc wrote:

Thanks Reeza for looking through, but the task in this question is to sort the colours in the data set by RGB colours, which is why I converted the string to the RGB colours. 

 

I have tested my data set and only those values with 2 digits and lesser cannot be read.

 

Attached my output for reference.

 

Thanks so much for looking through, and do let me know I should use other methods instead. 


 

New Contributor kbc
New Contributor
Posts: 4

Re: Need Solutions for Chapter 4 Question 37 in 'Exercises and Projects for The Little SAS Book'

Thanks Reeza,

I have rectified the issue as advised below:

 

DATA Crayons;

     SET Crayola.Crayons;

            Red = INPUT(SUBSTR(RGB, 2,3), 3.);

            Blue = INPUT (SUBSTR(RGB,7,3), 3.);

            Green = INPUT (SUBSTR(RGB,12,3), 3.);

 

However the issue now is that for numbers which are less than 2 digits, the function above is unable to extract the digits.

Is there a way I could change the SUBSTR function such that it could pull out up to 3 digits of my RGB string?

 

Thanks so much for your patience and help thus far, appreciate it!

Ask a Question
Discussion stats
  • 6 replies
  • 218 views
  • 0 likes
  • 2 in conversation